edited by
2,068 views
8 votes
8 votes
The syntax of the repeat-until statement is given by the following grammar

$S \rightarrow\text{ repeat }S_1\text{ until }E$

where E stands for expressions, $S$ and $S_1$ stand for statements. The non-terminals $S$ and $S_1$ have an attribute code that represents generated code. The non-terminal E has two attributes. The attribute code represents generated code to evaluate the expression and store its value in a  distinct variable, and the attribute varName contains the name of the variable in which the truth value is stored. The truth-value stored in the variable is 1 if E is true, 0 if E is false.

Give a syntax-directed definition to generate three-address code for the repeat-until statement. Assume that you can call a function newlabel() that returns a distinct label for a statement. Use the operator '\\' to concatenate two strings and the function gen(s) to generate a line containing the string s.
edited by

1 Answer

Best answer
8 votes
8 votes

The desired code sequence is 

S.begin:
    S1.code;
    E.code;
    if(E.varName = 1) goto S.next;
    goto S.begin;
S.next:

The following syntax-directed translation can achieve this (As mentioned in question "\\" is used to concatenate two strings)

  • S.begin := newlabel()
  • S.next := newlabel()
  • S.code := gen(S.begin:) \\ S1.code \\ E.code \\ gen(if(E.varName = 1 goto S.next)) \\ gen(goto S.begin) \\ gen(S.next:)
selected by

Related questions

36 votes
36 votes
2 answers
2
Kathleen asked Sep 14, 2014
12,613 views
The process of assigning load addresses to the various parts of the program and adjusting the code and the data in the program to reflect the assigned addresses is called...
37 votes
37 votes
6 answers
3
Kathleen asked Sep 14, 2014
18,079 views
Which of the following statements is false?An unambiguous grammar has same leftmost and rightmost derivationAn LL(1) parser is a top-down parserLALR is more powerful than...
20 votes
20 votes
2 answers
4
Kathleen asked Sep 14, 2014
3,654 views
Remove left-recursion from the following grammar: $S \rightarrow Sa \mid Sb \mid a \mid b$Consider the following grammar: $S \rightarrow aSbS\mid bSaS \mid �...