2package aud.example.expr;
64 boolean verbose_ =
false;
98 throw new SyntaxError(
"unknown token: lexcial analysis failed at '"+
112 protected void expect(
int tokenid,String text) {
120 protected void verbose(
int level,String message) {
122 System.err.println(
Sys.
indent(level)+message+
237 public static void main(String[] args) {
239 String input=(args.length>0) ? args[0] :
"1+2*(3+4)";
241 System.out.println(
"input = '"+input+
"'");
247 System.out.println(
"output = '"+tree+
"'");
251 System.out.println(tree.
toTikZ());
BinaryTreeTraversal< T >.Postorder postorder()
Get postorder iterator over nodes in tree .
String toTikZ()
get TikZ code for LaTeX export
Simple expression parser.
ExpressionTree product(int level)
parse product
String errorNear()
helper: generate a simple error message
ExpressionTree parse(String input)
parse input
ExpressionTree factor(int level)
parse factor
void setVerbose(boolean b)
set verbose mode (report state to System.err)
int lookahead()
helper: "lookahead" is the usual phrasing
void expect(int tokenid, String text)
helper: check token (without calling nextToken!)
static void main(String[] args)
test and example for usage
ExpressionTree expression(int level)
parse expression
ExpressionTree sum(int level)
parse sum
void verbose(int level, String message)
helper: print out information
String matchedText()
helper: get match corresponding to lookahead
void nextToken()
helper: consume current token and advance to next token
Tree representation of arithmetic expression.
Node representing constant number.
Node representing a symbolic parameter, e.g., a varibale.
signals syntax error during parsing a term
Breaks input string into pieces ("tokens").
static final int RIGHT_PAREN
static final int LEFT_PAREN
static final int IDENTIFIER
unary - operator: -A ("sign")
static final int END_OF_INPUT
no more input
int next(Rule[] rules)
match remainder to table of rules @endiliteral
int matchedTokenId()
get result of last call to next()
String matchedText()
get text of last match or call to next
String remainder()
get remaining text
static final int NO_MATCH
no match (usually implies a syntax error)
System related utilities.
static String indent(int level)
get indentation string filled with spaces
utilities (not related to AuD lecture)
AuD lecture: Data structures, algorithms, examples.