-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cup
191 lines (166 loc) · 5.58 KB
/
Parser.cup
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java_cup.runtime.*;
parser code {:
boolean syntaxErrors = false;
public void syntax_error(Symbol current_token) {
syntaxErrors = true;
report_error("Syntax error at line " + (current_token.left+1) + ", column "+ current_token.right, null);
}
:};
terminal ID , BOOL , INT , RAT , FLOAT , CHAR , SEMI , MAIN , COMMA , TDEF , FDEF , IF , THEN , ELSE , RBRACE , LBRACE , RPAREN , LPAREN , COLON , ASSOP , READ , PRINT , FI, IN , DO , OD , WHILE , FORALL , RETURN, TOP , LEN, DOT, SEQ, DICT,PLUS,MINUS,DIV,MULT,EXP,AND,OR,IMPL,CONCAT,LT,LTEQ,GT,GTEQ,EQ,NOTEQ,NOT,LSQB,RSQB,STRING,ALIAS,GARBAGE;
terminal Integer INT_V;
terminal Float FLOAT_V;
terminal String RAT_V;
terminal Char CHAR_V, BOOL_V;
non terminal program , main , statement , program_decl , var_decl , data_type_decl ,
declaration_list, formal_parameter_list , body , type , assignment , input , output , function_call , control_flow ,len,dict,seq, function_body,function_decl,expr,expr_part,tdef_list,tdef_helper,general_function,formal_parameter_list_helper,statement_list,expr_list,item,iterable,seq_assignment,binary_operator,unary_operator,assignment_type,dict_assignment,return_stmt,var_decl_empty,function_decl_empty,dict_element,seq_element,primitive,expr_next,expr_or_empty,id_followed_by,index,slicing,aggregate,dict_element_list,dict_element_list_tail,seq_element_list,seq_element_list_tail,expr_list_tail,declaration_list_tail,statement_list_tail,body_followup1,declaration_list_or_empty,slicing_or_empty,function_call_follow_up,formal_parameter_decl;
precedence left SEMI;
precedence left IF, READ, PRINT, WHILE, FORALL,RETURN;
precedence left LT,LTEQ,GT,GTEQ,EQ,NOTEQ;
precedence left IMPL,COMMA,DO;
precedence left PLUS, MINUS, OR;
precedence left MULT, DIV, LEN, AND, CONCAT;
precedence left NOT;
precedence left EXP;
precedence left IN;
precedence left DOT,LBRACE,RBRACE,LPAREN,RPAREN,COLON,LSQB,RSQB;
precedence left ASSOP;
precedence left STRING,INT_V,CHAR_V,RAT_V,FLOAT_V,BOOL_V,OD,FI,ELSE;
start with program;
program ::= declaration_list_or_empty main declaration_list_or_empty
;
declaration_list_or_empty ::= declaration_list
|
;
main ::= MAIN LBRACE function_body RBRACE SEMI
;
return_stmt ::= COLON type SEMI|SEMI;
statement ::= assignment SEMI
| input SEMI
| output SEMI
| function_call SEMI
| control_flow
;
program_decl ::= var_decl SEMI
| function_decl SEMI
| data_type_decl SEMI
;
var_decl ::= ID COLON type var_decl_empty
;
var_decl_empty ::= ASSOP expr_list
|
;
data_type_decl ::= TDEF ID LBRACE tdef_list RBRACE
| ALIAS type ID
;
tdef_list ::= var_decl tdef_helper
|
;
tdef_helper ::= COMMA var_decl tdef_helper
|
;
function_decl ::= general_function function_decl_empty
;
function_decl_empty ::= COLON type
|
;
general_function ::= FDEF ID LPAREN formal_parameter_list RPAREN LBRACE function_body RBRACE
;
formal_parameter_list ::= formal_parameter_decl formal_parameter_list_helper
|
;
formal_parameter_decl ::= ID COLON type;
formal_parameter_list_helper ::= COMMA formal_parameter_decl formal_parameter_list_helper
|
;
declaration_list ::= program_decl | declaration_list program_decl;
function_body ::= body
;
body ::= declaration_list body_followup1
| statement_list
;
body_followup1 ::= statement_list
|
;
statement_list ::= statement statement_list_tail;
statement_list_tail ::= statement statement_list_tail
|
;
type ::= INT | RAT | FLOAT | BOOL | CHAR | seq | dict | TOP | ID
;
assignment ::= expr ASSOP expr_list
;
input ::= READ ID
;
output ::= PRINT expr
;
function_call ::= ID LPAREN function_call_follow_up;
function_call_follow_up ::= expr_list RPAREN| RPAREN;
control_flow ::= IF LPAREN expr RPAREN THEN body FI
| IF LPAREN expr RPAREN THEN body ELSE body FI
| WHILE LPAREN expr RPAREN DO body OD
| FORALL LPAREN item IN iterable RPAREN DO body OD
| RETURN expr_or_empty SEMI
;
expr_or_empty ::= expr
|
;
item ::= ID;
iterable ::= ID|seq_assignment
;
binary_operator ::= PLUS|MINUS|MULT|DIV|EXP|AND|OR|IMPL|CONCAT|LT|LTEQ|EQ|NOTEQ|IN
;
unary_operator ::= MINUS|NOT
;
len ::= LEN LPAREN expr RPAREN
;
expr_list ::= expr expr_list_tail;
expr_list_tail ::= COMMA expr expr_list_tail
|
;
expr ::= expr_part expr_next
;
expr_next ::= binary_operator expr
|
;
expr_part ::= ID id_followed_by
| assignment_type
| unary_operator expr
| LPAREN expr RPAREN
| len
;
id_followed_by ::= LSQB index RSQB id_followed_by
| DOT expr_part
| LPAREN function_call_follow_up
|
;
index ::= expr slicing_or_empty
| slicing
;
slicing ::= COLON expr_or_empty;
slicing_or_empty ::= slicing | ;
assignment_type ::= primitive | aggregate;
primitive ::= INT_V | RAT_V | FLOAT_V | CHAR_V | BOOL_V;
aggregate ::= dict_assignment|seq_assignment;
dict_assignment ::= LBRACE dict_element_list RBRACE;
dict_element_list ::= dict_element dict_element_list_tail
|
;
dict_element_list_tail ::= COMMA dict_element dict_element_list_tail
|
;
dict_element ::= expr COLON expr
;
seq_assignment ::= LSQB seq_element_list RSQB
| STRING
;
seq_element_list ::= seq_element seq_element_list_tail;
seq_element_list_tail ::= COMMA seq_element seq_element_list_tail
|
;
seq_element ::= expr
|
;
dict ::= DICT LT type COMMA type GT
;
seq ::= SEQ LT type GT
;