Attribute grammars(MCQs)

What is an attribute grammar?

a) A type of context-free grammar
b) A type of formal grammar used for syntax analysis
c) A type of grammar used for parsing and semantic analysis
d) A type of regular grammar
Answer: c) A type of grammar used for parsing and semantic analysis

In attribute grammars, what does an attribute represent?

a) A symbol in the grammar
b) A type of grammar rule
c) A piece of information associated with a grammar symbol
d) A type of parser
Answer: c) A piece of information associated with a grammar symbol

Which of the following is true about synthesized attributes?

a) They are computed from attributes of parent nodes
b) They are computed from attributes of child nodes
c) They are used only in top-down parsing
d) They are used only in bottom-up parsing
Answer: b) They are computed from attributes of child nodes

Inherited attributes are computed from:

a) Attributes of child nodes
b) Attributes of sibling nodes
c) Attributes of parent nodes
d) Attributes of all nodes
Answer: c) Attributes of parent nodes

In an attribute grammar, which of the following is used to define semantic rules?

a) Productions
b) Attributes
c) Functions
d) Semantics rules
Answer: d) Semantics rules

What is the primary purpose of an attribute grammar?

a) To define syntax rules
b) To define semantic rules
c) To implement lexing
d) To perform syntax checking only
Answer: b) To define semantic rules

Which of the following types of attributes is used to represent values computed from the parse tree?

a) Synthesized attributes
b) Inherited attributes
c) Both synthesized and inherited attributes
d) None of the above
Answer: a) Synthesized attributes

In attribute grammars, what is a synthesized attribute’s value computed from?

a) Its parent node
b) Its sibling nodes
c) Its child nodes
d) Its own value
Answer: c) Its child nodes

Which attribute type is used to pass information from parent to child nodes?

a) Synthesized
b) Inherited
c) Global
d) Local
Answer: b) Inherited

Which of the following is an example of an inherited attribute?

a) The value of an identifier in a scope
b) The type of a variable in a declaration
c) The result of an arithmetic expression
d) The length of a string
Answer: a) The value of an identifier in a scope

What is the main challenge in evaluating inherited attributes in attribute grammars?

a) They require bottom-up parsing
b) They need access to parent node information
c) They are computed before synthesized attributes
d) They are used only in context-free grammars
Answer: b) They need access to parent node information

Which method of attribute grammar evaluation is suitable for top-down parsing?

a) Post-order traversal
b) Pre-order traversal
c) Level-order traversal
d) Depth-first traversal
Answer: b) Pre-order traversal

Which of the following is NOT a characteristic of synthesized attributes?

a) Computed from child nodes
b) Used for bottom-up parsing
c) Represent semantic values of a subtree
d) Depend on parent node information
Answer: d) Depend on parent node information

In attribute grammars, what is an attribute evaluation function?

a) A function that computes attribute values
b) A function that defines grammar rules
c) A function that parses input strings
d) A function that generates tokens
Answer: a) A function that computes attribute values

Which attribute type is most commonly used in syntax-directed translation?

a) Inherited attributes
b) Synthesized attributes
c) Global attributes
d) Local attributes
Answer: b) Synthesized attributes

Which of the following statements about attribute grammars is true?

a) All attributes must be synthesized
b) Inherited attributes are always computed first
c) Attribute grammars can be used to specify the semantics of programming languages
d) Attribute grammars are only used for syntactic analysis
Answer: c) Attribute grammars can be used to specify the semantics of programming languages

In an attribute grammar, how are inherited attributes typically computed?

a) By traversing the parse tree from leaves to root
b) By traversing the parse tree from root to leaves
c) By evaluating semantic rules from parent to child
d) By evaluating semantic rules from child to parent
Answer: c) By evaluating semantic rules from parent to child

What is the key advantage of using attribute grammars over context-free grammars alone?

a) Attribute grammars can specify semantic constraints and computations
b) Attribute grammars are simpler to implement
c) Attribute grammars provide faster parsing
d) Attribute grammars do not require a syntax tree
Answer: a) Attribute grammars can specify semantic constraints and computations

What type of attribute is used to carry information from a parent node to a child node?

a) Synthesized
b) Inherited
c) Static
d) Dynamic
Answer: b) Inherited

Which traversal order is typically used for synthesized attributes in a parse tree?

a) Pre-order
b) Post-order
c) Level-order
d) Depth-first
Answer: b) Post-order

In attribute grammars, what is a semantic action?

a) A function that modifies the parse tree
b) An operation that computes attribute values
c) A rule that changes grammar productions
d) An action performed during lexical analysis
Answer: b) An operation that computes attribute values

Which type of attribute is generally associated with attribute grammar rules that depend on parent information?

a) Synthesized
b) Inherited
c) Both synthesized and inherited
d) None of the above
Answer: b) Inherited

What is the role of semantic rules in attribute grammars?

a) To define the syntax of a language
b) To specify how attributes are computed
c) To generate tokens from input strings
d) To parse input strings
Answer: b) To specify how attributes are computed

Which of the following is NOT a typical use of attribute grammars?

a) Type checking
b) Syntax checking
c) Syntax-directed translation
d) Code optimization
Answer: d) Code optimization

What is a “well-formed” attribute grammar?

a) A grammar with only synthesized attributes
b) A grammar with no inherited attributes
c) A grammar where all attributes are properly computed according to their definitions
d) A grammar that only defines syntax rules
Answer: c) A grammar where all attributes are properly computed according to their definitions

Which algorithm is commonly used to evaluate attributes in attribute grammars?

a) LL algorithm
b) LR algorithm
c) SLR algorithm
d) DAG (Directed Acyclic Graph) algorithm
Answer: d) DAG (Directed Acyclic Graph) algorithm

What does a semantic predicate in an attribute grammar do?

a) Specifies a semantic action
b) Defines a semantic rule
c) Adds a condition to the attribute evaluation
d) Changes the grammar production
Answer: c) Adds a condition to the attribute evaluation

Which of the following is an example of a synthesized attribute in a simple arithmetic expression grammar?

a) The type of an operand
b) The result of an expression
c) The precedence of an operator
d) The scope of a variable
Answer: b) The result of an expression

How does an attribute grammar handle semantic errors?

a) By using semantic actions to perform error checking
b) By modifying the syntax rules
c) By ignoring errors during parsing
d) By adding additional attributes to the grammar
Answer: a) By using semantic actions to perform error checking

Which of the following is a common application of attribute grammars in compiler construction?

a) Code generation
b) Lexical analysis
c) Syntax-directed translation
d) Tokenization
Answer: c) Syntax-directed translation

What is a “semantic function” in the context of attribute grammars?

a) A function that modifies the syntax tree
b) A function that computes the value of an attribute
c) A function used in lexical analysis
d) A function that generates tokens
Answer: b) A function that computes the value of an attribute

In which traversal order are inherited attributes usually computed?

a) Post-order
b) Pre-order
c) Level-order
d) Depth-first
Answer: b) Pre-order

Which attribute type would be used to calculate the type of a variable in a type-checking attribute grammar?

a) Synthesized
b) Inherited
c) Global
d) Local
Answer: a) Synthesized

What is the primary benefit of using a DAG in evaluating attribute grammars?

a) It simplifies syntax analysis
b) It reduces the complexity of attribute evaluation
c) It improves lexical analysis
d) It eliminates the need for semantic actions
Answer: b) It reduces the complexity of attribute evaluation

What is a common characteristic of well-formed attribute grammars?

a) They use only inherited attributes
b) They use only synthesized attributes
c) They ensure that all attributes are computed according to their rules
d) They avoid using semantic rules
Answer: c) They ensure that all attributes are computed according to their rules

In attribute grammars, what does the term “syntax-directed translation” refer to?

a) Translating syntax rules into code
b) Translating parse trees into semantic actions
c) Translating tokens into syntax rules
d) Translating semantic actions into code
Answer: b) Translating parse trees into semantic actions

Which data structure is often used to represent attribute grammars?

a) Parse tree
b) Abstract syntax tree (AST)
c) Directed Acyclic Graph (DAG)
d) Linear list
Answer: c) Directed Acyclic Graph (DAG)

How can attribute grammars be used in type checking?

a) By defining semantic rules that enforce type constraints
b) By parsing type declarations
c) By generating type information from syntax rules
d) By creating type conversion functions
Answer: a) By defining semantic rules that enforce type constraints

Which attribute type is most useful for maintaining context-sensitive information?

a) Synthesized
b) Inherited
c) Both synthesized and inherited
d) None of the above
Answer: b) Inherited

What does a “semantic error” in an attribute grammar usually indicate?

a) A problem with syntax rules
b) A problem with attribute computation
c) A problem with tokenization
d) A problem with parsing
Answer: b) A problem with attribute computation

What role does a “context-free grammar” play in attribute grammars?

a) It defines the syntax of the language
b) It defines the semantic rules
c) It generates tokens for parsing
d) It manages semantic actions
Answer: a) It defines the syntax of the language

Which attribute type is most often used for computing values based on children nodes in a parse tree?

a) Inherited
b) Synthesized
c) Global
d) Static
Answer: b) Synthesized

In attribute grammars, what is a semantic rule typically used for?

a) Defining syntax productions
b) Computing the values of attributes
c) Parsing input strings
d) Generating tokens
Answer: b) Computing the values of attributes

What is the purpose of a “semantic predicate” in an attribute grammar?

a) To validate semantic constraints
b) To define syntax rules
c) To generate tokens
d) To perform lexical analysis
Answer: a) To validate semantic constraints

Which type of attribute is particularly useful for implementing scope rules in a compiler?

a) Synthesized
b) Inherited
c) Both synthesized and inherited
d) None of the above
Answer: b) Inherited

In attribute grammars, which of the following best describes synthesized attributes?

a) Computed from parent attributes
b) Computed from child attributes
c) Computed from sibling attributes
d) Computed from global attributes
Answer: b) Computed from child attributes

Which of the following is a common application of inherited attributes?

a) Symbol table management
b) Expression evaluation
c) Syntax checking
d) Token generation
Answer: a) Symbol table management

In an attribute grammar, what does a “semantic action” typically perform?

a) Computes attribute values
b) Defines syntax rules
c) Parses input strings
d) Generates tokens
Answer: a) Computes attribute values

What is the role of semantic rules in context-sensitive language processing?

a) To define syntax rules
b) To compute attribute values based on context
c) To tokenize input strings
d) To parse syntax trees
Answer: b) To compute attribute values based on context

Which attribute grammar concept is primarily concerned with attributes that depend on the parse tree structure and are computed from it?

a) Inherited attributes
b) Synthesized attributes
c) Global attributes
d) Local attributes
Answer: b) Synthesized attributes