-
Notifications
You must be signed in to change notification settings - Fork 0
/
yacc.y
240 lines (192 loc) · 11.7 KB
/
yacc.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
%{
#include<stdio.h>
#include<stdlib.h>
#include "symbolTable.h"
#include<limits.h>
#include<ctype.h>
#include<string.h>
extern int yylineno;
extern char* yytext;
entry **SymbolTable = NULL;
entry **ConstantTable=NULL;
mainsymtable symbolTableList[MAXSCOPE];
int yylex(void);
int yyerror(char *msg);
char* curDataType;
char* functionDataType;
int LOOP=0,DECLARATION=0,FUNCTION=0,CONSTANT=0,RHS=0,RETURNSET=0;
char *parameterList[10];
char *argumentList[10];
int pListIndex=0,aListIndex=0;
int functionScope=0;
%}
%union{
entry *tbEntry;
char* datatype;
}
%token ADD SUB MUL DIV ASSIGN PLUSEQ MINUSEQ MULEQ DIVEQ MOD
%token GT LT LET GET NEQ EQ
%token VOID IF ELSE FOR DO WHILE BREAK CONTINUE RETURN STATIC STRUCT PRINT SCAN
%token INT SHORT LONG CHAR FLOAT SIGNED UNSIGNED
%token OR AND NOT
%token DECR INCR HEADER
%token <tbEntry> HEXCONST DECCONST INTCONST IDENTIFIER
%type <datatype> arrayAssignment
%type <datatype> expressionType
%type <datatype> assignmentExpression
%type <datatype> arithmeticExpression %type <datatype> postPreExpression
%type <datatype> idOrArray
%type <datatype> functionCall
%type <tbEntry> arrayIndex
%type <tbEntry> constantType
%type <tbEntry> identifier
%%
program : HEADER start|start;
start :start declaration|declaration;
declaration : variable|function;
function : typeDeclaration identifier {functionDataType=curDataType; DECLARATION=1;currentscope=createscope(); } '('parameterList')' {DECLARATION = 0;
fillParameterList($2,parameterList,pListIndex);
pListIndex = 0;
checkAndSetFunction(SymbolTable,$2->idName);
FUNCTION=1;
functionScope=1;} blockOfStmts{if(strcmp(functionDataType,"VOID")!=0)
{
if(!RETURNSET)
yyerror("return statement not matched");
}FUNCTION=0;RETURNSET=0;};
typeDeclaration : typeSpecifier pointer{DECLARATION=1;}|typeSpecifier{DECLARATION=1;};
typeSpecifier : signSpecifier dataType|dataType;
signSpecifier : SIGNED|UNSIGNED|STATIC;
dataType : INT {curDataType="INT"; DECLARATION=1;}|SHORT {curDataType="SHORT"; DECLARATION=1;}|LONG {curDataType="LONG";DECLARATION =1;}|
VOID {curDataType="VOID"; DECLARATION=1; }|CHAR {curDataType = "CHAR"; DECLARATION = 1;}|FLOAT {curDataType = "FLOAT"; DECLARATION = 1;};
parameterList : parameters| ;
parameters : param|parameters ',' param;
param : typeSpecifier identifier{parameterList[pListIndex] = (char *)malloc(sizeof(curDataType));
strcpy(parameterList[pListIndex++],$2->datatype);
DECLARATION = 0;};
functionCall : identifier '(' args ')' {
if(FunctionCheck(SymbolTable,$1->idName)==0){yyerror("Function error");}; $$ = $1->datatype;
checkParameterList($1,argumentList,aListIndex);
aListIndex = 0;};
args : argList | ;
argList : argList ',' arg| arg ;
arg : expressionType {
argumentList[aListIndex] = (char *)malloc(sizeof($1));
strcpy(argumentList[aListIndex++],$1);
};
statementType : blockOfStmts|singleStmt;
blockOfStmts : '{'{if(!functionScope)
currentscope=createscope();
else
functionScope=0;
}
statements
'}'{currentscope=exitthescope();};
statements : statements statementType | ;
singleStmt : variable|whileLoop|doWhileLoop|ifStmt|forLoop|keywordStmts|PRINT';'|SCAN';';
variable : typeDeclaration varDeclList';'{DECLARATION = 0;}|varDeclList';'|postPreExpression';' ;
varDeclList : varDeclList ',' varDeclInitialize | varDeclInitialize;
varDeclInitialize : assignmentExpression|arrayAssignment|arithmeticExpression ;
keywordStmts :BREAK';'{if(!LOOP) yyerror("Break statement invalid");exit(0);}|CONTINUE';'{if(!LOOP) yyerror("Continue statement invalid");exit(0);}|
RETURN';'{if(FUNCTION)
{if(functionDataType!="void")
yyerror("return statement not matched");
}
}
|RETURN expressionType';'{RETURNSET=1;if(FUNCTION)
{if(strcmp(functionDataType,$2)!=0)
{yyerror("return statement not matched");}
}
else{yyerror("return statement invalid");}};
whileLoop : WHILE'('conditionExpression')'{LOOP=1;} statementType{LOOP=0;};
doWhileLoop : DO whileLoop;
ifStmt : IF'('conditionExpression')'statementType|IF'('conditionExpression')'statementType ELSE statementType;
forLoop : FOR'('conditionExpression';'conditionExpression';'conditionExpression')'{LOOP=1;} statementType{LOOP=0;};
conditionExpression : conditionExpression','expressionType|expressionType|';';
expressionType: expressionType GT expressionType{$$=$1;DataTypeCheck($1,$3);}| expressionType LT expressionType{$$=$1;DataTypeCheck($1,$3);}| expressionType GET expressionType{$$=$1;DataTypeCheck($1,$3);}|
expressionType LET expressionType{$$=$1;DataTypeCheck($1,$3);}|expressionType NEQ expressionType{$$=$1;DataTypeCheck($1,$3);}|expressionType EQ expressionType{$$=$1;DataTypeCheck($1,$3);}|
expressionType AND expressionType{$$=$1;DataTypeCheck($1,$3);}|expressionType OR expressionType{$$=$1;DataTypeCheck($1,$3);}|arithmeticExpression{$$=$1;}|assignmentExpression{$$=$1;}|
postPreExpression{$$=$1;};
assignmentExpression : idOrArray assignmentOperators arithmeticExpression{DataTypeCheck($1,$3);RHS=0;$$=$3;}|idOrArray assignmentOperators arrayAssignment{DataTypeCheck($1,$3);RHS=0;$$=$3;}|
idOrArray assignmentOperators functionCall{DataTypeCheck($1,$3);RHS=0;$$=$3;}|idOrArray assignmentOperators postPreExpression{DataTypeCheck($1,$3);RHS=0;$$=$3;};|
postPreExpression assignmentOperators postPreExpression{DataTypeCheck($1,$3);RHS=0;$$=$3;};
idOrArray : identifier{$$=$1->datatype;}|arrayAssignment{$$=$1;};
arithmeticExpression : arithmeticExpression ADD arithmeticExpression{DataTypeCheck($1,$3);}|arithmeticExpression SUB arithmeticExpression{DataTypeCheck($1,$3);}|
arithmeticExpression MUL arithmeticExpression{DataTypeCheck($1,$3);}|arithmeticExpression DIV arithmeticExpression{DataTypeCheck($1,$3);}|
arithmeticExpression MOD arithmeticExpression|'('arithmeticExpression')'{$$=$2;}|
identifier{$$=$1->datatype;}|constantType{$$=$1->datatype;};
assignmentOperators : EQ{RHS=1;}|ASSIGN{RHS=1;}|PLUSEQ{RHS=1;}|MINUSEQ{RHS=1;}|MULEQ{RHS=1;}|DIVEQ{RHS=1;};
postPreExpression : DECR identifier{$$=$2->datatype;}|INCR identifier{$$=$2->datatype;}|identifier DECR{$$=$1->datatype;}|identifier INCR{$$=$1->datatype;};
arrayAssignment : identifier '['arrayIndex']'
{if(DECLARATION)
{
if($3->value < 1)
yyerror("Arrays can't have dimension lesser than 1");
else
{
if(CONSTANT)
{
$1->arrayDim=$3->value;
setArrayDim(SymbolTable,$1->arrayDim,$1->idName);
CONSTANT=0;
}
if($3->value>$1->arrayDim)
yyerror("Array index out of bound"); $ }
else if(CONSTANT)
{
if($3->value>$1->arrayDim)
yyerror("Array index out of bound");
if($3->value<0)
yyerror("Array index cannot be negative");
CONSTANT=0;
}
};
arrayIndex : identifier{$$=$1;}|constantType{$$=$1;};
constantType :INTCONST{CONSTANT=1;}|DECCONST{CONSTANT=1;}|HEXCONST{CONSTANT=1;};
pointer : '*' pointer | '*';
identifier : IDENTIFIER {if(DECLARATION && !RHS){
$1 = InsertNew(SymbolTable,yylineno,yytext,INT_MAX,curDataType);
$1 = InsertNew(symbolTableList[currentscope].symtable,yylineno,yytext,INT_MAX,curDataType);
if($1==NULL)
yyerror("Redeclaration of varriable");
}
else
{
$1 = searchRecursive(yytext);
if($1 == NULL)
yyerror("Variable Not Declared");
}
$$=$1;};
%%
#include "lex.yy.c"
int main(int argc , char *argv[]){
SymbolTable = createTable();
ConstantTable=createTable();
int i;
for(i=0; i<MAXSCOPE;i++)
{
symbolTableList[i].symtable = NULL;
symbolTableList[i].previous = -1;
}
symbolTableList[0].symtable = createTable();
yyin = fopen(argv[1], "r");
if(!yyparse())
{
printf("\nParsing complete.\n"); printf("\n********************SYMBOL TABLE*******************\n");
Display(SymbolTable,1);
printf("\n********************CONSTANT TABLE*******************\n");
Display(ConstantTable,2);
}
else
{
printf("\nParsing failed.\n");
}
fclose(yyin);
return 0;
}
int yyerror(char *msg)
{
printf("Line no: %d Error message: %s Token: %s\n", yylineno, msg, yytext);
exit(0);
return 0;
}