-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
97 lines (78 loc) · 2.55 KB
/
Copy pathparser.y
File metadata and controls
97 lines (78 loc) · 2.55 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
%{
package main
import "fmt"
%}
%union {
v string /* Variable */
s string /* String */
num int /* Integer constant */
dec float64 /* Decimal constant */
node Node /* Node in the AST */
};
%token <num> INTEGER
%token <s> STRING
%token <v> VARIABLE
%token <dec> DECIMAL
%token PRINT IF GOTO LET END THEN CR
%left LT LE GT GE EQ NE
%left '+' '-'
%left '*' '/'
%type <node> line statement expression term factor number v s
%type <node> relop expr_list
%%
program:
block {}
;
block:
block line {}
| line {}
;
line:
INTEGER statement CR { ex($2,$1); }
;
statement:
PRINT expr_list { $$ = opr(PRINT, 1, $2); }
| IF expression relop expression THEN statement { $$ = opr(IF, 4, $2, $3, $4, $6); }
| GOTO expression { $$ = opr(GOTO, 1, $2); }
| LET v '=' expression { $$ = opr(LET, 2, $2, $4); }
| END { $$ = opr(END, 0); }
;
expr_list:
expr_list ',' expression { $$ = opr('l', 2, $1, $3); }
| expression { $$ = $1; }
;
expression:
expression '+' term { $$ = opr('+', 2, $1, $3); }
| expression '-' term { $$ = opr('-', 2, $1, $3); }
| term { $$ = $1; }
| s { $$ = $1; }
;
term:
term '*' factor { $$ = opr('*', 2, $1, $3); }
| term '/' factor { $$ = opr('/', 2, $1, $3); }
| factor { $$ = $1; }
;
factor:
v { $$ = $1; }
| number { $$ = $1; }
| '(' expression ')' { $$ = opr('(', 1, $2); }
;
number:
INTEGER { $$ = Op{INTEGER, fmt.Sprintf("%d", $1)}; }
| DECIMAL { $$ = Op{DECIMAL, fmt.Sprintf("%f", $1)}; }
;
v:
VARIABLE { $$ = VarOp{VARIABLE, $1}; }
;
s:
STRING { $$ = StringOp{STRING, $1};}
;
relop:
LT { $$ = RelOp{LT}; }
| LE { $$ = RelOp{LE}; }
| GT { $$ = RelOp{GT}; }
| GE { $$ = RelOp{GE}; }
| EQ { $$ = RelOp{EQ}; }
| NE { $$ = RelOp{NE}; }
;
%%