Header Ads Widget

Statements that alter the flow of control

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:

  1. S →     LABEL : S  
  2.     LABEL →     id  

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:

  1. S →  if E then S  
  2.      S  →   if E then S else S  
  3.      S →    while E do S  
  4.      S →    begin L end  
  5.      S→     A  
  6.      L→    L ; S  
  7.      L →   S  

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:

  1. S →   if E then M S  
  2.      S →    if E then M S else M S  
  3.      S →    while M E do M S  
  4.      S →    begin L end  
  5.      S →    A  
  6.      L→    L ; M S  
  7.      L →   S  
  8.      M →    ∈  
  9.      N →    ∈  

The translation scheme for this grammar is as follows:

Production ruleSemantic actions
S → if E then M S1BACKPATCH (E.TRUE, M.QUAD)
S.NEXT = MERGE (E.FALSE, S1.NEXT)
S → if E then M1 S1 else
M2 S2
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 S1BACKPATCH (S1,NEXT, M1.QUAD)
BACKPATCH (E.TRUE, M2.QUAD)
S.NEXT = E.FALSE
GEN (goto M1.QUAD)
S → begin L endS.NEXT = L.NEXT
S → AS.NEXT = MAKELIST ()
L → L ; M SBACKPATHCH (L1.NEXT, M.QUAD)
L.NEXT = S.NEXT
L → SL.NEXT = S.NEXT
M → ∈M.QUAD = NEXTQUAD
N→ ∈N.NEXT = MAKELIST (NEXTQUAD)
GEN (goto_)

Post a Comment

0 Comments