-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
252 lines (205 loc) · 11.8 KB
/
parser.y
File metadata and controls
252 lines (205 loc) · 11.8 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
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
241
242
243
244
245
246
247
248
249
250
251
%{
/***********************************************************************
* Interface to the parser module for CSC467 course project.
*
* Phase 2: Implement context free grammar for source language, and
* parse tracing functionality.
* Phase 3: Construct the AST for the source language program.
***********************************************************************/
/***********************************************************************
* C Definitions and external declarations for this module.
*
* Phase 3: Include ast.h if needed, and declarations for other global or
* external vars, functions etc. as needed.
***********************************************************************/
#include <string.h>
#include "common.h"
//#include "ast.h"
//#include "symbol.h"
//#include "semantic.h"
#define YYERROR_VERBOSE
#define yTRACE(x) { if (traceParser) fprintf(traceFile, "%s\n", x); }
void yyerror(const char* s); /* what to do in case of error */
int yylex(); /* procedure for calling lexical analyzer */
extern int yyline; /* variable holding current line number */
%}
/***********************************************************************
* Yacc/Bison declarations.
* Phase 2:
* 1. Add precedence declarations for operators (after %start declaration)
* 2. If necessary, add %type declarations for some nonterminals
* Phase 3:
* 1. Add fields to the union below to facilitate the construction of the
* AST (the two existing fields allow the lexical analyzer to pass back
* semantic info, so they shouldn't be touched).
* 2. Add <type> modifiers to appropriate %token declarations (using the
* fields of the union) so that semantic information can by passed back
* by the scanner.
* 3. Make the %type declarations for the language non-terminals, utilizing
* the fields of the union as well.
***********************************************************************/
%{
#define YYDEBUG 1
%}
// TODO:Modify me to add more data types
// Can access me from flex useing yyval
%union {
int i_value;
float f_value;
char *var_name;
}
%token TOKEN_VAR
%token TOKEN_RESULT_GL_FRAGCOLOR TOKEN_RESULT_GL_FRAGDEPTH TOKEN_RESULT_GL_FRAGCOORD TOKEN_ATTR_GL_TEXCOORD TOKEN_ATTR_GL_COLOR TOKEN_ATTR_GL_SECONDARY TOKEN_ATTR_GL_FOGFRAGCOORD TOKEN_UNIF_GL_LIGHT_HALF TOKEN_UNIF_GL_LIGHT_AMBIENT TOKEN_UNIF_GL_MATERIAL_SHININESS TOKEN_UNIF_ENV1 TOKEN_UNIF_ENV2 TOKEN_UNIF_ENV3
%token TOKEN_FUNC_LIT TOKEN_FUNC_DP3 TOKEN_FUNC_RSQ
%token TOKEN_TYPE_BOOL TOKEN_TYPE_BVEC2 TOKEN_TYPE_BVEC3 TOKEN_TYPE_BVEC4 TOKEN_TYPE_INT TOKEN_TYPE_IVEC2 TOKEN_TYPE_IVEC3 TOKEN_TYPE_IVEC4 TOKEN_TYPE_FLOAT TOKEN_TYPE_VEC2 TOKEN_TYPE_VEC3 TOKEN_TYPE_VEC4
%token TOKEN_NUM_INT TOKEN_NUM_TRUE TOKEN_NUM_FALSE TOKEN_NUM_FLOAT
%token TOKEN_KEYWORD_IF TOKEN_KEYWORD_ELSE TOKEN_KEYWORD_WHILE TOKEN_KEYWORD_CONST
%precedence WITHOUT_ELSE
%precedence WITH_ELSE
%left TOKEN_OP_OR
%left TOKEN_OP_AND
%token TOKEN_OP_EQUAL TOKEN_OP_NOT_EQUAL TOKEN_OP_LESS_THAN TOKEN_OP_LESS_THAN_OR_EQUAL TOKEN_OP_GREATER_THAN TOKEN_OP_GREATER_THAN_OR_EQUAL
%left TOKEN_OP_PLUS TOKEN_OP_MINUS
%left TOKEN_OP_MULTIPLY TOKEN_OP_DIVIDE
%right TOKEN_OP_POWER
%token TOKEN_OP_NOT
%precedence UNARY
%left TOKEN_PUNC_OPEN_PAREN TOKEN_PUNC_CLOSE_PAREN TOKEN_PUNC_SEMICOLON TOKEN_PUNC_COMMA TOKEN_PUNC_OPEN_BRACE TOKEN_PUNC_CLOSE_BRACE TOKEN_PUNC_OPEN_BRACKET TOKEN_PUNC_CLOSE_BRACKET
%token TOKEN_OP_ASSIGNMENT
%start program
%%
/***********************************************************************
* Yacc/Bison rules
* Phase 2:
* 1. Replace grammar found here with something reflecting the source
* language grammar
* 2. Implement the trace parser option of the compiler
* Phase 3:
* 1. Add code to rules for construction of AST.
***********************************************************************/
program
: scope {yTRACE("program -> scope")}
;
scope
: TOKEN_PUNC_OPEN_BRACE declarations statements TOKEN_PUNC_CLOSE_BRACE {yTRACE("scope -> TOKEN_PUNC_OPEN_BRACE declarations statements TOKEN_PUNC_CLOSE_BRACE")}
;
declarations
: declarations declaration {yTRACE("declarations -> declarations declaration")}
| %empty {yTRACE("declarations -> %empty")}
;
statements
: statements statement {yTRACE("statements -> statements statement")}
| %empty {yTRACE("statements -> %empty")}
;
declaration
: type TOKEN_VAR TOKEN_PUNC_SEMICOLON {yTRACE("declaration -> type TOKEN_VAR TOKEN_PUNC_SEMICOLON")}
| type TOKEN_VAR TOKEN_OP_ASSIGNMENT expression TOKEN_PUNC_SEMICOLON {yTRACE("declaration -> type TOKEN_VAR TOKEN_OP_EQUAL expression TOKEN_PUNC_SEMICOLON")}
| TOKEN_KEYWORD_CONST type TOKEN_VAR TOKEN_OP_ASSIGNMENT expression TOKEN_PUNC_SEMICOLON {yTRACE("declaration -> TOKEN_KEYWORD_CONST type TOKEN_VAR TOKEN_OP_EQUAL expression TOKEN_PUNC_SEMICOLON")}
;
statement
: variable TOKEN_OP_ASSIGNMENT expression TOKEN_PUNC_SEMICOLON {yTRACE("statement -> variable TOKEN_OP_ASSIGNMENT expression TOKEN_PUNC_SEMICOLON")}
| TOKEN_KEYWORD_IF TOKEN_PUNC_OPEN_PAREN expression TOKEN_PUNC_CLOSE_PAREN statement TOKEN_KEYWORD_ELSE statement %prec WITH_ELSE {yTRACE("statement -> TOKEN_KEYWORD_IF TOKEN_PUNC_OPEN_PAREN expression TOKEN_PUNC_CLOSE_PAREN statement TOKEN_KEYWORD_ELSE statement")}
| TOKEN_KEYWORD_IF TOKEN_PUNC_OPEN_PAREN expression TOKEN_PUNC_CLOSE_PAREN statement %prec WITHOUT_ELSE {yTRACE("statement -> TOKEN_KEYWORD_IF TOKEN_PUNC_OPEN_PAREN expression TOKEN_PUNC_CLOSE_PAREN statement")}
| TOKEN_KEYWORD_WHILE TOKEN_PUNC_OPEN_PAREN expression TOKEN_PUNC_CLOSE_PAREN statement {yTRACE("statement -> TOKEN_KEYWORD_WHILE TOKEN_PUNC_OPEN_PAREN expression TOKEN_PUNC_CLOSE_PAREN statement")}
| scope {yTRACE("statement -> scope")}
| TOKEN_PUNC_SEMICOLON {yTRACE("statement -> TOKEN_PUNC_SEMICOLON")}
;
type
: TOKEN_TYPE_BOOL {yTRACE("type -> TOKEN_TYPE_BOOL")}
| TOKEN_TYPE_BVEC2 {yTRACE("type -> TOKEN_TYPE_BVEC2")}
| TOKEN_TYPE_BVEC3 {yTRACE("type -> TOKEN_TYPE_BVEC3")}
| TOKEN_TYPE_BVEC4 {yTRACE("type -> TOKEN_TYPE_BVEC4")}
| TOKEN_TYPE_INT {yTRACE("type -> TOKEN_TYPE_INT")}
| TOKEN_TYPE_IVEC2 {yTRACE("type -> TOKEN_TYPE_IVEC2")}
| TOKEN_TYPE_IVEC3 {yTRACE("type -> TOKEN_TYPE_IVEC3")}
| TOKEN_TYPE_IVEC4 {yTRACE("type -> TOKEN_TYPE_IVEC4")}
| TOKEN_TYPE_FLOAT {yTRACE("type -> TOKEN_TYPE_FLOAT")}
| TOKEN_TYPE_VEC2 {yTRACE("type -> TOKEN_TYPE_VEC2")}
| TOKEN_TYPE_VEC3 {yTRACE("type -> TOKEN_TYPE_VEC3")}
| TOKEN_TYPE_VEC4 {yTRACE("type -> TOKEN_TYPE_VEC4")}
;
expression
: TOKEN_NUM_INT {yTRACE("expression -> TOKEN_NUM_INT")}
| TOKEN_NUM_TRUE {yTRACE("expression -> TOKEN_NUM_TRUE")}
| TOKEN_NUM_FALSE {yTRACE("expression -> TOKEN_NUM_FALSE")}
| TOKEN_NUM_FLOAT {yTRACE("expression -> TOKEN_NUM_FLOAT")}
| variable {yTRACE("expression -> variable")}
| TOKEN_PUNC_OPEN_PAREN expression TOKEN_PUNC_CLOSE_PAREN {yTRACE("expression -> TOKEN_PUNC_OPEN_PAREN expression TOKEN_PUNC_CLOSE_PAREN")}
| TOKEN_OP_MINUS expression %prec UNARY {yTRACE("expression -> TOKEN_OP_MINUS expression")}
| TOKEN_OP_NOT expression {yTRACE("expression -> TOKEN_OP_NOT expression")}
| expression TOKEN_OP_PLUS expression {yTRACE("expression -> expression TOKEN_OP_PLUS expression")}
| expression TOKEN_OP_MINUS expression {yTRACE("expression -> expression TOKEN_OP_MINUS expression")}
| expression TOKEN_OP_EQUAL expression {yTRACE("expression -> expression TOKEN_OP_EQUAL expression")}
| expression TOKEN_OP_NOT_EQUAL expression {yTRACE("expression -> expression TOKEN_OP_NOT_EQUAL expression")}
| expression TOKEN_OP_LESS_THAN expression {yTRACE("expression -> expression TOKEN_OP_LESS_THAN expression")}
| expression TOKEN_OP_LESS_THAN_OR_EQUAL expression {yTRACE("expression -> expression TOKEN_OP_LESS_THAN_OR_EQUAL expression")}
| expression TOKEN_OP_GREATER_THAN expression {yTRACE("expression -> expression TOKEN_OP_GREATER_THAN expression")}
| expression TOKEN_OP_GREATER_THAN_OR_EQUAL expression {yTRACE("expression -> expression TOKEN_OP_GREATER_THAN_OR_EQUAL expression")}
| expression TOKEN_OP_POWER expression {yTRACE("expression -> expression TOKEN_OP_POWER expression")}
| expression TOKEN_OP_MULTIPLY expression {yTRACE("expression -> expression TOKEN_OP_MULTIPLY expression")}
| expression TOKEN_OP_DIVIDE expression {yTRACE("expression -> expression TOKEN_OP_DIVIDE expression")}
| expression TOKEN_OP_AND expression {yTRACE("expression -> expression TOKEN_OP_AND expression")}
| expression TOKEN_OP_OR expression {yTRACE("expression -> expression TOKEN_OP_OR expression")}
| constructor {yTRACE("expression -> constructor")}
| function {yTRACE("expression -> function")}
;
variable
: identifier {yTRACE("variable -> identifier")}
| identifier TOKEN_PUNC_OPEN_BRACKET TOKEN_NUM_INT TOKEN_PUNC_CLOSE_BRACKET {yTRACE("variable -> identifier TOKEN_PUNC_OPEN_BRACKET TOKEN_NUM_INT TOKEN_PUNC_CLOSE_BRACKET")}
;
identifier
: TOKEN_VAR {yTRACE("identifier -> TOKEN_VAR")}
| TOKEN_RESULT_GL_FRAGCOLOR {yTRACE("identifier -> TOKEN_RESULT_GL_FRAGCOLOR")}
| TOKEN_RESULT_GL_FRAGDEPTH {yTRACE("identifier -> TOKEN_RESULT_GL_FRAGDEPTH")}
| TOKEN_RESULT_GL_FRAGCOORD {yTRACE("identifier -> TOKEN_RESULT_GL_FRAGCOORD")}
| TOKEN_ATTR_GL_TEXCOORD {yTRACE("identifier -> TOKEN_ATTR_GL_TEXCOORD")}
| TOKEN_ATTR_GL_COLOR {yTRACE("identifier -> TOKEN_ATTR_GL_COLOR")}
| TOKEN_ATTR_GL_SECONDARY {yTRACE("identifier -> TOKEN_ATTR_GL_SECONDARY")}
| TOKEN_ATTR_GL_FOGFRAGCOORD {yTRACE("identifier -> TOKEN_ATTR_GL_FOGFRAGCOORD")}
| TOKEN_UNIF_GL_LIGHT_HALF {yTRACE("identifier -> TOKEN_UNIF_GL_LIGHT_HALF")}
| TOKEN_UNIF_GL_LIGHT_AMBIENT {yTRACE("identifier -> TOKEN_UNIF_GL_LIGHT_AMBIENT")}
| TOKEN_UNIF_GL_MATERIAL_SHININESS {yTRACE("identifier -> TOKEN_UNIF_GL_MATERIAL_SHININESS")}
| TOKEN_UNIF_ENV1 {yTRACE("identifier -> TOKEN_UNIF_ENV1")}
| TOKEN_UNIF_ENV2 {yTRACE("identifier -> TOKEN_UNIF_ENV2")}
| TOKEN_UNIF_ENV3 {yTRACE("identifier -> TOKEN_UNIF_ENV3")}
;
constructor
: type TOKEN_PUNC_OPEN_PAREN arguments TOKEN_PUNC_CLOSE_PAREN {yTRACE("constructor -> type TOKEN_PUNC_OPEN_PAREN arguments TOKEN_PUNC_CLOSE_PAREN")}
;
function
: function_name TOKEN_PUNC_OPEN_PAREN optional_arguments TOKEN_PUNC_CLOSE_PAREN {yTRACE("function -> function_name TOKEN_PUNC_OPEN_PAREN optional_arguments TOKEN_PUNC_CLOSE_PAREN")}
;
function_name
: TOKEN_FUNC_LIT {yTRACE("function_name -> TOKEN_FUNC_LIT")}
| TOKEN_FUNC_DP3 {yTRACE("function_name -> TOKEN_FUNC_DP3")}
| TOKEN_FUNC_RSQ {yTRACE("function_name -> TOKEN_FUNC_RSQ")}
;
optional_arguments
: arguments {yTRACE("optional_arguments -> arguments")}
| %empty {yTRACE("optional_arguments -> %empty")}
;
arguments
: arguments TOKEN_PUNC_COMMA expression {yTRACE("arguments -> arguments TOKEN_PUNC_COMMA expression")}
| expression {yTRACE("arguments -> expression")}
;
%%
/***********************************************************************ol
* Extra C code.
*
* The given yyerror function should not be touched. You may add helper
* functions as necessary in subsequent phases.
***********************************************************************/
void yyerror(const char* s) {
if (errorOccurred)
return; /* Error has already been reported by scanner */
else
errorOccurred = 1;
fprintf(errorFile, "\nPARSER ERROR, LINE %d",yyline);
if (strcmp(s, "parse error")) {
if (strncmp(s, "parse error, ", 13))
fprintf(errorFile, ": %s\n", s);
else
fprintf(errorFile, ": %s\n", s+13);
} else
fprintf(errorFile, ": Reading token %s\n", yytname[YYTRANSLATE(yychar)]);
}