-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
169 lines (126 loc) · 3.15 KB
/
parser.y
File metadata and controls
169 lines (126 loc) · 3.15 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
/*
File: parser.y
Author: Dylan Bryan
Date: 8/9/21, 8:33 PM
Project: project-four
Purpose: Define Parse tree grammar
and syntactic analyzer
*/
%{
#include <string>
#include <vector>
#include <map>
using namespace std;
#include "types.h"
#include "listing.h"
#include "symbols.h"
int yylex();
void yyerror(const char* message);
Symbols<Types> symbols;
%}
%error-verbose
%union
{
CharPtr iden;
Types type;
}
%token <iden> IDENTIFIER
%token <type> INT_LITERAL REAL_LITERAL BOOL_LITERAL
%token ADDOP MULOP RELOP REMOP EXPOP ANDOP OROP ARROWOP
%token BEGIN_ BOOLEAN CASE ELSE END ENDCASE ENDIF ENDREDUCE FUNCTION INTEGER
%token IF IS NOT OTHERS REAL REDUCE RETURNS THEN WHEN
%type <type> type statement statement_ reductions expression expression2 relation relation2 relation3 term factor primary cases case case2
%%
function:
function_header_ variables2 body ;
function_header_:
function_header |
error ';' ;
function_header:
FUNCTION IDENTIFIER parameters RETURNS type ';' ;
parameters:
parameter optional_parameter ;
optional_parameter:
',' parameter |
;
parameter:
IDENTIFIER ':' type ;
variables2:
variables optional_variable ;
variables:
variable optional_variable |
error ';' ;
optional_variable:
variable |
;
variable:
IDENTIFIER ':' type IS statement_
{checkAssignment($3, $5, "Variable Initialization");
symbols.findDuplicate($1, $3);
symbols.insert($1, $3);} ;
type:
INTEGER {$$ = INT_TYPE;} |
REAL {$$ = REAL_TYPE;} |
BOOLEAN {$$ = BOOL_TYPE;} ;
body:
BEGIN_ statement_ END ';' ;
statement_:
statement ';' |
error ';' {$$ = MISMATCH;} ;
statement:
expression |
REDUCE operator reductions ENDREDUCE {$$ = $3;} |
IF expression THEN statement_ ELSE statement_ ENDIF {checkIf($2, $4, $6);} |
CASE expression IS cases OTHERS ARROWOP statement_ ENDCASE {checkCase($2);} ;
operator:
ADDOP |
MULOP ;
reductions:
reductions statement_ {$$ = checkArithmetic($1, $2);} |
{$$ = INT_TYPE;} ;
cases:
case case2 {checkCases($1, $2, "Case");} ;
case:
WHEN INT_LITERAL ARROWOP expression ';' {$$ = $4;} ;
case2:
WHEN INT_LITERAL ARROWOP expression ';' {$$ = $4;} ;
expression:
expression ANDOP expression2 {$$ = checkLogical($1, $3);} |
expression2 ;
expression2:
expression2 OROP relation {$$ = checkLogical($1, $3);} |
relation ;
relation:
relation RELOP relation2 {$$ = checkRelational($1, $3);}|
relation2 ;
relation2:
relation2 REMOP relation3 {$$ = checkRemainder($1, $3);} |
relation3 ;
relation3:
relation3 EXPOP term {$$ = checkArithmetic($1, $3);} |
term ;
term:
term ADDOP factor {$$ = checkArithmetic($1, $3);} |
factor ;
factor:
factor MULOP primary {$$ = checkArithmetic($1, $3);} |
primary ;
primary:
'(' expression ')' {$$ = $2;} |
NOT expression {$$ = $2;} |
INT_LITERAL |
REAL_LITERAL |
BOOL_LITERAL |
IDENTIFIER {if (!symbols.find($1, $$)) appendError(UNDECLARED, $1);} ;
%%
void yyerror(const char* message)
{
appendError(SYNTAX, message);
}
int main(int argc, char *argv[])
{
firstLine();
yyparse();
lastLine();
return 0;
}