-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathcalc.java.g
61 lines (53 loc) · 1.29 KB
/
calc.java.g
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
/**
* Generated parser in Java.
*
* ./bin/syntax -g examples/calc.java.g -m lalr1 -o CalcParser.rs
*
* import com.syntax.*;
*
* CalcParser parser = new CalcParser();
*
* System.out.println(parser.parse("2 + 2 * 2"); // 6
* System.out.println(parser.parse("(2 + 2) * 2"); // 8
*/
{
"lex": {
"rules": [
["\\s+", '/* skip whitespace */ return null'],
["\\d+", 'return "NUMBER"'],
["\\*", 'return "*"'],
["\\+", 'return "+"'],
["\\(", 'return "("'],
["\\)", 'return ")"'],
]
},
"operators": [
["left", "+"],
["left", "*"],
],
"moduleInclude": `
/**
* The ParserEvents class allows subscribing to
* different parsing events.
*/
class ParserEvents {
public static void init() {
System.out.println("Parser is created.");
}
public static void onParseBegin(String str) {
System.out.println("Parsing is started: " + str);
}
public static void onParseEnd(Object result) {
System.out.println("Parsing is completed: " + result);
}
}
`,
"bnf": {
"E": [
["E + E", "$$ = (Integer)$1 + (Integer)$3"],
["E * E", "$$ = (Integer)$1 * (Integer)$3"],
["NUMBER", "$$ = Integer.valueOf(yytext)"],
["( E )", "$$ = $2"],
],
},
}