Statements that alter the flow of control
The goto statement alters the flow of control. If we implement goto statements then we need to define a LABEL for a statement. A production can be added for this purpose:
In this production system, semantic action is attached to record the LABEL and its value in the symbol table.
Following grammar used to incorporate structure flow-of-control constructs:
Here, S is a statement, L is a statement-list, A is an assignment statement and E is a Boolean-valued expression.
Translation scheme for statement that alters flow of control
- We introduce the marker non-terminal M as in case of grammar for Boolean expression.
- This M is put before statement in both if then else. In case of while-do, we need to put M before E as we need to come back to it after executing S.
- In case of if-then-else, if we evaluate E to be true, first S will be executed.
- After this we should ensure that instead of second S, the code after the if-then else will be executed. Then we place another non-terminal marker N after first S.
The grammar is as follows:
The translation scheme for this grammar is as follows:
|S → if E then M S1
||BACKPATCH (E.TRUE, M.QUAD)
S.NEXT = MERGE (E.FALSE, S1.NEXT)
|S → if E then M1 S1 else
|BACKPATCH (E.TRUE, M1.QUAD)
BACKPATCH (E.FALSE, M2.QUAD)
S.NEXT = MERGE (S1.NEXT, N.NEXT, S2.NEXT)
|S → while M1 E do M2 S1
||BACKPATCH (S1,NEXT, M1.QUAD)
BACKPATCH (E.TRUE, M2.QUAD)
S.NEXT = E.FALSE
GEN (goto M1.QUAD)
|S → begin L end
||S.NEXT = L.NEXT
|S → A
||S.NEXT = MAKELIST ()
|L → L ; M S
||BACKPATHCH (L1.NEXT, M.QUAD)
L.NEXT = S.NEXT
|L → S
||L.NEXT = S.NEXT
|M → ∈
||M.QUAD = NEXTQUAD
||N.NEXT = MAKELIST (NEXTQUAD)