-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathparser.y
198 lines (142 loc) · 4.42 KB
/
parser.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
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
192
193
194
195
196
197
198
%{
#include "semantics.c"
#include "symtab.c"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern FILE *yyin;
extern FILE *yyout;
extern int lineno;
extern int yylex();
void yyerror();
%}
/* YYSTYPE union */
%union{
char char_val;
int int_val;
double double_val;
char* str_val;
list_t* symtab_item;
}
/* token definition */
%token<int_val> CHAR INT FLOAT DOUBLE IF ELSE WHILE FOR CONTINUE BREAK VOID RETURN
%token<int_val> ADDOP MULOP DIVOP INCR OROP ANDOP NOTOP EQUOP RELOP
%token<int_val> LPAREN RPAREN LBRACK RBRACK LBRACE RBRACE SEMI DOT COMMA ASSIGN REFER
%token <symtab_item> ID
%token <int_val> ICONST
%token <double_val> FCONST
%token <char_val> CCONST
%token <str_val> STRING
/* precedencies and associativities */
%left LPAREN RPAREN LBRACK RBRACK
%right NOTOP INCR REFER
%left MULOP DIVOP
%left ADDOP
%left RELOP
%left EQUOP
%left OROP
%left ANDOP
%right ASSIGN
%left COMMA
%start program
%%
program: declarations statements RETURN SEMI functions_optional ;
/* declarations */
declarations: declarations declaration | declaration ;
declaration: { declare = 1; } type names { declare = 0; } SEMI ;
type: INT | CHAR | FLOAT | DOUBLE | VOID ;
names: names COMMA variable | names COMMA init | variable | init ;
variable: ID |
pointer ID |
ID array
;
pointer: pointer MULOP | MULOP ;
array: array LBRACK expression RBRACK | LBRACK expression RBRACK ;
init: var_init | array_init ;
var_init : ID ASSIGN constant ;
array_init: ID array ASSIGN LBRACE values RBRACE ;
values: values COMMA constant | constant ;
/* statements */
statements: statements statement | statement ;
statement:
if_statement | for_statement | while_statement | assigment SEMI |
CONTINUE SEMI | BREAK SEMI | function_call SEMI | ID INCR SEMI | INCR ID SEMI
;
if_statement:
IF LPAREN expression RPAREN tail else_if optional_else |
IF LPAREN expression RPAREN tail optional_else
;
else_if:
else_if ELSE IF LPAREN expression RPAREN tail |
ELSE IF LPAREN expression RPAREN tail
;
optional_else: ELSE tail | /* empty */ ;
for_statement: FOR LPAREN assigment SEMI expression SEMI expression RPAREN tail ;
while_statement: WHILE LPAREN expression RPAREN tail ;
tail: LBRACE statements RBRACE ;
expression:
expression ADDOP expression |
expression MULOP expression |
expression DIVOP expression |
ID INCR |
INCR ID |
expression OROP expression |
expression ANDOP expression |
NOTOP expression |
expression EQUOP expression |
expression RELOP expression |
LPAREN expression RPAREN |
var_ref |
sign constant |
function_call
;
sign: ADDOP | /* empty */ ;
constant: ICONST | FCONST | CCONST ;
assigment: var_ref ASSIGN expression ;
var_ref: variable | REFER variable ;
function_call: ID LPAREN call_params RPAREN ;
call_params: call_param | STRING | /* empty */
call_param: call_param COMMA expression | expression ;
/* functions */
functions_optional: functions | /* empty */ ;
functions: functions function | function ;
function: { incr_scope(); } function_head function_tail { hide_scope(); } ;
function_head: { declare = 1; } return_type ID LPAREN { declare = 0; } parameters_optional RPAREN ;
return_type: type | type pointer ;
parameters_optional: parameters | /* empty */ ;
parameters: parameters COMMA parameter | parameter ;
parameter : { declare = 1; } type variable { declare = 0; } ;
function_tail: LBRACE declarations_optional statements_optional return_optional RBRACE ;
declarations_optional: declarations | /* empty */ ;
statements_optional: statements | /* empty */ ;
return_optional: RETURN expression SEMI | /* empty */ ;
%%
void yyerror ()
{
fprintf(stderr, "Syntax error at line %d\n", lineno);
exit(1);
}
int main (int argc, char *argv[]){
// initialize symbol table
init_hash_table();
// initialize revisit queue
queue = NULL;
// parsing
int flag;
yyin = fopen(argv[1], "r");
flag = yyparse();
fclose(yyin);
printf("Parsing finished!\n");
if(queue != NULL){
printf("Warning: Something has not been checked in the revisit queue!\n");
}
// symbol table dump
yyout = fopen("symtab_dump.out", "w");
symtab_dump(yyout);
fclose(yyout);
// revisit queue dump
yyout = fopen("revisit_dump.out", "w");
revisit_dump(yyout);
fclose(yyout);
return flag;
}