Starting from:

$30

PRL - Assignment_1 - Solved

  Acquaint yourself with flex and bison.   

Specifications 
You will extend calc.l and calc.y  to parse programs whose syntax is defined below.

Prog à main() {Stmts}  

Stmts à ε | Stmt; Stmts  

Stmt à Id = E | Id *= E | Id += E | print E  

E à Integer | Id | E * E | E + E | (-Integer)

Integer à digit+

 

Prog is a program that contains one main function.

Stmts is empty or a sequence of statements separated using ;

 

Integer is either a positive integer, or a negative integer enclosed in “(“ and “)”

Id is an identifier, which is a sequence of one or more lower-case letters or digits.  In addition, Id should start with a lower-case letters.  For example, x, x1, xy are identifiers, but 1x and A are not.   

Expression E is an integer, an identifier, or an infix arithmetic expression with operators "*" and "+". These two operators are left associative (e.g., 1 + 2 + 3 is equivalent to (1 + 2) + 3). "*" has higher precedence than “+”.   

Id = E assigns the value of an expression E to the variable Id, Id *= E assigns Id * E to Id, and Id += E assigns Id + E to Id.  println E outputs the value of the expression E.  Assume that Id is already assigned a value prior to the execution of Id *= E and Id += E.

If there is any syntax error, you are expected to interpret the program until the statement where you find the error. Also, your error message must contain the line number where the error was found. 

 

Tokens  may be separated by any number of white spaces, tabs, or new lines.   

 

Compile your program: 

 flex –l calc.l bison -dv calc.y

gcc -o calc calc.tab.c lex.yy.c –lfl

 

 

Execution (example):. 

./calc < input 

Where input is the name of the input file 

  

Example Programs (Note that the test cases used in grading may be different from the examples) 

   

Program 1:

                     main() {x = 3;}

                                 

Output:

                 

   

Program 2:

                     main() {x = 3; print x;}

 

Output:  

                     3

   

Program 3:

                     main() {x = 3;  x = 5 + 4; print x;}

 

Output:

                     9

 

Program 4:

                     main() {x = 3* 4; print x;}

 

Output:

                     12

 

   

Program 5:

                     main() {print 1 + 2 + 4;}

 

Output:

                     7

 

   

Program 6:

                     main() {x = 0; x = 1-2; }

                 

Output:

                Lexical error: -     Parsing error: line 1

                 

   

Program 7:

                main() {                   1x = 3;

                     }

 

Output:

                     Lexical error: 1x

                     Parsing error: line 2

 

 

Program 8: 

                     main() {

                 
x = 3;
                     print          

                     }

 

Output:

                     3

 
   x;
 

Program 9: 

 

                    x = 0;

 

Output: 

        Parsing error: line 1

 
 
 

Program 10:

                     main() {x = 3;  x += 5 + 4; print x;}

 

Output:

                     12

 

Program 11:

                     main() {x = 3;  x = (-5) + 3; print x;}

 

Output:

                     -2

 

Program 12:

                     main() {x = 3;  y = 2; print x; print y;}

 

Output:

                     3

                     2

More products