Repository navigation
Expand file tree
/
Copy pathParser.java
More file actions
227 lines (192 loc) · 7 KB
/
Copy pathParser.java
File metadata and controls
227 lines (192 loc) · 7 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import java.util.*;
public class Parser {
// Recursive descent parser that inputs a C++Lite program and
// generates its abstract syntax. Each method corresponds to
// a concrete syntax grammar rule, which appears as a comment
// at the beginning of the method.
Token token; // current token from the input stream
Lexer lexer;
public Parser(Lexer ts) { // Open the C++Lite source program
lexer = ts; // as a token stream, and
token = lexer.next(); // retrieve its first Token
}
private String match (TokenType t) {
String value = token.value();
if (token.type().equals(t))
token = lexer.next();
else
error(t);
return value;
}
private void error(TokenType tok) {
System.err.println("Syntax error: expecting: " + tok
+ "; saw: " + token);
System.exit(1);
}
private void error(String tok) {
System.err.println("Syntax error: expecting: " + tok
+ "; saw: " + token);
System.exit(1);
}
public Program program() {
// Program --> void main ( ) '{' Declarations Statements '}'
TokenType[ ] header = {TokenType.Int, TokenType.Main,
TokenType.LeftParen, TokenType.RightParen};
for (int i=0; i<header.length; i++) // bypass "int main ( )"
match(header[i]);
match(TokenType.LeftBrace);
// student exercise
match(TokenType.RightBrace);
return null; // student exercise
}
private Declarations declarations () {
// Declarations --> { Declaration }
return null; // student exercise
}
private void declaration (Declarations ds) {
// Declaration --> Type Identifier { , Identifier } ;
// student exercise
}
private Type type () {
// Type --> int | bool | float | char
Type t = null;
// student exercise
return t;
}
private Statement statement() {
// Statement --> ; | Block | Assignment | IfStatement | WhileStatement
Statement s = new Skip();
// student exercise
return s;
}
private Block statements () {
// Block --> '{' Statements '}'
Block b = new Block();
// student exercise
return b;
}
private Assignment assignment () {
// Assignment --> Identifier = Expression ;
return null; // student exercise
}
private Conditional ifStatement () {
// IfStatement --> if ( Expression ) Statement [ else Statement ]
return null; // student exercise
}
private Loop whileStatement () {
// WhileStatement --> while ( Expression ) Statement
return null; // student exercise
}
private Expression expression () {
// Expression --> Conjunction { || Conjunction }
return null; // student exercise
}
private Expression conjunction () {
// Conjunction --> Equality { && Equality }
return null; // student exercise
}
private Expression equality () {
// Equality --> Relation [ EquOp Relation ]
return null; // student exercise
}
private Expression relation (){
// Relation --> Addition [RelOp Addition]
return null; // student exercise
}
private Expression addition () {
// Addition --> Term { AddOp Term }
Expression e = term();
while (isAddOp()) {
Operator op = new Operator(match(token.type()));
Expression term2 = term();
e = new Binary(op, e, term2);
}
return e;
}
private Expression term () {
// Term --> Factor { MultiplyOp Factor }
Expression e = factor();
while (isMultiplyOp()) {
Operator op = new Operator(match(token.type()));
Expression term2 = factor();
e = new Binary(op, e, term2);
}
return e;
}
private Expression factor() {
// Factor --> [ UnaryOp ] Primary
if (isUnaryOp()) {
Operator op = new Operator(match(token.type()));
Expression term = primary();
return new Unary(op, term);
}
else return primary();
}
private Expression primary () {
// Primary --> Identifier | Literal | ( Expression )
// | Type ( Expression )
Expression e = null;
if (token.type().equals(TokenType.Identifier)) {
e = new Variable(match(TokenType.Identifier));
} else if (isLiteral()) {
e = literal();
} else if (token.type().equals(TokenType.LeftParen)) {
token = lexer.next();
e = expression();
match(TokenType.RightParen);
} else if (isType( )) {
Operator op = new Operator(match(token.type()));
match(TokenType.LeftParen);
Expression term = expression();
match(TokenType.RightParen);
e = new Unary(op, term);
} else error("Identifier | Literal | ( | Type");
return e;
}
private Value literal( ) {
return null; // student exercise
}
private boolean isAddOp( ) {
return token.type().equals(TokenType.Plus) ||
token.type().equals(TokenType.Minus);
}
private boolean isMultiplyOp( ) {
return token.type().equals(TokenType.Multiply) ||
token.type().equals(TokenType.Divide);
}
private boolean isUnaryOp( ) {
return token.type().equals(TokenType.Not) ||
token.type().equals(TokenType.Minus);
}
private boolean isEqualityOp( ) {
return token.type().equals(TokenType.Equals) ||
token.type().equals(TokenType.NotEqual);
}
private boolean isRelationalOp( ) {
return token.type().equals(TokenType.Less) ||
token.type().equals(TokenType.LessEqual) ||
token.type().equals(TokenType.Greater) ||
token.type().equals(TokenType.GreaterEqual);
}
private boolean isType( ) {
return token.type().equals(TokenType.Int)
|| token.type().equals(TokenType.Bool)
|| token.type().equals(TokenType.Float)
|| token.type().equals(TokenType.Char);
}
private boolean isLiteral( ) {
return token.type().equals(TokenType.IntLiteral) ||
isBooleanLiteral() ||
token.type().equals(TokenType.FloatLiteral) ||
token.type().equals(TokenType.CharLiteral);
}
private boolean isBooleanLiteral( ) {
return token.type().equals(TokenType.True) ||
token.type().equals(TokenType.False);
}
public static void main(String args[]) {
Parser parser = new Parser(new Lexer(args[0]));
Program prog = parser.program();
prog.display(); // display abstract syntax tree
} //main
} // Parser