Attribute Grammars MCQs January 8, 2026December 14, 2024 by u930973931_answers 15 min Score: 0 Attempted: 0/15 Subscribe 1. What is the purpose of attribute grammars in compiler design? (A) To specify the semantic rules for a language (B) To define the syntax of a programming language (C) To generate code for a program (D) To optimize the code generated by a compiler 2. In attribute grammars, what are attributes used for? (A) To define variables (B) To store the machine code of the program (C) To define the syntactic structure of a language (D) To specify type information and other semantics 3. What are the two types of attributes in attribute grammars? (A) Synthesized and inherited (B) Static and dynamic (C) Direct and indirect (D) Local and global 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 5. In attribute grammars, where do synthesized attributes typically flow? (A) From a child node to its parent (B) From a parent node to its children (C) Within a single production rule (D) Between unrelated non-terminal symbols 6. Which of the following describes an inherited attribute? (A) It is used to represent the intermediate code during code generation. (B) It is computed by traversing the tree from the leaves. (C) It is propagated from leaf nodes to the root. (D) 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) Memory management (D) 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 9. Which of the following is an example of a synthesized attribute for an expression in an abstract syntax tree? (A) The value of the expression after evaluation (B) The type of the left operand (C) The precedence of the operator (D) The position of the expression in the source code 10. In an attribute grammar, what is the typical direction of attribute flow for synthesized attributes? (A) Right to left, across the production rules (B) Downwards, from the root to the leaves (C) Left to right, across the production rules (D) Upwards, from the leaves to the root 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) There is no difference between synthesized and inherited attributes. (D) 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 value of a variable in an expression (B) The scope of a variable defined in a function (C) The type of a variable passed to a function (D) The class of a node in an inheritance hierarchy 13. How are attribute grammars typically used in compilers? (A) For defining the syntax of a language (B) For performing lexical analysis (C) For providing semantic rules during code generation (D) For analyzing runtime behavior 14. Which of the following is true about the attribute grammar formalism? (A) It cannot handle syntax-directed translation (B) It is limited to defining syntax rules (C) It only supports semantic analysis for simple expressions (D) It allows both syntax and semantics to be described together 15. In attribute grammars, which method is used to propagate inherited attributes? (A) Left-to-right traversal (B) Bottom-up propagation (C) Top-down propagation (D) Right-to-left traversal