![]() |
AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
|
Same as ExpressionParser
but using modified grammar to provide the usual left-associative expressions.
More...
Static Public Member Functions | |
static void | main (String[] args) |
test and example for usage More... | |
static void | main (String[] args) |
test and example for usage More... | |
Protected Member Functions | |
ExpressionTree | sum (int level) |
parse sum More... | |
ExpressionTree | product (int level) |
parse product More... | |
![]() | |
String | errorNear () |
helper: generate a simple error message More... | |
int | lookahead () |
helper: "lookahead" is the usual phrasing More... | |
void | nextToken () |
helper: consume current token and advance to next token More... | |
String | matchedText () |
helper: get match corresponding to lookahead More... | |
void | expect (int tokenid, String text) |
helper: check token (without calling nextToken !) More... | |
void | verbose (int level, String message) |
helper: print out information More... | |
ExpressionTree | expression (int level) |
parse expression More... | |
ExpressionTree | sum (int level) |
parse sum More... | |
ExpressionTree | product (int level) |
parse product More... | |
ExpressionTree | factor (int level) |
parse factor More... | |
Additional Inherited Members | |
![]() | |
ExpressionTree | parse (String input) |
parse input More... | |
void | setVerbose (boolean b) |
set verbose mode (report state to System.err ) More... | |
Same as ExpressionParser
but using modified grammar to provide the usual left-associative expressions.
Here is the "correct" grammar, that implements left-associative expressions, i.e., a+b+c=(a+b)+c
and a*b*c=(a*b)*c
.
expression ::= sum
sum ::= product |
sum '+' product | sum '-' product
product ::= factor |
product '*' factor | product '/' factor
factor ::= number | symbol | '(' expression ')' |
'+' factor | '-' factor
Important note: We cannot implement this directly with a recursive descent parser as this would result in an infinite recursion! (This type of recursion is generally referred to as left recursion and requires a transformation of the grammar.)
I don't go into details but provide a simple solution right ahead: model part of the recursion (the left recursion) using an iterative implementation. (Can you see how the transformed grammar would look like?)
Definition at line 36 of file ExpressionParser2.java.
|
static |
test and example for usage
Reimplemented from aud.example.expr.ExpressionParser.
Definition at line 110 of file ExpressionParser2.java.
References aud.example.expr.ExpressionParser.parse(), aud.BinaryTree< T >.postorder(), aud.example.expr.ExpressionParser.setVerbose(), aud.BinaryTreeTraversal< T >.Traversal.toString(), and aud.BinaryTree< T >.toTikZ().
|
protected |
parse product
product ::= factor |
product '*' factor | product '/' factor
Reimplemented from aud.example.expr.ExpressionParser.
Definition at line 82 of file ExpressionParser2.java.
References aud.Queue< T >.dequeue(), aud.example.expr.Tokenizer.DIVIDE, aud.Queue< T >.enqueue(), aud.example.expr.ExpressionParser.factor(), aud.Queue< T >.is_empty(), aud.example.expr.ExpressionParser.lookahead(), aud.example.expr.ExpressionParser.nextToken(), aud.example.expr.Tokenizer.TIMES, and aud.example.expr.ExpressionParser.verbose().
Referenced by aud.example.expr.ExpressionParser2.sum().
|
protected |
parse sum
sum ::= product |
sum '+' product | sum '-' product
Reimplemented from aud.example.expr.ExpressionParser.
Definition at line 50 of file ExpressionParser2.java.
References aud.Queue< T >.dequeue(), aud.Queue< T >.enqueue(), aud.Queue< T >.is_empty(), aud.example.expr.ExpressionParser.lookahead(), aud.example.expr.Tokenizer.MINUS, aud.example.expr.ExpressionParser.nextToken(), aud.example.expr.Tokenizer.PLUS, aud.example.expr.ExpressionParser2.product(), and aud.example.expr.ExpressionParser.verbose().