Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let , and be the placeholders for the non-terminals or in the following table:
Which one of the following are appropriate choices for and ?
View worked solution
Answer: A
To determine the correct placeholders, let us analyze how type information flows in a variable declaration grammar using Syntax-Directed Translation (SDT).
1. Analysis of
- The non-terminal synthesizes the type (either
intorfloat) via the rules and . - This type information must be passed down (inherited) to the list of identifiers .
- Therefore, the semantic action must assign the type of to :
- Comparing this with the given action , we get:
2. Analysis of
- The parent list has already inherited the type attribute in .
- This type must be propagated down to the sublist so that the identifiers inside can also be declared with the same type.
- Therefore, the inherited type of is obtained from :
- Comparing this with the given action , we get:
Conclusion
Matching our findings with the options:
This corresponds exactly to Option A.
Free practice · Your answers on this page are not saved.
