Package aud.example.expr
Class ExpressionParser
java.lang.Object
aud.example.expr.ExpressionParser
- Direct Known Subclasses:
ExpressionParser2
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(int)
. - The
level
argument is provided only for formattingverbose(int, java.lang.String)
output. - The parser uses a
Tokenizer
for lexcical scanning. - The current token is used for "looking ahead" (
lookahead()
): it is advanced (bynextToken()
only if this token is "consumed" by part of a BNF rule. - The given grammar defines expressions right-associative, which
is not quite correct:
a+b+c
should equal(a+b)+c
(left associative) rather thana+(b+c)
!
A simple change of the grammar however leads to an infinite recursion (due to left recursion in the new grammar.
This can be fixed easily by a transformation of the grammar. Also, the practical implementation is easy (use an iteration). However, both is boyond the scope of the lecture at this point. For a correct implementation seeExpressionParser2
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected String
helper: generate a simple error messageprotected void
helper: check token (without callingnextToken()
!)protected ExpressionTree
expression
(int level) parse expressionprotected ExpressionTree
factor
(int level) parse factorprotected int
helper: "lookahead" is the usual phrasingstatic void
test and example for usageprotected String
helper: get match corresponding tolookahead()
protected void
helper: consume current token and advance to next tokenparse inputprotected ExpressionTree
product
(int level) parse productvoid
setVerbose
(boolean b) set verbose mode (report state toSystem.err
)protected ExpressionTree
sum
(int level) parse sumprotected void
helper: print out information
-
Constructor Details
-
ExpressionParser
public ExpressionParser()
-
-
Method Details
-
parse
parse input- Parameters:
input
- term- Returns:
- extression tree that represents input term
- Throws:
SyntaxError
- on syntax error
-
setVerbose
public void setVerbose(boolean b) set verbose mode (report state toSystem.err
) -
errorNear
helper: generate a simple error message -
lookahead
protected int lookahead()helper: "lookahead" is the usual phrasing -
nextToken
protected void nextToken()helper: consume current token and advance to next token -
matchedText
helper: get match corresponding tolookahead()
- Returns:
LexicalScanner.matchedText()
-
expect
helper: check token (without callingnextToken()
!)- Throws:
SyntaxError
- if token does not match
-
verbose
helper: print out information -
expression
parse expressionexpression ::= sum
-
sum
parse sumsum ::= product | product '+' sum | product '-' sum
-
product
parse productproduct ::= factor | factor '*' product | factor '/' product
-
factor
parse factor
The last part of the rule correspong to unary plus and minus operatorsfactor ::= number | symbol | '(' expression ')' | '+' factor | '-' factor
+x
and-x
. The implementation ignores unary plus, i.e.,+x -> x
. -
main
test and example for usage
-