-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExpressionParser.cpp
More file actions
81 lines (72 loc) · 2.21 KB
/
Copy pathExpressionParser.cpp
File metadata and controls
81 lines (72 loc) · 2.21 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
#include "ExpressionParser.h"
ExpressionParser::ExpressionParser(const std::string& expression) : input(expression), position(0) {}
// Parses the expression and checks if it is fully parsed.
bool ExpressionParser::parse() {
if (expression()) {
return position == input.length();
}
return false;
}
// Checks if a character is an operator (+, -, *, /).
bool ExpressionParser::isOperator(char op) const {
return op == '+' || op == '-' || op == '*' || op == '/';
}
// Checks if a character is a valid identifier (alphabetical character).
bool ExpressionParser::isIdentifier(char ch) const {
return std::isalpha(ch);
}
// Function to match the expected character in the input string
void ExpressionParser::match(char expected) {
if (position < input.length() && input[position] == expected) {
position++;
} else {
throw std::runtime_error("Expected '" + std::string(1, expected) + "'");
}
}
// Function to parse a factor in the expression
bool ExpressionParser::factor() {
if (position < input.length()) {
if (isIdentifier(input[position]) || std::isdigit(input[position])) {
position++;
} else if (input[position] == '(') {
position++;
// Parse the expression within the parentheses
if (!expression()) {
return false;
}
// After parsing the expression, expect a closing parenthesis
match(')');
} else {
throw std::runtime_error("Invalid character");
}
} else {
throw std::runtime_error("Unexpected end of input");
}
return true;
}
// Function to parse a term in the expression
bool ExpressionParser::term() {
if (!factor()) {
return false;
}
while (position < input.length() && isOperator(input[position])) {
position++;
if (!factor()) {
return false;
}
}
return true;
}
// Function to parse an expression
bool ExpressionParser::expression() {
if (!term()) {
return false;
}
while (position < input.length() && isOperator(input[position])) {
position++;
if (!term()) {
return false;
}
}
return true;
}