Skip to content

Commit 17b494a

Browse files
authored
Update ConcreteSyntax.java
1 parent 5b73af3 commit 17b494a

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

src/main/java/com/scanner/project/ConcreteSyntax.java

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package com.scanner.project;
21
// ConcreteSyntax.java
32

43
// Implementation of the Recursive Descent Parser algorithm
@@ -10,6 +9,8 @@
109
// the code and also make sure it implements a parser for KAY - not something
1110
// else.
1211

12+
import java.beans.Expression;
13+
1314
public class ConcreteSyntax {
1415

1516
// READ THE COMPLETE FILE FIRST
@@ -46,22 +47,19 @@ private void match(String s) {
4647

4748
public Program program() {
4849
// Program --> main '{' Declarations Statements '}'
49-
String[] header = { "main", "{" };
5050
Program p = new Program();
51-
for (int i = 0; i < header.length; i++)
52-
// bypass " main { "
53-
match(header[i]);
51+
match("main");
52+
match("{");
5453
p.decpart = declarations();
5554
p.body = statements();
5655
match("}");
5756
return p;
5857
}
5958

6059
private Declarations declarations() {
61-
// TODO TO BE COMPLETED
6260
// Declarations --> { Declaration }*
6361
Declarations ds = new Declarations();
64-
while (token.getValue().equals("int")
62+
while (token.getValue().equals("integer")
6563
|| token.getValue().equals("bool")) {
6664
declaration(ds);
6765
}
@@ -76,15 +74,14 @@ private void declaration(Declarations ds) {
7674
}
7775

7876
private Type type() {
79-
// TODO TO BE COMPLETED
8077
// Type --> integer | bool
81-
Type t = null;
82-
if (token.getValue().equals("int"))
83-
t = new Type(token.getValue());
78+
Type t;
79+
if (token.getValue().equals("integer"))
80+
t = new Type("integer");
8481
else if (token.getValue().equals("bool"))
85-
t = new Type(token.getValue());
82+
t = new Type("bool");
8683
else
87-
throw new RuntimeException(SyntaxError("int | boolean"));
84+
throw new RuntimeException(SyntaxError("integer | bool"));
8885
token = input.nextToken(); // pass over the type
8986
return t;
9087
}
@@ -124,15 +121,15 @@ private Statement statement() {
124121
token = input.nextToken();
125122
s = statements();
126123
match("}");
127-
} else if (token.getValue().equals("if")) // IfStatement
124+
} else if (token.getValue().equals("if")) {// IfStatement
128125
s = ifStatement();
129-
else if (token.getValue().equals("while")) {
130-
// WhileStatement
126+
} else if (token.getValue().equals("while")) {
131127
s = whileStatement();
132128
} else if (token.getType().equals("Identifier")) { // Assignment
133129
s = assignment();
134-
} else
130+
} else {
135131
throw new RuntimeException(SyntaxError("Statement"));
132+
}
136133
return s;
137134
}
138135

@@ -151,20 +148,21 @@ private Assignment assignment() {
151148
if (token.getType().equals("Identifier")) {
152149
a.variable = new Variable();
153150
a.variable.id = token.getValue();
151+
154152
token = input.nextToken();
155153
match(":=");
156154
a.source = expression();
157155
match(";");
158-
} else
156+
} else {
159157
throw new RuntimeException(SyntaxError("Identifier"));
158+
}
160159
return a;
161160
}
162161

163162
private Expression expression() {
164163
// Expression --> Conjunction { || Conjunction }*
165164
Binary b;
166-
Expression e;
167-
e = conjunction();
165+
Expression e = conjunction();
168166
while (token.getValue().equals("||")) {
169167
b = new Binary();
170168
b.term1 = e;
@@ -179,8 +177,7 @@ private Expression expression() {
179177
private Expression conjunction() {
180178
// Conjunction --> Relation { && Relation }*
181179
Binary b;
182-
Expression e;
183-
e = relation();
180+
Expression e = relation();
184181
while (token.getValue().equals("&&")) {
185182
b = new Binary();
186183
b.term1 = e;
@@ -195,16 +192,17 @@ private Expression conjunction() {
195192
private Expression relation() {
196193
// Relation --> Addition [ < | <= | > | >= | == | <> ] Addition }*
197194
Binary b;
198-
Expression e;
199-
e = addition();
200-
while (token.getValue().equals("<") || token.getValue().equals("<=")
201-
|| token.getValue().equals(">=")
202-
|| token.getValue().equals("==")
203-
|| token.getValue().equals("<>")) {
195+
Expression e = addition();
196+
while (token.getValue().equals("<") ||
197+
token.getValue().equals("<=") ||
198+
token.getValue().equals(">") ||
199+
token.getValue().equals(">=") ||
200+
token.getValue().equals("==") ||
201+
token.getValue().equals("!=")) {
204202
b = new Binary();
205-
b.term1 = e;
203+
b.term1 = e;
206204
b.op = new Operator(token.getValue());
207-
token = input.nextToken();
205+
token = input.nextToken();
208206
b.term2 = addition();
209207
e = b;
210208
}
@@ -214,8 +212,7 @@ private Expression relation() {
214212
private Expression addition() {
215213
// Addition --> Term { [ + | - ] Term }*
216214
Binary b;
217-
Expression e;
218-
e = term();
215+
Expression e = term();
219216
while (token.getValue().equals("+") || token.getValue().equals("-")) {
220217
b = new Binary();
221218
b.term1 = e;
@@ -230,8 +227,7 @@ private Expression addition() {
230227
private Expression term() {
231228
// Term --> Negation { [ '*' | / ] Negation }*
232229
Binary b;
233-
Expression e;
234-
e = negation();
230+
Expression e = negation();
235231
while (token.getValue().equals("*") || token.getValue().equals("/")) {
236232
b = new Binary();
237233
b.term1 = e;
@@ -252,8 +248,7 @@ private Expression negation() {
252248
token = input.nextToken();
253249
u.term = factor();
254250
return u;
255-
} else
256-
return factor();
251+
} return factor();
257252
}
258253

259254
private Expression factor() {
@@ -280,8 +275,9 @@ else if (token.getValue().equals("false"))
280275
token = input.nextToken();
281276
e = expression();
282277
match(")");
283-
} else
278+
} else {
284279
throw new RuntimeException(SyntaxError("Identifier | Literal | ("));
280+
}
285281
return e;
286282
}
287283

0 commit comments

Comments
 (0)