![]() |
AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
|
Simple expression parser. More...
Public Member Functions | |
ExpressionTree | parse (String input) |
parse input More... | |
void | setVerbose (boolean b) |
set verbose mode (report state to System.err ) More... | |
Static Public Member Functions | |
static void | main (String[] args) |
test and example for usage More... | |
Protected Member Functions | |
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... | |
Simple expression parser.
Build ExpressionTree
from (infix) string representation of a term.
Implements a top-down recusive descent parser based on the following rules in BNF form (w/o <>
to ease HTML formatting of this page)
expression ::= sum
sum ::= product |
product '+' sum | product '-' sum
product ::= factor |
factor '*' product | factor '/' product
factor ::= number | symbol | '(' expression ')' |
'+' factor | '-' factor
<number>
and <symbol>
are identified during the lexcical analysis. They correspond to AtomicExpression
instances, which are used to build the tree. The same applies to operators '+'
,...
Notes
The left-hand-sides of the BNF rules are "mapped" to (recursive) method calls, e.g. expression
.
The level
argument is provided only for formatting verbose
output.
The parser uses a Tokenizer
for lexcical scanning.
The current token is used for "looking ahead" (lookahead
): it is advanced (by nextToken
only if this token is "consumed" by part of a BNF rule.
The current implementation has a problem! Do you see what and why?
Definition at line 61 of file ExpressionParser.java.
|
protected |
helper: generate a simple error message
Definition at line 85 of file ExpressionParser.java.
Referenced by aud.example.expr.ExpressionParser.factor().
|
protected |
helper: check token (without calling nextToken
!)
SyntaxError | if token does not match |
Definition at line 112 of file ExpressionParser.java.
References aud.example.expr.ExpressionParser.lookahead().
Referenced by aud.example.expr.ExpressionParser.factor().
|
protected |
parse expression
expression ::= sum
Definition at line 134 of file ExpressionParser.java.
References aud.example.expr.ExpressionParser.sum(), and aud.example.expr.ExpressionParser.verbose().
Referenced by aud.example.expr.ExpressionParser.factor().
|
protected |
parse factor
factor ::= number | symbol | '(' expression ')' |
'+' factor | '-' factor
The last part of the rule correspong to unary plus and minus operators +x
and -x
. The implementation ignores unary plus, i.e., +x -> x
.
Definition at line 199 of file ExpressionParser.java.
References aud.example.expr.ExpressionParser.errorNear(), aud.example.expr.ExpressionParser.expect(), aud.example.expr.ExpressionParser.expression(), aud.example.expr.ExpressionParser.factor(), aud.example.expr.Tokenizer.IDENTIFIER, aud.example.expr.Tokenizer.LEFT_PAREN, aud.example.expr.ExpressionParser.lookahead(), aud.example.expr.ExpressionParser.matchedText(), aud.example.expr.Tokenizer.MINUS, aud.example.expr.ExpressionParser.nextToken(), aud.example.expr.Tokenizer.NUMBER, aud.example.expr.Tokenizer.PLUS, aud.example.expr.Tokenizer.RIGHT_PAREN, and aud.example.expr.ExpressionParser.verbose().
Referenced by aud.example.expr.ExpressionParser.factor(), and aud.example.expr.ExpressionParser.product().
|
protected |
helper: "lookahead" is the usual phrasing
Definition at line 91 of file ExpressionParser.java.
Referenced by aud.example.expr.ExpressionParser.expect(), aud.example.expr.ExpressionParser.factor(), aud.example.expr.ExpressionParser.product(), and aud.example.expr.ExpressionParser.sum().
|
static |
test and example for usage
Definition at line 237 of file ExpressionParser.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 |
helper: get match corresponding to lookahead
Tokenizer#matchedText
Definition at line 105 of file ExpressionParser.java.
Referenced by aud.example.expr.ExpressionParser.factor().
|
protected |
helper: consume current token and advance to next token
Definition at line 96 of file ExpressionParser.java.
Referenced by aud.example.expr.ExpressionParser.factor(), aud.example.expr.ExpressionParser.product(), and aud.example.expr.ExpressionParser.sum().
ExpressionTree aud.example.expr.ExpressionParser.parse | ( | String | input | ) |
parse input
input | term |
SyntaxError | on syntax error |
Definition at line 71 of file ExpressionParser.java.
Referenced by aud.example.expr.ExpressionParser.main(), and aud.example.expr.ExpressionTreeTraversal.main().
|
protected |
parse product
product ::= factor |
factor '*' product | factor '/' product
Definition at line 170 of file ExpressionParser.java.
References aud.example.expr.Tokenizer.DIVIDE, aud.example.expr.ExpressionParser.factor(), aud.example.expr.ExpressionParser.lookahead(), aud.example.expr.ExpressionParser.nextToken(), aud.example.expr.ExpressionParser.product(), aud.example.expr.Tokenizer.TIMES, and aud.example.expr.ExpressionParser.verbose().
Referenced by aud.example.expr.ExpressionParser.product(), and aud.example.expr.ExpressionParser.sum().
void aud.example.expr.ExpressionParser.setVerbose | ( | boolean | b | ) |
set verbose mode (report state to System.err
)
Definition at line 80 of file ExpressionParser.java.
Referenced by aud.example.expr.ExpressionParser.main().
|
protected |
parse sum
sum ::= product |
product '+' sum | product '-' sum
Definition at line 145 of file ExpressionParser.java.
References aud.example.expr.ExpressionParser.lookahead(), aud.example.expr.Tokenizer.MINUS, aud.example.expr.ExpressionParser.nextToken(), aud.example.expr.Tokenizer.PLUS, aud.example.expr.ExpressionParser.product(), aud.example.expr.ExpressionParser.sum(), and aud.example.expr.ExpressionParser.verbose().
Referenced by aud.example.expr.ExpressionParser.expression(), and aud.example.expr.ExpressionParser.sum().
|
protected |
helper: print out information
Definition at line 120 of file ExpressionParser.java.
Referenced by aud.example.expr.ExpressionParser.expression(), aud.example.expr.ExpressionParser.factor(), aud.example.expr.ExpressionParser.product(), and aud.example.expr.ExpressionParser.sum().