88
99// This code DOES NOT implement a parser for KAY. You have to complete
1010// the code and also make sure it implements a parser for KAY - not something
11- // else, not more, not less .
11+ // else.
1212
1313public class ConcreteSyntax {
1414
@@ -45,19 +45,17 @@ private void match(String s) {
4545 // Implementation of the Recursive Descent Parser
4646
4747 public Program program () {
48- // TODO TO BE COMPLETED
4948 // Program --> main '{' Declarations Statements '}'
50- String [] header = { };
49+ match ("main" );
50+ match ("{" );
5151 Program p = new Program ();
52- for (int i = 0 ; i < header .length ; i ++)
53- // bypass " main { "
54- match (header [i ]);
55- // add the required code
52+ p .decpart = declarations ();
53+ p .body = statements ();
54+ match ("}" );
5655 return p ;
5756 }
5857
5958 private Declarations declarations () {
60- // TODO TO BE COMPLETED
6159 // Declarations --> { Declaration }*
6260 Declarations ds = new Declarations ();
6361 while (token .getValue ().equals ("integer" )
@@ -75,15 +73,14 @@ private void declaration(Declarations ds) {
7573 }
7674
7775 private Type type () {
78- // TODO CHECK THE CODE BELOW AND CHANGE IT IF NECESSARY
7976 // Type --> integer | bool
8077 Type t = null ;
8178 if (token .getValue ().equals ("integer" ))
82- t = new Type (token . getValue () );
79+ t = new Type ("integer" );
8380 else if (token .getValue ().equals ("bool" ))
8481 t = new Type (token .getValue ());
8582 else
86- throw new RuntimeException (SyntaxError ("int | boolean " ));
83+ throw new RuntimeException (SyntaxError ("integer | bool " ));
8784 token = input .nextToken (); // pass over the type
8885 return t ;
8986 }
@@ -125,10 +122,11 @@ private Statement statement() {
125122 match ("}" );
126123 } else if (token .getValue ().equals ("if" )) // IfStatement
127124 s = ifStatement ();
128- else if (token .getValue ().equals ("while" )) { // WhileStatement
129- // TODO TO BE COMPLETED
125+ else if (token .getValue ().equals ("while" )) {
126+ // WhileStatement
127+ s = whileStatement ();
130128 } else if (token .getType ().equals ("Identifier" )) { // Assignment
131- // TODO TO BE COMPLETED
129+ s = assignment ();
132130 } else
133131 throw new RuntimeException (SyntaxError ("Statement" ));
134132 return s ;
@@ -147,7 +145,12 @@ private Assignment assignment() {
147145 // Assignment --> Identifier := Expression ;
148146 Assignment a = new Assignment ();
149147 if (token .getType ().equals ("Identifier" )) {
150- // TODO TO BE COMPLETED
148+ a .target = new Variable ();
149+ a .target .id = token .getValue ();
150+ token = input .nextToken ();
151+ match (":=" );
152+ a .source = expression ();
153+ match (";" );
151154 } else
152155 throw new RuntimeException (SyntaxError ("Identifier" ));
153156 return a ;
@@ -176,7 +179,10 @@ private Expression conjunction() {
176179 e = relation ();
177180 while (token .getValue ().equals ("&&" )) {
178181 b = new Binary ();
179- // TODO TO BE COMPLETED
182+ b .term1 = e ;
183+ b .op = new Operator (token .getValue ());
184+ token = input .nextToken ();
185+ b .term2 = relation ();
180186 e = b ;
181187 }
182188 return e ;
@@ -187,13 +193,15 @@ private Expression relation() {
187193 Binary b ;
188194 Expression e ;
189195 e = addition ();
190- // TODO TO BE CHECKED AND COMPLETED. Do we have all the operators?
191196 while (token .getValue ().equals ("<" ) || token .getValue ().equals ("<=" )
192- || token .getValue ().equals (">=" )
197+ || token .getValue ().equals (">" ) || token . getValue (). equals ( "> =" )
193198 || token .getValue ().equals ("==" )
194199 || token .getValue ().equals ("!=" )) {
195200 b = new Binary ();
196- // TODO TO BE COMPLETED
201+ b .term1 = e ;
202+ b .op = new Operator (token .getValue ());
203+ token = input .nextToken ();
204+ b .term2 = addition ();
197205 e = b ;
198206 }
199207 return e ;
@@ -205,7 +213,12 @@ private Expression addition() {
205213 Expression e ;
206214 e = term ();
207215 while (token .getValue ().equals ("+" ) || token .getValue ().equals ("-" )) {
208- // TODO TO BE COMPLETED
216+ b = new Binary ();
217+ b .term1 = e ;
218+ b .op = new Operator (token .getValue ());
219+ token = input .nextToken ();
220+ b .term2 = term ();
221+ e = b ;
209222 }
210223 return e ;
211224 }
@@ -217,7 +230,10 @@ private Expression term() {
217230 e = negation ();
218231 while (token .getValue ().equals ("*" ) || token .getValue ().equals ("/" )) {
219232 b = new Binary ();
220- // TODO TO BE COMPLETED
233+ b .term1 = e ;
234+ b .op = new Operator (token .getValue ());
235+ token = input .nextToken ();
236+ b .term2 = negation ();
221237 e = b ;
222238 }
223239 return e ;
@@ -237,7 +253,6 @@ private Expression negation() {
237253 }
238254
239255 private Expression factor () {
240- // TODO CHECK THE CODE BELOW
241256 // Factor --> Identifier | Literal | ( Expression )
242257 Expression e = null ;
243258 if (token .getType ().equals ("Identifier" )) {
@@ -248,7 +263,7 @@ private Expression factor() {
248263 } else if (token .getType ().equals ("Literal" )) {
249264 Value v = null ;
250265 if (isInteger (token .getValue ()))
251- v = new Value (( new Integer (token .getValue ())). intValue ( ));
266+ v = new Value (Integer . valueOf (token .getValue ()));
252267 else if (token .getValue ().equals ("True" ))
253268 v = new Value (true );
254269 else if (token .getValue ().equals ("False" ))
@@ -269,14 +284,28 @@ else if (token.getValue().equals("False"))
269284 private Conditional ifStatement () {
270285 // IfStatement --> if ( Expression ) Statement { else Statement }opt
271286 Conditional c = new Conditional ();
272- // TODO TO BE COMPLETED
287+ match ("if" );
288+ match ("(" );
289+ c .test = expression ();
290+ match (")" );
291+ c .thenbranch = statement ();
292+ if (token .getValue ().equals ("else" )) {
293+ token = input .nextToken ();
294+ c .elsebranch = statement ();
295+ } else {
296+ c .elsebranch = null ;
297+ }
273298 return c ;
274299 }
275300
276301 private Loop whileStatement () {
277302 // WhileStatement --> while ( Expression ) Statement
278303 Loop l = new Loop ();
279- // TODO TO BE COMPLETED
304+ match ("while" );
305+ match ("(" );
306+ l .test = expression ();
307+ match (")" );
308+ l .body = statement ();
280309 return l ;
281310 }
282311
@@ -287,4 +316,4 @@ private boolean isInteger(String s) {
287316 result = false ;
288317 return result ;
289318 }
290- }
319+ }
0 commit comments