forked from ThrostGunnulf/iJavaC
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathijparser.y
More file actions
201 lines (163 loc) · 6.62 KB
/
ijparser.y
File metadata and controls
201 lines (163 loc) · 6.62 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
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
199
200
201
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "astNodes.h"
#include "show.h"
#include "symbols.h"
#include "semantic.h"
#include "exitClean.h"
void checkFlags(int, char **, int*, int*);
extern int prevLineNo;
extern int prevColNo;
extern char *yytext;
int error = 0;
Class* myProgram = NULL;
ClassTable* symbolsTable = NULL;
%}
%union
{
char *token;
Type type;
struct _class *class;
DeclList *decllist;
VarDecl *vardecl;
MethodDecl *methoddecl;
ParamList *paramlist;
VarDeclList *vardecllist;
IDList *idlist;
StmtList *stmtlist;
Stmt *stmt;
Expr *expr;
ArgsList *argslist;
}
%token <token> INT BOOL NEW IF ELSE WHILE PRINT PARSEINT CLASS PUBLIC STATIC VOID STRING DOTLENGTH RETURN AND OR RELCOMPAR BOOLLIT ID INTLIT RESERVED EQUALITY ADDITIVE MULTIPLIC
%type <class> start
%type <decllist> decls
%type <vardecl> fielddecl
%type <methoddecl> methoddecl
%type <paramlist> formalparams formalparamslist
%type <vardecllist> vardecl
%type <idlist> idlist
%type <stmtlist> stmtlist
%type <stmt> statement
%type <expr> expr exprindex exprnotindex
%type <argslist> args argslist
%type <type> methodtype type
%left OR
%left AND
%left EQUALITY
%left RELCOMPAR
%left ADDITIVE
%left MULTIPLIC
%right UNARY
%left '[' DOTLENGTH
%nonassoc IFX
%nonassoc ELSE
%%
start: CLASS ID '{' decls '}' {$$=insertClass($2, $4); myProgram = $$;}
| CLASS ID '{' '}' {$$=insertClass($2, NULL); myProgram = $$;}
decls: decls fielddecl {$$=insertDecl(VARDECL, $2, $1);}
| decls methoddecl {$$=insertDecl(METHODDECL, $2, $1);}
| fielddecl {$$=insertDecl(VARDECL, $1, NULL);}
| methoddecl {$$=insertDecl(METHODDECL, $1, NULL);};
fielddecl: STATIC type ID idlist ';' {$$=insertFieldDecl($2, $3, $4);};
methoddecl: PUBLIC STATIC methodtype ID '(' formalparams ')' '{' vardecl stmtlist '}'
{$$=insertMethodDecl($3, $4, $6, $9, $10);};
methodtype: type {$$=$1;}
| VOID {$$=VOID_T;};
formalparams: type ID formalparamslist {$$=insertFormalParam($1, $2, $3, 1);}
| STRING '[' ']' ID {$$=insertFormalParam(STRINGARRAY, $4, NULL, 1);}
| {$$=NULL;};
formalparamslist: formalparamslist ',' type ID {$$=insertFormalParam($3, $4, $1, 0);}
| {$$=NULL;}
stmtlist: stmtlist statement {$$=insertStmtList($2, $1);}
| {$$=NULL;};
vardecl: vardecl type ID idlist ';' {$$=insertVarDecl($1, $2, $3, $4);}
| {$$=NULL;};
idlist: idlist ',' ID {$$=insertID($3, $1);}
| {$$=NULL;};
type: INT '[' ']' {$$=INTARRAY;}
| BOOL '[' ']' {$$=BOOLARRAY;}
| INT {$$=INT_T;}
| BOOL {$$=BOOL_T;};
statement: '{' stmtlist '}' {$$=insertStmt(CSTAT, NULL, NULL, NULL, NULL, NULL, $2);}
| IF '(' expr ')' statement ELSE statement %prec ELSE {$$=insertStmt(IFELSE, NULL, $3, NULL, $5, $7, NULL);}
| IF '(' expr ')' statement %prec IFX {$$=insertStmt(IFELSE, NULL, $3, NULL, $5, NULL, NULL);}
| WHILE '(' expr ')' statement {$$=insertStmt(WHILE_T, NULL, $3, NULL, $5, NULL, NULL);}
| PRINT '(' expr ')' ';' {$$=insertStmt(PRINT_T, NULL, $3, NULL, NULL, NULL, NULL);}
| ID '[' expr ']' '=' expr ';' {$$=insertStmt(STOREARRAY, $1, $3, $6, NULL, NULL, NULL);}
| ID '=' expr ';' {$$=insertStmt(STORE, $1, $3, NULL, NULL, NULL, NULL);}
| RETURN expr ';' {$$=insertStmt(RETURN_T, NULL, $2, NULL, NULL, NULL, NULL);}
| RETURN ';' {$$=insertStmt(RETURN_T, NULL, NULL, NULL, NULL, NULL, NULL);};
expr: exprindex {$$=$1;}
| exprnotindex {$$=$1;};
exprindex: ID {$$=insertExpr(ID_T, NULL, NULL, NULL, $1, NULL);}
| INTLIT {$$=insertExpr(INTLIT_T, NULL, NULL, NULL, $1, NULL);}
| BOOLLIT {$$=insertExpr(BOOLLIT_T, NULL, NULL, NULL, $1, NULL);}
| '(' expr ')' {$$=$2;}
| expr DOTLENGTH {$$=insertExpr(UNOP, "DOT", $1, NULL, NULL, NULL);}
| PARSEINT '(' ID '[' expr ']' ')' {$$=insertExpr(PARSEINT_T, $3, $5, NULL, $3, NULL);}
| ID '(' args ')' {$$=insertExpr(CALL, NULL, NULL, NULL, $1, $3);}
| ID '(' ')' {$$=insertExpr(CALL, NULL, NULL, NULL, $1, NULL);};
| exprindex '[' expr ']' {$$=insertExpr(INDEX, NULL, $1, $3, NULL, NULL);}
exprnotindex: NEW INT '[' expr ']' {$$=insertExpr(NEWINTARR, NULL, $4, NULL, NULL, NULL);}
| NEW BOOL '[' expr ']' {$$=insertExpr(NEWBOOLARR, NULL, $4, NULL, NULL, NULL);}
| '!' expr %prec UNARY {$$=insertExpr(UNOP, "!", $2, NULL, NULL, NULL);}
| ADDITIVE expr %prec UNARY {$$=insertExpr(UNOP, $1, $2, NULL, NULL, NULL);}
| expr AND expr {$$=insertExpr(BINOP, $2, $1, $3, NULL, NULL);}
| expr OR expr {$$=insertExpr(BINOP, $2, $1, $3, NULL, NULL);}
| expr RELCOMPAR expr {$$=insertExpr(BINOP, $2, $1, $3, NULL, NULL);}
| expr EQUALITY expr {$$=insertExpr(BINOP, $2, $1, $3, NULL, NULL);}
| expr ADDITIVE expr {$$=insertExpr(BINOP, $2, $1, $3, NULL, NULL);}
| expr MULTIPLIC expr {$$=insertExpr(BINOP, $2, $1, $3, NULL, NULL);};
args: expr argslist {$$=insertArg($1, $2);}
| expr {$$=insertArg($1, NULL);};
argslist: ',' args {$$=$2;};
%%
int main(int argc, char *argv[])
{
int printTree, printSymbols;
yyparse();
if(error)
exitClean(0);
checkFlags(argc, argv, &printTree, &printSymbols);
if(printTree)
printProgram(myProgram);
if(!printTree || printSymbols)
{
symbolsTable = buildSymbolsTables(myProgram);
checkSemantics(myProgram);
}
if(printSymbols)
printSymbolTables(symbolsTable);
//freeProgram(myProgram, symbolsTable); //TO IMPLEMENT
return 0;
}
void checkFlags(int argc, char *argv[], int* printTree, int* printSymbols)
{
int i;
*printTree = *printSymbols = 0;
for(i=0; i < argc; i++)
{
if(strcmp(argv[i], "-s") == 0)
{
*printSymbols = 1;
}
else if(strcmp(argv[i], "-t") == 0)
{
*printTree = 1;
}
}
}
void exitClean(int exitValue)
{
//freeProgram(myProgram, symbolsTable); //TO IMPLEMENT
exit(0);
}
int yyerror(char *s)
{
printf("Line %d, col %d: %s: %s\n", prevLineNo, prevColNo, s, yytext);
error=1;
return -1;
}