-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.hpp
160 lines (145 loc) · 3.5 KB
/
parser.hpp
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
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <cctype>
/*
* A tiny recursive-descent parser which uses
* syntax-directed translation to convert infix
* expressions to postfix using the following production
* rules and translation scheme.
*
* ===================================
* PRODUCTION RULES
* ===================================
* expr -> expr + term | expr - term | term
* term -> term * factor | term / factor | factor
* factor -> (expr) | digit
* digit -> 0|1|2|3|4|5|6|7|8|9
*
* ===================================
* TRANSLATION SCHEME
* ===================================
*
* expr -> expr + term {print("+")}
* | expr - term {print("-")}
* | term
*
* term -> term * factor {print("*")}
* | term / factor {print("/")}
* | factor
*
* factor -> (expr) | digit
*
* digit -> 0 {print("0")}
* | 1 {print("1")}
* ...
* ...
* | 9 {print("9")}
*
* ==================================
* ELIMINATING LEFT RECURSION
* ==================================
*
* expr -> term expr'
* expr' -> + term {print("+")} expr'
* | - term {print("-")} expr'
* | epsilon
* term -> factor term'
* term' -> * factor {print("*")} term'
* | / factor {print("/")} term'
* | epsilon
* factor -> (expr) | digit
* digit -> 0 {print("0")}
* | 1 {print("1")}
* ...
* ...
* | 9 {print("9")}
*/
class Parser {
public:
Parser() {
std::string str;
std::cout << "Enter infix string : ";
std::cin >> str;
stream = std::stringstream(str);
lookahead = stream.get();
}
void parse() {
expr();
print_translated_string();
}
private:
char lookahead;
std::string translated;
std::stringstream stream;
void display(char terminal) {
translated += terminal;
}
void print_translated_string() {
std::cout << translated << std::endl;
}
std::string error_message(char lookahead) {
return "Syntax error near " + std::string(1, lookahead);
}
void match(char terminal) {
if (terminal == lookahead) {
lookahead = stream.get();
}
else {
throw std::invalid_argument(error_message(lookahead));
}
}
void digit() {
display(lookahead);
match(lookahead);
}
void factor() {
if (isdigit(lookahead)) {
digit();
}
else if (lookahead == '(') {
match('(');
expr();
match(')');
}
else {
throw std::invalid_argument(error_message(lookahead));
}
}
void term() {
factor();
while (true) {
if (lookahead == '*') {
match('*');
factor();
display('*');
}
else if (lookahead == '/') {
match('/');
factor();
display('/');
}
else {
return;
}
}
}
void expr() {
term();
while (true) {
if (lookahead == '+') {
match('+');
term();
display('+');
}
else if (lookahead == '-') {
match('-');
term();
display('-');
}
else {
return;
}
}
}
};