edited by
55,040 views
100 100 votes

In a bottom-up evaluation of a syntax directed definition, inherited attributes can

  1. always be evaluated
  2. be evaluated only if the definition is L-attributed
  3. be evaluated only if the definition has synthesized attributes
  4. never be evaluated

12 Answers

Best answer
56 56 votes

In the context of syntax-directed definitions (SDDs) and their evaluation during parsing, the question revolves around the evaluation of inherited attributes in a bottom-up parsing approach. Let's delve into the specifics:

Syntax-Directed Definitions (SDDs)

  1. Synthesized Attributes: These are attributes of a non-terminal that are computed from the attributes of its children in the parse tree. They naturally fit the bottom-up parsing model since they are evaluated after the children are processed.
  1. Inherited Attributes: These are attributes of a non-terminal that are computed from the attributes of its parent or siblings. Evaluating these in a bottom-up manner can be challenging because they often rely on information that is not available until after the non-terminal is processed.

L-Attributed Definitions

An SDD is termed L-attributed if each inherited attribute of a non-terminal can be computed using the attributes of its siblings to the left and its parent. This structure allows for a left-to-right evaluation that aligns with top-down parsing strategies, such as LL parsing.

Bottom-Up Parsing

In bottom-up parsing, such as LR parsing, the parse tree is constructed from the leaves (tokens) to the root. This approach is inherently suitable for evaluating synthesized attributes, as each node's attributes can be computed from its children as they are reduced.

Evaluation of Inherited Attributes in Bottom-Up Parsing

  • Always Be Evaluated: This is incorrect because inherited attributes depend on context that may not be available in a bottom-up approach unless specific conditions are met.
  • Evaluated Only If the Definition is L-Attributed: This is the correct answer. L-attributed definitions allow inherited attributes to be evaluated in a way that can be adapted to bottom-up parsing, provided that the dependencies are such that they can be resolved during the reduction process.
  • Evaluated Only If the Definition Has Synthesized Attributes: This is incorrect because synthesized attributes do not directly affect the evaluation of inherited attributes. They are evaluated independently in a bottom-up manner.
  • Never Be Evaluated: This is incorrect because there are cases, specifically with L-attributed grammars, where inherited attributes can indeed be evaluated in a bottom-up parser.

Conclusion

In a bottom-up evaluation, inherited attributes can be evaluated only if the syntax-directed definition is L-attributed. This is because L-attributed grammars ensure that the dependencies needed for computing inherited attributes can be resolved in a manner compatible with the bottom-up parsing process. However, it is important to note that not all L-attributed grammars can be efficiently parsed bottom-up, but if an SDD allows for such parsing, it is necessarily L-attributed.

 

Ref: http://infolab.stanford.edu/~ullman/dragon/slides2.pdf
https://gateoverflow.in/?qa=blob&qa_blobid=14587629398289520039

A nice PDF for the same :- https://acm.sjtu.edu.cn/w/images/a/a1/Compiler2013-lec07.pdf

edited by
14 14 votes

Ans D

Why ?

A) inherited attributes can have cyclic dependencis. Due to which we can not be sure whether they can be evaluated in First Place.

So A is wrong.

B) This is wrong because even defination is is L-attributed, we need to go top down, left to right. We can not do standard bottom up Traversal.

example :-

T' -> *FT1' | T1'.inh = T'.inh * F.val -> This move is allowed in L attributed, which can not be computed using bottom up traversal. We need to go from left to right, top down. So B is out of question.

reference :- https://en.wikipedia.org/wiki/L-attributed_grammar

L-attributed grammars are a special type of attribute grammars. They allow the attributes to be evaluated in one depth-first left-to-right traversal of the abstract syntax tree. As a result, attribute evaluation in L-attributed grammars can be incorporated conveniently in top-down parsing.

A syntax-directed definition is L-attributed if each inherited attribute of Xj on the right side of

A → X1 X2 … Xn

depends only on

1.the attributes of the symbols X1, X2, …, Xj-1

2.the inherited attributes of A // This is why B is false.

C) This is wrong because it says " definition has synthesized attributes". So along with Synthesized attributes, I can even have cycles. Which makes this wrong..

So answer is One and only D. Never !

6 6 votes
Ans:C

lets see how ,consider following SDD:

A->BC   B.i =A.i 

B->b  B.s=b.val 

C->c  C.s=c.val

if u draw parse tree

  A

B  C

b   c

now ur traversing bottom up

first you 'll evaluate B.s then you want to evaluate B.i    but as we can see as i is an inherited attribute and it is dependent on A.i , but we have'nt evaluated A.i yet coz we are doing a bottom up traversal and we havent seen A yet . so it cannot be evaluated due to Inherited attribute i.

 

now if  A=BC  A.s=B.s+C.s

B->b  B.s=b.val 

C->c  C.s=c.val

see that

  A

B  C

b   c

you 'll traverse bottom up ,first eval B.s=b then C.s=c

then when you are at A ,you have already evaluated all the attributes on which A.s is dependent..so Only in this case where SDD has Synthesized attributes ,then only Bottom up Evaluation can be used.

 for Inherited attributes  we will need  a depth first evaluation.

6 6 votes

I’ll try to answer this. Let me know if there are any conflicts in this answer.

As every S attributed is L attributed, so if L attributed is not possible, then even S attributed is not possible. S -attributed definitions use synthesized attributes. L-attributed definitions use both inherited and synthesized attributes. S-attributed can be parsed by BUP, post order traversal, traversal of the parse-tree as S-attributes are computed from child nodes. Therefore, if the grammar is L attributed then there are possibilities that we can solve using BUP (provided it is S attributed too), while not all L attributed are solvable using BUP (as not all L attributed are S attributed). 

S-attributed uses synthesized attributes that can be traversed by BUP (True) and some L attributed definitions uses inherited (which cannot be traversed by BUP) and synthesized attributes (which can be traversed by BUP). So, option B seems to be the correct answer.

Some more info : 

  1. Inherited attributes cannot be always evaluated by a Pre-Order traversal of the parse-tree. 
  2. Inherited attributes that do not depend on the right children can be evaluated by a Pre-Order traversal.

Ref. http://cse.iitkgp.ac.in/~bivasm/notes/SDD.pdf

edited by
5 5 votes
Ans: B

In bottom-up parser inherited from the parent isn't possible.but inherited from the left child is possible in L-attributed
0 0 votes
Option b.
edited by
Answer:
Position:
Show:

Related questions

61 61 votes
5 answers 5 answers
14.5k
14.5k views
Kathleen asked Sep 17, 2014
14,523 views
A program consists of two modules executed sequentially. Let $f_1(t)$ and $f_2(t)$ respectively denote the probability density functions of time taken to execute the two ...
44 44 votes
5 answers 5 answers
13.0k
13.0k views
Kathleen asked Sep 17, 2014
12,982 views
Consider the syntax directed definition shown below.$$\begin{array}{ll}S \rightarrow \mathbf{ id :=} E&\qquad \{gen(\mathbf{ id}.place = E.place;);\}\\E \rightarrow E_1 +...
54 54 votes
5 answers 5 answers
21.9k
21.9k views
go_editor asked Apr 24, 2016
21,871 views
The following program fragment is written in a programming language that allows global variables and does not allow nested declarations of functions.global int i=100, j=5...
51 51 votes
5 answers 5 answers
19.5k
19.5k views
Kathleen asked Sep 17, 2014
19,528 views
Consider the translation scheme shown below.$S \rightarrow T\;R$$R \rightarrow + T \{\text{print}(‘+’);\} R\mid \varepsilon$$T \rightarrow$ num $\{\text{print}$(num.val)...