-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSimpleYacc.y
158 lines (127 loc) · 4.28 KB
/
SimpleYacc.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
%{
public StListNode root;
public Parser(AbstractScanner<ValueType, LexLocation> scanner) : base(scanner) { }
private bool InDefSect = false;
%}
%output = SimpleYacc.cs
%union {
public bool bVal;
public int iVal;
public string sVal;
public Node nVal;
public ExprNode eVal;
public StatementNode stVal;
public StListNode blVal;
}
%using System.IO;
%using ProgramTree;
%namespace SimpleParser
%token BEGIN END ASSIGN SEMICOLON FOR COMMA COLON LPAR RPAR WHILE IF ELSE INPUT PRINT
VAR OR AND EQUAL NOTEQUAL LESS GREATER EQGREATER EQLESS GOTO PLUS MINUS MULT DIV NOT
%token <iVal> INUM
%token <bVal> BOOL
%token <sVal> ID
%type <eVal> expr ident A B C E T F exprlist
%type <stVal> assign statement for while if input print varlist var labelstatement goto block
%type <blVal> stlist progr
%%
progr : stlist { root = $1; }
;
stlist : statement { $$ = new StListNode($1); }
| stlist statement
{
$1.Add($2);
$$ = $1;
}
;
statement: assign SEMICOLON { $$ = $1; }
| for { $$ = $1; }
| while { $$ = $1; }
| if { $$ = $1; }
| block { $$ = $1; }
| input SEMICOLON { $$ = $1; }
| print SEMICOLON { $$ = $1; }
| var SEMICOLON { $$ = $1; }
| goto SEMICOLON { $$ = $1; }
| labelstatement { $$ = $1; }
;
ident : ID {
if (!InDefSect)
if (!SymbolTable.vars.ContainsKey($1))
throw new Exception("("[email protected]+","[email protected]+"): Variable "+$1+" not described");
$$ = new IdNode($1);
}
;
assign : ident ASSIGN expr { $$ = new AssignNode($1 as IdNode, $3); }
;
block : BEGIN stlist END { $$ = new BlockNode($2); }
;
for : FOR ident ASSIGN expr COMMA expr statement
{ $$ = new ForNode($2 as IdNode, $4, $6, $7); }
;
while : WHILE expr statement { $$ = new WhileNode($2, $3); }
;
if : IF expr statement ELSE statement { $$ = new IfElseNode($2, $3, $5); }
| IF expr statement { $$ = new IfElseNode($2, $3); }
;
expr : expr OR A { $$ = new BinOpNode($1, $3, OpType.OR); }
| A { $$ = $1; }
;
A : A AND B { $$ = new BinOpNode($1, $3, OpType.AND); }
| B { $$ = $1; }
;
B : B EQUAL C { $$ = new BinOpNode($1, $3, OpType.EQUAL); }
| B NOTEQUAL C { $$ = new BinOpNode($1, $3, OpType.NOTEQUAL); }
| C { $$ = $1; }
;
C : C GREATER E { $$ = new BinOpNode($1, $3, OpType.GREATER); }
| C LESS E { $$ = new BinOpNode($1, $3, OpType.LESS); }
| C EQGREATER E { $$ = new BinOpNode($1, $3, OpType.EQGREATER); }
| C EQLESS E { $$ = new BinOpNode($1, $3, OpType.EQLESS); }
| E { $$ = $1; }
;
E : E PLUS T { $$ = new BinOpNode($1, $3, OpType.PLUS); }
| E MINUS T { $$ = new BinOpNode($1, $3, OpType.MINUS); }
| T { $$ = $1; }
;
T : T MULT F { $$ = new BinOpNode($1, $3, OpType.MULT); }
| T DIV F { $$ = new BinOpNode($1, $3, OpType.DIV); }
| F { $$ = $1; }
;
F : ident { $$ = $1 as IdNode; }
| INUM { $$ = new IntNumNode($1); }
| LPAR expr RPAR { $$ = $2; }
| BOOL { $$ = new BoolValNode($1); }
| MINUS F { $$ = new UnOpNode($2, OpType.UNMINUS); }
| NOT F { $$ = new UnOpNode($2, OpType.NOT);}
;
input : INPUT LPAR ident RPAR { $$ = new InputNode($3 as IdNode); }
;
exprlist : expr { $$ = new ExprListNode($1); }
| exprlist COMMA expr
{
($1 as ExprListNode).Add($3);
$$ = $1;
}
;
print : PRINT LPAR exprlist RPAR { $$ = new PrintNode($3 as ExprListNode); }
;
varlist : ident { $$ = new VarListNode($1 as IdNode); }
| varlist COMMA ident
{
($1 as VarListNode).Add($3 as IdNode);
$$ = $1;
}
;
var : VAR { InDefSect = true; } varlist
{
foreach (var v in ($3 as VarListNode).vars)
SymbolTable.NewVarDef(v.Name);
InDefSect = false;
}
;
goto : GOTO INUM { $$ = new GotoNode($2); }
;
labelstatement : INUM COLON statement { $$ = new LabelStatementNode($1, $3); }
;
%%