Syntax-Directed-Translation

Question 1
 Consider the following grammar (that admits a series of declarations, followed by expressions) and the associated syntax directed translation (SDT) actions, given as pseudo-code:
P ⟶ D* E*
D ⟶ int ID {record that ID.lexeme is of type int}
D ⟶ bool ID {record that ID.lexeme is of type bool}
E ⟶ E1 + E2 {check that E1.type = E2.type = int; set E.type := int}
E ⟶ !E1 {check that E1.type = bool; set E.type := bool}
E ⟶ ID {set E.type := int}
With respect to the above grammar, which one of the following choices is correct?
A
The actions can be used to type-check syntactically correct integer variable declarations and integer expressions.
B
The actions will lead to an infinite loop.
C
The actions can be used to type-check syntactically correct boolean variable declarations and boolean expressions.
D
The actions can be used to correctly type-check any syntactically correct program.
Question 1 Explanation: 
This SDT will never lead to infinite loop so option 2 is false. This SDT is type checking for bool as well as integer variables, hence this SDT can be used to correctly type-check any syntactically correct program involving boolean and integer variables.
Question 2

Suppose we have a computer with a single register and only three instructions given below:

 LOAD addren     ; load register
                 ; from addren
 STORE addren    ; store register
                 ; at addren
 ADD addren      ; add register to
                 ; contents of addren
                 ; and place the result
                 ; in the register 

Consider the following grammar:

   A → id :=E       E → E + T|T        T → (E)|id 

Write a syntax directed translation to generate code using this grammar for the computer described above.

A
Theory Explanation.
Question 3

Consider the syntax-directed translation schema (SDTS) shown below:

    E → E + E  {print “+”}
    E → E ∗ E  {print “.”}
    E → id     {print id.name}
    E → (E) 

An LR-parser executes the actions associated with the productions immediately after a reduction by the corresponding production. Draw the parse tree and write the translation for the sentence.

(a+b)∗(c+d), using the SDTS given above. 
A
Theory Explanation.
There are 3 questions to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access