-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
185 lines (174 loc) · 5.94 KB
/
parser.cpp
File metadata and controls
185 lines (174 loc) · 5.94 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
/**
* CS 106B/X Stanford 1-2-3
* This file implements the parser.h interface.
*
* Please do not modify this provided file. Your turned-in files should work
* with an unmodified version of all provided code files.
*
* @author Marty Stepp, Julie Zelenski, Jerry Cain
* @version 2016/11/29
* - 16au 106x version (stepp)
*/
#include "parser.h"
#include <iostream>
#include <string>
#include "error.h"
#include "set.h"
#include "strlib.h"
#include "tokenscanner.h"
#include "expression.h"
#include "range.h"
// set to true to see debug messages related to parsing
static const bool DEBUG = false;
/**
* Implementation notes: parseExp
* ------------------------------
* This code just reads an expression and then checks for extra tokens.
*/
Expression* Parser::parseExpression(const std::string& rawText) {
TokenScanner scanner;
scanner.ignoreWhitespace();
scanner.scanNumbers();
scanner.scanStrings();
scanner.setInput(rawText);
Expression* exp = readExpression(scanner);
exp->setRawText(rawText);
return exp;
}
/**
* Implementation notes: precedence
* --------------------------------
* This function checks the token against each of the defined operators
* and returns the appropriate precedence value.
*/
int Parser::precedence(const std::string& token) {
if (token == "+" || token == "-") {
return 1;
} else if (token == "*" || token == "/") {
return 2;
} else {
return 0;
}
}
/**
* Implementation notes: readExpression
* ------------------------------------
* This function scans an overall cell expression.
* An expression can be a number, a string, or a formula.
*/
Expression* Parser::readExpression(TokenScanner& scanner) {
if (DEBUG) std::cout << " readExpr(" << scanner << ")" << std::endl;
std::string token = scanner.nextToken();
TokenType type = scanner.getTokenType(token);
if (token == "=") {
// beginning of a formula
Expression* exp = readFormula(scanner);
if (scanner.hasMoreTokens()) {
error("Parse error: Unexpected token: \"" + scanner.nextToken() + "\"");
}
return exp;
} else if (type == NUMBER && !scanner.hasMoreTokens()) {
return new DoubleExp(stringToReal(token));
} else {
return new TextStringExp(trim(scanner.getInput()));
}
}
/**
* Implementation notes: readFormula
* Usage: exp = readFormula(scanner, prec);
* ----------------------------------------
* This implementation uses precedence to resolve the ambiguity in
* the grammar. At each level, the parser reads operators and subexpressions
* until it finds an operator whose precedence is greater than the prevailing
* one. When a higher-precedence operator is found, readE calls itself
* recursively to read that subexpression as a unit.
*/
Expression* Parser::readFormula(TokenScanner& scanner, int prec) {
if (DEBUG) std::cout << " readForm(" << scanner << "), prec=" << prec << ")" << std::endl;
Expression* exp = readTerm(scanner);
std::string token;
while (true) {
// read operator
token = scanner.nextToken();
int tprec = precedence(token);
if (tprec <= prec) {
break;
}
if (!scanner.hasMoreTokens()) {
error("Parse error: Invalid binary " + token
+ " expression; missing right operand");
exp = nullptr;
} else {
Expression* rhs = readFormula(scanner, tprec);
exp = new CompoundExp(token, exp, rhs);
}
}
scanner.saveToken(token);
return exp;
}
/**
* Implementation notes: readRange
* Usage: exp = readRange(scanner);
* --------------------------------
* This function scans a range of cells, such as A1:A7.
*/
Range Parser::readRange(TokenScanner& scanner) {
if (DEBUG) std::cout << " readRang(" << scanner << ")" << std::endl;
if (scanner.nextToken() != "(") {
error("Parse error: Invalid range format; missing initial (.");
}
std::string startCellName = scanner.nextToken();
if (!Range::isValidName(startCellName)) {
error("Parse error: Invalid start cell name for range: \"" + startCellName + "\"");
}
std::string sep = scanner.nextToken();
if (sep != ":" && sep != "-") {
error("Parse error: Invalid range format; missing : in middle.");
}
std::string endCellName = scanner.nextToken();
if (!Range::isValidName(endCellName)) {
error("Parse error: Invalid end cell name for range: \"" + endCellName + "\"");
}
if (scanner.nextToken() != ")") {
error("Parse error: Invalid range format; missing final ).");
}
//Check that is valid values for range
Range rng(startCellName, endCellName);
return rng;
}
/**
* Implementation notes: readTerm
* ------------------------------
* This function scans a term, which is either an integer, an identifier,
* or a parenthesized subexpression.
*/
Expression* Parser::readTerm(TokenScanner& scanner) {
if (DEBUG) std::cout << "readTerm(" << scanner << ")" << std::endl;
std::string token = scanner.nextToken();
TokenType type = scanner.getTokenType(token);
Expression* result = nullptr;
if (token == "(") {
// beginning of a parenthesized expression
Expression* exp = readFormula(scanner);
token = scanner.nextToken();
if (token != ")") {
error("Parse error: Unclosed parenthesis.");
} else {
result = exp;
}
} else if (type == NUMBER) {
result = new DoubleExp(stringToReal(token));
} else if (type == WORD) {
token = toUpperCase(token);
if (Range::isKnownFunctionName(token)) {
result = new RangeExp(token, readRange(scanner));
} else if (Range::isValidName(token)) {
result = new IdentifierExp(token);
} else {
error("Parse error: Invalid cell name or token: \"" + token + "\"");
}
} else {
result = new TextStringExp(token);
}
return result;
}