-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathcalc.cs.g
More file actions
39 lines (36 loc) · 774 Bytes
/
calc.cs.g
File metadata and controls
39 lines (36 loc) · 774 Bytes
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
/**
* Generated parser in C#.
*
* ./bin/syntax -g examples/calc.cs.g -m lalr1 -o CalcParser.cs
*
* using SyntaxParser;
*
* var parser = new CalcParser();
*
* Console.WriteLine(parser.parse("2 + 2 * 2")); // 6
* Console.WriteLine(parser.parse("(2 + 2) * 2")); // 8
*/
{
"lex": {
"rules": [
["\\s+", '/* skip whitespace */ return null'],
["\\d+", 'return "NUMBER"'],
["\\*", 'return "*"'],
["\\+", 'return "+"'],
["\\(", 'return "("'],
["\\)", 'return ")"'],
]
},
"operators": [
["left", "+"],
["left", "*"],
],
"bnf": {
"E": [
["E + E", "$$ = $1 + $3"],
["E * E", "$$ = $1 * $3"],
["NUMBER", "$$ = Convert.ToInt32($1)"],
["( E )", "$$ = $2"],
],
},
}