-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cup
184 lines (149 loc) · 4.34 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
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,LANGLB,RANGLB;
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 , return_type , funtion_decl , general_decl , formal_parameter_list , funtion_body , body , type , assignment , input , output , function_call , control_flow ,len,dict,seq;
startwith program;
program ::= declaration_list main declaration_list
;
main ::= MAIN LBRACE function_body RBRACE (empty| COLON type)
;
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 (ASSOP expr | empty) SEMI
;
data_type_decl ::= TDEF ID LBRACE tdef_list RBRACE
| alias ID type
;
tdef_list ::= var_decl tdef_helper
;
tdef_helper ::= COMMA var_decl tdef_helper
| empty
;
function_decl ::= general_function (empty| COLON type)
;
general_function ::= FDEF ID LPAREN formal_parameter_list RPAREN LBRACE function_body RBRACE
;
formal_parameter_list ::= var_decl formal_parameter_list_helper
| empty
;
formal_parameter_list_helper ::= COMMA var_decl formal_parameter_list_helper
| empty
;
declaration_list ::= program_decl declaration_list
| empty
;
function_body ::= body
;
body ::= declaration_list statement statement_list
;
statement_list ::= statement statement_list
| empty
;
type ::= INT | RAT | FLOAT | BOOL | CHAR | seq | dict | TOP | ID
;
assignment ::= ID ASSOP expr_list
;
input ::= READ ID
;
output ::= PRINT expr
;
function_call ::= ID LPAREN expr_list 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 interable RPAREN DO body OD
| RETURN expr SEMI
;
operator ::= boolean_op | numeric_op | dictionary_op | sequence_op | comparison_op
;
binary_operator ::= PLUS|MINUS|MULT|DIV|EXP|AND|OR|IMPL|CONCAT|LT|LTEQ|GT|GTEQ|EQ|NOTEQ|IN
;
unary_operator ::= MINUS|NOT
;
boolean_op ::= NOT | AND | OR | IMPL
;
numeric_op ::= PLUS | MINUS | MULT | DIV | EXP
;
dictionary_op ::= IN | ID LSQB expr RSQB | len
;
secquence_op ::= IN
| CONCAT
| len
| ID LSQB expr RSQB
| ID LSQB (expr COLON expr | expr COLON | COLON expr) RSQB
;
comparison_op ::= LT
| LTEQ
| GT
| GTEQ
| EQ
| NOTEQ
;
len ::= LEN LPAREN ID RPAREN
;
field_ref ::= ID DOT ID
;
expr ::= expr_part binary_operator expr_part
| expr_part
;
expr_part ::= function_call
| assignment_type
| len expr
| ID LSQB expr RSQB
| ID LSQB (expr|empty)COLON(empty|expr) RSQB
| field_ref
| ID
| unary_operator expr
| dict_assignment
| seq_assignment
| LPAREN expr RPAREN
;
dict_assignment ::= LBRACE list_dict_elements RBRACE
;
list_dict_elements ::= dict_element list_dict_elements_helper
| empty
;
list_dict_elements_helper ::= COMMA dict_element list_dict_elements_helper
| empty
;
dict_element ::= (assignment_type|ID) COLON (assignment_type|ID)
;
seq_assignment ::= LSQB list_seq_assignment_type RSQB
;
list_seq_assignment ::= seq_element list_seq_assignment_helper
| empty
;
list_seq_assignment_helper ::= COMMA seq_element list_seq_assignment_helper
| empty
;
seq_element ::= assignment_type|ID
;
expr_list ::= expr expr_helper
;
expr_helper ::= COMMA expr expr_helper
| empty
;
number ::= (MINUS|empty)(INT_V | FLOAT_V | RAT_V)
;
primitive ::= numbers | BOOL_V | CHAR_V
;
aggregated ::= seq_assignment | dict_assignment
;
assignment_type ::= primitive | aggregated
;
dict ::= DICT LANGLB type COMMA type RANGLB
;
seq ::= SEQ LANGLB type RANGLB
;