1package aud.example.expr;
5import java.util.regex.Pattern;
11 protected static final Pattern
P_PLUS = Pattern.compile(
"\\+");
12 protected static final Pattern
P_MINUS = Pattern.compile(
"-");
13 protected static final Pattern
P_TIMES = Pattern.compile(
"\\*");
14 protected static final Pattern
P_DIVIDE = Pattern.compile(
"/");
15 protected static final Pattern
P_POWER = Pattern.compile(
"(\\*\\*)|\\^");
16 protected static final Pattern
P_LEFTPAREN = Pattern.compile(
"\\(");
17 protected static final Pattern
P_RIGHTPAREN = Pattern.compile(
"\\)");
19 public static final int PLUS=
'+';
20 public static final int MINUS=
'-';
21 public static final int TIMES=
'*';
23 public static final int POWER=
'^';
28 public static final int NUMBER=0x101;
49 public static void main(String[] args) {
52 (args.length==0 ?
"2*(3+4)/x-3.14+2^3-2**3" : args[0]);
54 System.out.println(
"input = '"+t.
remainder()+
"'");
58 System.out.println(
"syntax error near '"+t.
remainder()+
"'");
63 System.out.println(
"read a number: "+d);
66 System.out.println(
"matched text = '"+t.
matchedText()+
"'");
67 System.out.println(
"remaining input = '"+t.
remainder()+
"'");
Breaks input string into pieces ("tokens").
static final Pattern P_POWER
static final Pattern P_TIMES
static final int RIGHT_PAREN
static final Pattern P_RIGHTPAREN
static final LexicalScanner.Rule[] RULES
static final int LEFT_PAREN
static final Pattern P_PLUS
static final Pattern P_DIVIDE
static final int IDENTIFIER
static final Pattern P_MINUS
static void main(String[] args)
testing and example for usage
Tokenizer(String input)
create new tokenizer for input
static final Pattern P_LEFTPAREN
Base class for a simple lexical scanner.
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)
static final Pattern P_IDENTIFIER
identifiers
static final Pattern P_FLOAT
floating point number
utilities (not related to AuD lecture)
AuD lecture: Data structures, algorithms, examples.