-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopDownParser.cpp
More file actions
200 lines (171 loc) · 4.39 KB
/
topDownParser.cpp
File metadata and controls
200 lines (171 loc) · 4.39 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
/*
Calculator.cpp - A simple arithmetic interpreter.
Name: R. Shore
Class: CSC-4510
The program demonstrates two main components of
an interpreter/compiler.
1) A lexical analyzer to scan the input for
LETTERs, DIGITs, or OPERATORs
An IDENT consist of a LETTER followed by
a LETTER or DIGIT (max 99 characters)
AN INT_LIT consists of a sequence of DIGITs
(max 99 digits).
An OPERATOR =, +, -, *, /, (, )
lexeme[] holds the item read from source
nextToken holds the type
2) A recursive descent parser
The Grammar - EBNF description
<expr> → <term> {(+ | -) <term>}
<term> → <factor> {(* | /) <factor>}
<factor> → id | int_constant | ( <expr> )
NOTE: the recusive descent starts at <expr>
*/
#include<iostream>
#include<stdio.h>
#include"general.h"
#include"lex.h"
#include"topDownParser.h"
#include"symTable.h"
using namespace std;
/******************************************************/
/* main driver */
int main(void) {
do {
getChar();
lex();
cout << " = " << stmt() << endl;
} while (nextToken != EOF);
//}
}
int stmt()
{
int return_val = 0;
//for assignment statements
if(nextToken == IDENT) {
//check for quit
if(strcmp(lexeme,"quit") == 0) {
exit(11);
}
else if(strcmp(lexeme,"dump") == 0) {
symbolTable.dump_table();
}
else {
//get the next token
Symbol_ptr var_to_assign = symbolTable.insert(lexeme);
//expr is too extreme here, should only
return_val = expr();
//factor();
while (nextToken == ASSIGN_OP) {
lex();
return_val = stmt();
//return_val = expr();
//cout << "return_val = " << return_val << endl;
var_to_assign->putval(return_val);
}
}
} else {
//plane expressions
return_val = expr();
}
return return_val;
}
/* expr
Parses strings in the language generated by the rule:
<expr> -> <term> {(+ | -) <term>}
*/
int expr()
{
int return_val;
/* Parse the first term */
return_val = term();
/*while (nextToken == ASSIGN_OP) {
lex();
term();
}*/
/* As long as the next token is + or -, get
the next token and parse the next term */
while (nextToken == ADD_OP ) {
lex();
return_val += term();
}
while (nextToken == SUB_OP) {
lex();
return_val -= term();
}
return return_val;
} /* End of function expr */
/* term
* Parses strings in the language generated by the rule:
* <term> -> <factor> {(* | /) <factor>)
* <term> -> <factor>
* <term> -> - <factor>
*/
int term()
{
int return_val;
/* Parse the first factor */
if (nextToken == SUB_OP) {
//negative
lex();
return_val = factor() * -1;
} else {
return_val = factor();
}
/* As long as the next token is * or /, get the
next token and parse the next factor */
while (nextToken == MULT_OP){
lex();
return_val *= factor();
}
while ( nextToken == DIV_OP) {
lex();
return_val /= factor();
}
while (nextToken == MOD_OP) {
lex();
return_val = return_val % factor();
}
while (nextToken == POW_OP) {
lex();
return_val = pow(return_val,factor());
}
return return_val;
} /* End of function term */
/* factor
* Parses strings in the language generated by the rule:
*
* <factor> -> id | int_constant | ( <expr )
*
*/
int factor()
{
int return_val = 0;
/* Determine which RHS */
if (nextToken == IDENT) {
//look up the ident in the symbol table
return_val = symbolTable.insert(lexeme)->getval();
lex();
} else if (nextToken == INT_LIT){
/* Get the next token d*/
return_val = atoi(lexeme);
lex();
/* If the RHS is ( <expr> ), call lex to pass over the left
parenthesis, call expr and check for the right parenthesis */
} else if (nextToken == LEFT_PAREN) {
lex();
return_val = expr();
if (nextToken == RIGHT_PAREN)
lex();
else
error("Right without left paren");
} else {
/* It was not an id, an integer literal, or a left
parenthesis */
error("expected an id, integer, or a left paren");
} /* End of else */
return return_val;
}/* End of function factor */
void error(const char *message)
{
printf("Error: %s\n",message);
}