CS230

Compiler Design


Project Assignment #3

Enhancement

  Enhance your compiler to scan and parse programs described by the grammar of Assignment #1 with the addition of the following productions (some productions being repeated only for context, but all the ones from assignment 1 should still be present):

<block> → { <declarations> <optional_statements>? }

<declarations>  →  (<declaration>)*

<declaration  →  <type> <identifier_list>;

<identifier_list>  →  <id>  ( , <id> ) *

<type>  →   int  |  bool

<statement>   →   <variable> <assignop> <expression>
                              | <block>
                              | if (<expression>) <statement>  <else_clause>
                              | while (<expression>) <statement>

<else_clause>  →  ( else <statement> )?

<expression>   →  <simple_expression> <relopclause>
<relopclause>   →  <relop> <simple_expression> | ε

<simple_expression>  →   <term> ( <addop> <term> ) *

<term> →   <factor> (<mulop> <factor>) *
                   | <addop> <term>

<factor>  →   <id> |  (<expression>) |  <num>  |  ! <factor> 

<sign>  →  + | -

<relop>  →   ==  |  !=   |  <  |  <=  |  >  |  >=

<mulop>  →   *   |  /  | % |    &&

<addop>  →   + | -  |  ||


Run your program on the following inputs:

(a)
void main()
{
   int i, j;
   bool variable;
   
   if (i > j)
    i = i + j;
   else if (variable)
    if (i <= j)
     i = 0;
    else
     j = 0;
}

(b)
void main()
{
   int i, j ;
   bool notDone ;
   while( (i < j) && NotDone ) {
    k = k  + 1;
    while  (i == j)   i = i + 2;
   }
}.

(c) One of your own choosing.

Submission location is here.


Back to CS 230 Home Page