-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJagger.jj
69 lines (62 loc) · 1.37 KB
/
Jagger.jj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Options for JavaCC.
options { LOOKAHEAD=1; FORCE_LA_CHECK=true; }
// Fonction principale
PARSER_BEGIN(Jagger)
public class Jagger
{
public static void main(String args[]) throws ParseException
{
Jagger parser = new Jagger(System.in);
parser.mainloop();
}
}
PARSER_END(Jagger)
// Characters to ignore.
SKIP: { " " | "\r" | "\t" }
// Token specifications.
TOKEN:
{
< NUMBER: (<DIGIT>)+ ("." (<DIGIT>)*)? > // A decimal number.
| < DIGIT: ["0"-"9"] > // A decimal digit.
| < EOL: "\n" > // End of line.
}
// Main lopp: read expressions on a line until end of file.
// mainloop → (expression <EOL>)* <EOF>
void mainloop():
{ double a; }
{
(
a=expression() <EOL> { System.out.println(a); }
)*
<EOF>
}
// Expression (the axiom).
// E -> T ('+'T | '-'T)*
double expression():
{ double a,b; }
{
a=term()
(
"+" b=expression() { a += b; }
| "-" b=expression() { a -= b; }
)? { return a; }
}
// Term.
// T -> F ('*'F | '/'F)*
double term():
{ double a,b; }
{
a=factor()
(
"*" b=factor() { a *= b; }
| "/" b=factor() { a /= b; }
)* { return a; }
}
// Factor of an expression.
// F -> <NUMBER> | "(" E ")"
double factor():
{ Token t; double e; }
{
t=<NUMBER> { return Double.parseDouble(t.toString()); }
| "(" e=expression() ")" { return e; }
}