Documente Academic
Documente Profesional
Documente Cultură
We learnt the flex and bison tools which generate program segments that
can read the source program and discover its structure. The translation
rules in our lex script recognizes all the symbols in our input and returns
the corresponding token(s). The lex script also de nes regular expression
for the whitespaces and takes no action when the whitespaces are found in
the input.
Constructing the Abstract Syntax Tree
For the turn(arg) operation, where 'arg' is the expression passed to turn
function as an argument, We have to first compute the matrix for 'arg', and
then rotate it 90 degrees clockwise to obtain the resulting matrix. Note that
each element of the matrix for 'arg' will be rotated by 90 degrees in
addition to the 90 degrees clockwise orientation of the whole matrix.
For the sew(arg1, arg2) operation, where 'arg1' and 'arg2' are the
expressions passed to sew function as arguments. We have to compute the
matrices for 'arg1' and 'arg2' and then combine these matrices to obtain the
resulting matrix. Note that the number of rows in the matrix for 'arg1' and
the matrix for 'arg2' must be same.
The way we implemented the evaluation mechanism is by first creating a
virtual function called evaluate() in our base class for Expression, so that
each of its subclasses must implement this evaluate() function to compute
their corresponding matrix.
For generating the equivalent C++ code for a given AST, we first tried to
create a global variable which was a vector of strings to act as a stack of
function calls and to store the expressions which are assigned to temporary
variables (of type 'Quilt'). We declared a global variable to keep track of
the total number of temporary variables. And then we created a print
function to print the C++ code using the stack (a vector of strings) and the
temporary variable counter. We also use Quadruple in this.
This allows us to define our own variables and functions and use them in
our expressions. The main challenge was to find the scope of the variable
or the function in the expression. This is done by creating a stack of
symbol tables and pushing a symbol table for every LET expression and
popping off that symbol table from the stack after the end of let
expression.