Intra-function code generation
Description
Modify the parser to generate assembly-like code for each function in a
input program. The specific requirements are as follows.
-
As in previous projects, your parser should ignore (but copy) "meta-statements"
to the output program.
-
The output program uses the same grammar as the input program with the
following modifications. (This is a precise grammar defintion.
But you do not need to worry about parsing this grammar since you are not
going to compile the output program.)
-
Grammar modification for data declaration:
-
No data declaration is allowed except one array at program level and one
array inside each function.
-
Grammar modification for assignment and expression:
-
<assignment> --> <id> equal_sign <operand> <operation>
<operand> semicolon | <id> equal_sign <operand> semicolon
-
<operand> --> NUMBER | <id>
-
Change all mentioning of <expression> with <operand>, as in <id>,
read/write statement, <func call>, <return>, and <condition>.
-
Now <id>s are array references.
-
Grammar modification for control flow:
-
<if-statement> --> if left_parenthesis <operand> <comparison
op> <operand> right_parenthesis goto LABEL semicolon
-
<goto-statement> --> goto LABEL semicolon
-
<label-statement> --> LABEL colon semicolon
-
LABEL is just another name for ID token
-
Do not use the previous definition of <if-statement>. Do not use
<while-statement>, <break>, and <continue>.
-
Grammar modification for functions:
-
Function declaration and definition remain unchanged.
-
In <func call>, each parameter must be <id> instead of <experssion>.
-
In <return>, use <id> instead of <expression>.
-
The output program should be compilable by a C compiler and have the same
behavior as the input program.