-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsilly.y
46 lines (31 loc) · 772 Bytes
/
silly.y
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
%{
package main
%}
%union {
val int
ident string
node node
list *stmtList
}
%token '{' '}' ';' '=' '+' '/' '-' '*' '(' ')' '#'
%token <val> VAL
%token <ident> IDENT
%type <node> expr stmt
%type <list> stmtList
%%
top: expr { r = $1 }
// ignoring order of operations... there will be s/r conflict & ambiguous output
expr: VAL { $$ = &intNode{$1} }
| expr '+' expr { $$ = &binopNode{"+", $1, $3} }
| expr '-' expr { $$ = &binopNode{"-", $1, $3} }
| expr '/' expr { $$ = &binopNode{"/", $1, $3} }
| expr '*' expr { $$ = &binopNode{"*", $1, $3} }
| IDENT '=' expr { $$ = &assignNode{$1, $3} }
| IDENT { $$ = &identNode{$1} }
| '{' stmtList '}' { $$ = $2; }
stmtList:
{ $$ = nil }
| stmt stmtList { $$ = &stmtList{$1, $2} }
stmt:
expr ';' { $$ = $1 }
%%