The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+5 votes
436 views
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.
in Compiler Design by Veteran (52.1k points)
edited by | 436 views
+1
No one was able to answer this question?

1 Answer

+2 votes
Best answer

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:)

Reference: https://www.cse.iitk.ac.in/users/karkare/cs335/lectures/16IRGeneration.pdf 

by Veteran (416k points)
selected by

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,807 questions
54,727 answers
189,302 comments
79,845 users