HW3

Due: April 1, 2003 (Before class time)
[100 points]

Write the YACC/LEX (or JavaCC) programs as per the specifications below. For this Homework, e-mail the submission to kalathur@bu.edu

Process a sequence of assignment statements of the form:

  <var> = <expression>

Whenever a variable (i.e., the identifier) is assigned a value, the value should be stored in the symbol table structure you developed in the previous homework. When a variable name occurs on the right hand side of an assignment, you should lookup the corresponding value from the symbol table.

Use the ambiguous grammar for expressions and use the precedence and associative properties of the operators (required: +, -, *, /  optional: mod %, exp ^  )

 

Sample Input:

  count = 0;
  result = 10;
  count = count + 1;
  result = result * count + 5;
  sum = 20.0;
  sum = sum + count + result;
  write result;
  write sum;
  write count

Corresponding Sample Output:

   Value of result is 15.0
   Value of sum is 36.0
   Value of count is 1.0
   

Running the Yacc program

  1. Write the Yacc specifications in a file, say, hw3.y
  1. Write the corresponding Lex specifications  in a file, say, hw3.l
  2. On Unix systems, do the following:
       % yacc -d hw3.y                (Produces y.tab.h and y.tab.c as output)
       % lex hw3.l                    (Produces lex.yy.c as the output)
       % cc y.tab.c lex.yy.c -ly -ll              (Compile the C files and link with Yacc & Lex library)
       % a.out                        (Run the executable, and type the input, followed by )
                         or, if the input is in a file, say, hw3.in, then do the following:
       % a.out < hw3.in

Now, add the IF-THEN or the IF-THEN-ELSE statement. You can assume no nesting of the IF statements. Keep track if you are processing an IF-statement, and if so, the value of the test.

Sample Input:

  count = 2;
  result = 10;
  temp = 5;
  if (temp > 10) 
    result = result + count;
  else
    result = result - count;
  write result;
  write count

Corresponding Sample Output:

   Value of result is 8.0
   Value of count is 2.0