1. What is the purpose of attribute grammars in compiler design?
A) To generate code for a program
B) To define the syntax of a programming language
C) To specify the semantic rules for a language
D) To optimize the code generated by a compiler
Answer: C) To specify the semantic rules for a language
2. In attribute grammars, what are attributes used for?
A) To define variables
B) To specify type information and other semantics
C) To define the syntactic structure of a language
D) To store the machine code of the program
Answer: B) To specify type information and other semantics
3. What are the two types of attributes in attribute grammars?
A) Static and dynamic
B) Synthesized and inherited
C) Direct and indirect
D) Local and global
Answer: B) Synthesized and inherited
4. Which of the following is an example of a synthesized attribute?
A) The value of a variable at runtime
B) The type of an expression in an expression tree
C) The scope of a variable
D) The precedence of an operator
Answer: B) The type of an expression in an expression tree
5. In attribute grammars, where do synthesized attributes typically flow?
A) From a parent node to its children
B) From a child node to its parent
C) Within a single production rule
D) Between unrelated non-terminal symbols
Answer: B) From a child node to its parent
6. Which of the following describes an inherited attribute?
A) It is computed from the values of its parent and siblings.
B) It is computed by traversing the tree from the leaves.
C) It is propagated from leaf nodes to the root.
D) It is used to represent the intermediate code during code generation.
Answer: A) It is computed from the values of its parent and siblings.
7. Which of the following operations can attribute grammars perform?
A) Syntactic analysis
B) Code generation
C) Semantic analysis
D) Memory management
Answer: C) Semantic analysis
8. In an attribute grammar, what is a semantic rule?
A) A rule that defines how a production is applied
B) A rule that specifies how attributes are computed
C) A rule that specifies the syntax of a language
D) A rule for generating machine code
Answer: B) A rule that specifies how attributes are computed
9. Which of the following is an example of a synthesized attribute for an expression in an abstract syntax tree?
A) The type of the left operand
B) The value of the expression after evaluation
C) The precedence of the operator
D) The position of the expression in the source code
Answer: B) The value of the expression after evaluation
10. In an attribute grammar, what is the typical direction of attribute flow for synthesized attributes?
A) Upwards, from the leaves to the root
B) Downwards, from the root to the leaves
C) Left to right, across the production rules
D) Right to left, across the production rules
Answer: B) Downwards, from the root to the leaves
11. Which of the following best describes the main difference between synthesized and inherited attributes?
A) Synthesized attributes depend on parent nodes, while inherited attributes depend on child nodes.
B) Synthesized attributes are computed at the leaf level, while inherited attributes are computed at the root level.
C) Synthesized attributes are passed up the tree, while inherited attributes are passed down the tree.
D) There is no difference between synthesized and inherited attributes.
Answer: C) Synthesized attributes are passed up the tree, while inherited attributes are passed down the tree.
12. Which of the following is NOT a valid example of an inherited attribute?
A) The type of a variable passed to a function
B) The scope of a variable defined in a function
C) The value of a variable in an expression
D) The class of a node in an inheritance hierarchy
Answer: C) The value of a variable in an expression
13. How are attribute grammars typically used in compilers?
A) For defining the syntax of a language
B) For providing semantic rules during code generation
C) For performing lexical analysis
D) For analyzing runtime behavior
Answer: B) For providing semantic rules during code generation
14. Which of the following is true about the attribute grammar formalism?
A) It cannot handle syntax-directed translation
B) It allows both syntax and semantics to be described together
C) It only supports semantic analysis for simple expressions
D) It is limited to defining syntax rules
Answer: B) It allows both syntax and semantics to be described together
15. In attribute grammars, which method is used to propagate inherited attributes?
A) Top-down propagation
B) Bottom-up propagation
C) Left-to-right traversal
D) Right-to-left traversal
Answer: A) Top-down propagation