Here are some things to think about when implementing goto statements.

 

You will add the following productions to your grammar:

 

stmt:     ID ‘:’ stmt

            | goto ID

 

1.         Note that you are going to expect ID to have UNDEF stored in its type field.  If you find S_INT or S_FLOAT, you should emit an error such as “Invalid Label Type”.

 

2.  You need to consider the following two cases:

 

a. If ID ‘:’ stmt comes before goto ID in your program

 

The semantic action for ID ‘:’ stmt should store progp in a new field you add to ID ($1->br_addr=progp)

The semantic action for goto ID should emit code2(branch, (Inst)$2->br_addr);

 

b.  If goto ID comes before ID ‘:’ stmt in your program

                       

The semantic action for goto ID should emit code2(branch, STOP); and save address of STOP in a new field in you add to ID ($2->fixup_addr=progp-1

The semantic action for ID ‘:’ stmt should fix-up the branch emitted at the goto.  For example: ($1->fixup_addr)[0]=$1->br_addr;

 

c.  You are just inhumanly cool if you can figure out how to implement:

 

goto a;

…

goto a;

…

goto a;

…

a: