Skip to content

Commit 0480c71

Browse files
authored
Refactor ConcreteSyntax for improved readability
1 parent a44bd4b commit 0480c71

File tree

1 file changed

+74
-135
lines changed

1 file changed

+74
-135
lines changed
Lines changed: 74 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package com.scanner.project;
2-
// ConcreteSyntax.java
32

43
public class ConcreteSyntax {
54

65
public Token token;
7-
public TokenStream input;
6+
public TokenStream input;
87

9-
public ConcreteSyntax(TokenStream ts) {
10-
input = ts;
11-
token = input.nextToken();
8+
public ConcreteSyntax(TokenStream ts) {
9+
input = ts;
10+
token = input.nextToken();
1211
}
1312

14-
private String SyntaxError(String tok) {
15-
String s = "Syntax error - Expecting: " + tok + " But saw: "
16-
+ token.getType() + " = " + token.getValue();
17-
System.out.println(s);
18-
return s;
13+
private String SyntaxError(String expected) {
14+
return "Syntax error - Expecting: " + expected +
15+
" But saw: " + token.getType() + " = " + token.getValue();
1916
}
2017

2118
private void match(String s) {
@@ -36,12 +33,10 @@ public Program program() {
3633
}
3734

3835
private Declarations declarations() {
39-
Declarations ds = new Declarations();
40-
while (token.getValue().equals("integer")
41-
|| token.getValue().equals("bool")) {
42-
declaration(ds);
43-
}
44-
return ds;
36+
Declarations d = new Declarations();
37+
while (token.getValue().equals("integer") || token.getValue().equals("bool"))
38+
declaration(d);
39+
return d;
4540
}
4641

4742
private void declaration(Declarations ds) {
@@ -51,92 +46,71 @@ private void declaration(Declarations ds) {
5146
}
5247

5348
private Type type() {
54-
Type t;
55-
if (token.getValue().equals("integer"))
56-
t = new Type("integer");
57-
else if (token.getValue().equals("bool"))
58-
t = new Type("bool");
59-
else
60-
throw new RuntimeException(SyntaxError("integer | bool"));
61-
token = input.nextToken();
62-
return t;
49+
if (token.getValue().equals("integer") || token.getValue().equals("bool")) {
50+
Type t = new Type(token.getValue());
51+
token = input.nextToken();
52+
return t;
53+
}
54+
throw new RuntimeException(SyntaxError("integer | bool"));
6355
}
6456

6557
private void identifiers(Declarations ds, Type t) {
66-
Declaration d = new Declaration();
67-
d.t = t;
68-
if (token.getType().equals("Identifier")) {
58+
if (!token.getType().equals("Identifier"))
59+
throw new RuntimeException(SyntaxError("Identifier"));
60+
61+
while (true) {
62+
Declaration d = new Declaration();
63+
d.t = t;
6964
d.v = new Variable();
70-
d.v.id = token.getValue();
65+
d.v.id = token.getValue();
7166
ds.addElement(d);
7267
token = input.nextToken();
73-
while (token.getValue().equals(",")) {
74-
d = new Declaration();
75-
d.t = t;
76-
token = input.nextToken();
77-
if (token.getType().equals("Identifier")) {
78-
d.v = new Variable();
79-
d.v.id = token.getValue();
80-
ds.addElement(d);
81-
token = input.nextToken();
82-
} else
83-
throw new RuntimeException(SyntaxError("Identifier"));
84-
}
85-
} else
86-
throw new RuntimeException(SyntaxError("Identifier"));
87-
}
8868

89-
private Statement statement() {
90-
Statement s = new Skip();
91-
if (token.getValue().equals(";")) {
69+
if (!token.getValue().equals(",")) break;
9270
token = input.nextToken();
93-
return s;
94-
} else if (token.getValue().equals("{")) {
95-
token = input.nextToken();
96-
s = statements();
97-
match("}");
98-
} else if (token.getValue().equals("if")) {
99-
s = ifStatement();
100-
} else if (token.getValue().equals("while")) {
101-
s = whileStatement();
102-
} else if (token.getType().equals("Identifier")) {
103-
s = assignment();
104-
} else {
105-
throw new RuntimeException(SyntaxError("Statement"));
10671
}
107-
return s;
10872
}
10973

11074
private Block statements() {
11175
Block b = new Block();
112-
while (!token.getValue().equals("}")) {
113-
b.blockmembers.addElement(statement());
114-
}
76+
while (!token.getValue().equals("}"))
77+
b.blockmembers.add(statement());
11578
return b;
11679
}
11780

118-
private Assignment assignment() {
119-
Assignment a = new Assignment();
120-
if (token.getType().equals("Identifier")) {
121-
// FIXED: Use .target instead of .variable to match Program.java
122-
a.target = new Variable();
123-
a.target.id = token.getValue();
124-
81+
private Statement statement() {
82+
if (token.getValue().equals(";")) {
12583
token = input.nextToken();
126-
match(":=");
127-
a.source = expression();
128-
match(";");
129-
} else {
130-
throw new RuntimeException(SyntaxError("Identifier"));
84+
return new Skip();
13185
}
86+
if (token.getValue().equals("{")) {
87+
token = input.nextToken();
88+
Block b = statements();
89+
match("}");
90+
return b;
91+
}
92+
if (token.getValue().equals("if")) return ifStatement();
93+
if (token.getValue().equals("while")) return whileStatement();
94+
if (token.getType().equals("Identifier")) return assignment();
95+
96+
throw new RuntimeException(SyntaxError("Statement"));
97+
}
98+
99+
private Assignment assignment() {
100+
Assignment a = new Assignment();
101+
a.variable = new Variable();
102+
a.variable.id = token.getValue();
103+
token = input.nextToken();
104+
match(":=");
105+
a.source = expression();
106+
match(";");
132107
return a;
133108
}
134109

135110
private Expression expression() {
136-
Binary b;
137111
Expression e = conjunction();
138112
while (token.getValue().equals("||")) {
139-
b = new Binary();
113+
Binary b = new Binary();
140114
b.term1 = e;
141115
b.op = new Operator(token.getValue());
142116
token = input.nextToken();
@@ -147,10 +121,9 @@ private Expression expression() {
147121
}
148122

149123
private Expression conjunction() {
150-
Binary b;
151124
Expression e = relation();
152125
while (token.getValue().equals("&&")) {
153-
b = new Binary();
126+
Binary b = new Binary();
154127
b.term1 = e;
155128
b.op = new Operator(token.getValue());
156129
token = input.nextToken();
@@ -161,17 +134,9 @@ private Expression conjunction() {
161134
}
162135

163136
private Expression relation() {
164-
Binary b;
165137
Expression e = addition();
166-
// FIXED: Added support for != and <>
167-
while (token.getValue().equals("<") ||
168-
token.getValue().equals("<=") ||
169-
token.getValue().equals(">") ||
170-
token.getValue().equals(">=") ||
171-
token.getValue().equals("==") ||
172-
token.getValue().equals("!=") ||
173-
token.getValue().equals("<>")) {
174-
b = new Binary();
138+
while (token.getValue().matches("<|<=|>|>=|==|!=")) {
139+
Binary b = new Binary();
175140
b.term1 = e;
176141
b.op = new Operator(token.getValue());
177142
token = input.nextToken();
@@ -182,10 +147,9 @@ private Expression relation() {
182147
}
183148

184149
private Expression addition() {
185-
Binary b;
186150
Expression e = term();
187151
while (token.getValue().equals("+") || token.getValue().equals("-")) {
188-
b = new Binary();
152+
Binary b = new Binary();
189153
b.term1 = e;
190154
b.op = new Operator(token.getValue());
191155
token = input.nextToken();
@@ -196,58 +160,41 @@ private Expression addition() {
196160
}
197161

198162
private Expression term() {
199-
Binary b;
200-
Expression e = negation();
163+
Expression e = factor();
201164
while (token.getValue().equals("*") || token.getValue().equals("/")) {
202-
b = new Binary();
165+
Binary b = new Binary();
203166
b.term1 = e;
204167
b.op = new Operator(token.getValue());
205168
token = input.nextToken();
206-
b.term2 = negation();
169+
b.term2 = factor();
207170
e = b;
208171
}
209172
return e;
210173
}
211174

212-
private Expression negation() {
213-
Unary u;
214-
if (token.getValue().equals("!")) {
215-
u = new Unary();
216-
u.op = new Operator(token.getValue());
217-
token = input.nextToken();
218-
// FIXED: Recursive call to support stacked negation (e.g. !!true)
219-
u.term = negation();
220-
return u;
221-
} return factor();
222-
}
223-
224175
private Expression factor() {
225-
Expression e = null;
226176
if (token.getType().equals("Identifier")) {
227177
Variable v = new Variable();
228178
v.id = token.getValue();
229-
e = v;
230179
token = input.nextToken();
231-
} else if (token.getType().equals("Literal")) {
232-
Value v = null;
233-
if (isInteger(token.getValue()))
234-
v = new Value((new Integer(token.getValue())).intValue());
235-
else if (token.getValue().equals("true"))
236-
v = new Value(true);
237-
else if (token.getValue().equals("false"))
238-
v = new Value(false);
239-
else
240-
throw new RuntimeException(SyntaxError("Literal"));
241-
e = v;
180+
return v;
181+
}
182+
if (token.getType().equals("Literal")) {
183+
Value v = token.getValue().equals("true") ?
184+
new Value(true) :
185+
token.getValue().equals("false") ?
186+
new Value(false) :
187+
new Value(Integer.parseInt(token.getValue()));
242188
token = input.nextToken();
243-
} else if (token.getValue().equals("(")) {
189+
return v;
190+
}
191+
if (token.getValue().equals("(")) {
244192
token = input.nextToken();
245-
e = expression();
193+
Expression e = expression();
246194
match(")");
247-
} else {
248-
throw new RuntimeException(SyntaxError("Identifier | Literal | ("));
195+
return e;
249196
}
250-
return e;
197+
throw new RuntimeException(SyntaxError("Identifier | Literal | ("));
251198
}
252199

253200
private Conditional ifStatement() {
@@ -258,7 +205,7 @@ private Conditional ifStatement() {
258205
match(")");
259206
c.thenbranch = statement();
260207
if (token.getValue().equals("else")) {
261-
match("else");
208+
token = input.nextToken();
262209
c.elsebranch = statement();
263210
}
264211
return c;
@@ -273,12 +220,4 @@ private Loop whileStatement() {
273220
l.body = statement();
274221
return l;
275222
}
276-
277-
private boolean isInteger(String s) {
278-
boolean result = true;
279-
for (int i = 0; i < s.length(); i++)
280-
if ('0' > s.charAt(i) || '9' < s.charAt(i))
281-
result = false;
282-
return result;
283-
}
284223
}

0 commit comments

Comments
 (0)