Construction of parsers (e.g., using Yacc)(MCQs)

What is Yacc primarily used for in compiler construction?

a) Generating lexical analyzers
b) Generating syntax analyzers (parsers)
c) Generating code for a virtual machine
d) Generating intermediate code
Answer: b) Generating syntax analyzers (parsers)
What does Yacc stand for?

a) Yet Another Compiler Compiler
b) Yet Another Code Compiler
c) Your Automated Compiler Compiler
d) Your Analyzed Code Compiler
Answer: a) Yet Another Compiler Compiler
In Yacc, which section contains the grammar rules for parsing?

a) Definitions section
b) Rules section
c) Code section
d) Declarations section
Answer: b) Rules section
What is the primary purpose of the ‘%%’ symbols in a Yacc file?

a) To indicate the beginning and end of the rules section
b) To define the lexical analyzer functions
c) To declare global variables
d) To specify comments
Answer: a) To indicate the beginning and end of the rules section
Which file is generated by Yacc and contains the parser code?

a) y.tab.c
b) y.tab.h
c) yacc.out
d) parser.out
Answer: a) y.tab.c
What is the purpose of the ‘lex’ tool in conjunction with Yacc?

a) To generate the lexical analyzer that tokenizes the input
b) To parse the syntax rules defined in Yacc
c) To optimize the generated code
d) To generate intermediate code
Answer: a) To generate the lexical analyzer that tokenizes the input
Which of the following is a key feature of Yacc’s output?

a) It includes functions for error handling and parsing
b) It includes machine code for execution
c) It includes a symbol table for variable management
d) It includes debugging information for lexical analysis
Answer: a) It includes functions for error handling and parsing
In Yacc, how are semantic actions specified?

a) Within curly braces {} following the grammar rules
b) Within parentheses () following the grammar rules
c) Within square brackets [] following the grammar rules
d) Within angle brackets <> following the grammar rules
Answer: a) Within curly braces {} following the grammar rules
What does the y.tab.h file generated by Yacc contain?

a) Declarations for token types and parser functions
b) Definitions for lexical analyzer functions
c) The grammar rules for parsing
d) The main function for executing the parser
Answer: a) Declarations for token types and parser functions
What is the purpose of the %token declaration in Yacc?

a) To define terminal symbols (tokens) used in the grammar rules
b) To specify non-terminal symbols
c) To declare functions for semantic actions
d) To include external libraries
Answer: a) To define terminal symbols (tokens) used in the grammar rules
How does Yacc handle conflicts in grammar rules?

a) By providing default conflict resolution strategies or requiring manual intervention
b) By automatically resolving conflicts without user input
c) By ignoring conflicting rules
d) By generating multiple parsers for different rules
Answer: a) By providing default conflict resolution strategies or requiring manual intervention
What does the yacc command do in a typical compilation process?

a) It generates parser code from a Yacc input file
b) It compiles the generated parser code into executable format
c) It links the parser code with the lexical analyzer
d) It executes the parser code directly
Answer: a) It generates parser code from a Yacc input file
In Yacc, what is the function of the error token in grammar rules?

a) To handle syntax errors and recover from them
b) To define error handling functions
c) To specify non-terminal symbols
d) To define terminal symbols
Answer: a) To handle syntax errors and recover from them
What is the role of the %left, %right, and %nonassoc directives in Yacc?

a) To specify operator precedence and associativity
b) To define terminal symbols
c) To declare global variables
d) To specify semantic actions
Answer: a) To specify operator precedence and associativity
How are actions specified in Yacc grammar rules?

a) Using C code enclosed in {} braces
b) Using Python code enclosed in [] brackets
c) Using Java code enclosed in <> angle brackets
d) Using comments enclosed in /* */
Answer: a) Using C code enclosed in {} braces
Which directive in Yacc is used to include C code in the generated parser file?

a) %{ %}
b) %include
c) %code
d) %insert
Answer: a) %{ %}
What is the primary function of the yacc parser generator?

a) To transform grammar rules into a C program for parsing
b) To generate lexical analyzer code
c) To optimize the generated parser code
d) To compile and link the parser with other code
Answer: a) To transform grammar rules into a C program for parsing
Which file extension is commonly associated with Yacc input files?

a) .y
b) .lex
c) .cpp
d) .h
Answer: a) .y
In Yacc, what does the %start directive specify?

a) The starting non-terminal symbol for parsing
b) The terminal symbols used in the grammar
c) The file to include for additional code
d) The error handling routine
Answer: a) The starting non-terminal symbol for parsing
What does the yyparse function do in the generated Yacc parser code?

a) It starts the parsing process
b) It generates the lexical analyzer
c) It handles syntax errors
d) It performs semantic analysis
Answer: a) It starts the parsing process
Which of the following tools is commonly used alongside Yacc to generate lexical analyzers?

a) Lex
b) Bison
c) ANTLR
d) Flex
Answer: d) Flex
In Yacc, what is the purpose of the %% section that appears at the end of the file?

a) To contain C code for the parser’s actions and utility functions
b) To define terminal symbols
c) To declare global variables
d) To specify additional grammar rules
Answer: a) To contain C code for the parser’s actions and utility functions
How does Yacc handle multiple parsing rules that share common prefixes?

a) By generating a parser that can handle the common prefixes effectively
b) By requiring manual resolution of conflicts
c) By ignoring common prefixes
d) By creating separate parsing tables for each rule
Answer: a) By generating a parser that can handle the common prefixes effectively
What does the %define directive in Yacc allow you to do?

a) Define parser behavior and attributes
b) Include external libraries
c) Specify token types
d) Declare global variables
Answer: a) Define parser behavior and attributes
In Yacc, how are semantic actions typically used within grammar rules?

a) To execute code that processes or manipulates the parsed data
b) To define additional grammar rules
c) To declare token types
d) To handle lexical analysis
Answer: a) To execute code that processes or manipulates the parsed data
What is the primary purpose of the #include directive in the Yacc input file?

a) To include additional header files or libraries in the generated parser code
b) To specify the terminal symbols
c) To define semantic actions
d) To declare non-terminal symbols
Answer: a) To include additional header files or libraries in the generated parser code
Which of the following is true about the yyerror function in Yacc-generated code?

a) It is used to handle syntax errors during parsing
b) It defines terminal symbols for parsing
c) It specifies the starting non-terminal symbol
d) It handles lexical errors
Answer: a) It is used to handle syntax errors during parsing
What is the role of the %union directive in Yacc?

a) To define a union of data types for semantic values in parsing rules
b) To specify token types
c) To declare global variables
d) To define operator precedence
Answer: a) To define a union of data types for semantic values in parsing rules
How are non-terminal symbols defined in a Yacc grammar file?

a) As part of the grammar rules without special directives
b) Using the %nonterminal directive
c) Using the %define directive
d) Using the %token directive
Answer: a) As part of the grammar rules without special directives
What is the role of the yywrap function in Yacc?

a) To handle end-of-file conditions for the lexical analyzer
b) To manage parser states
c) To handle syntax errors
d) To perform semantic actions
Answer: a) To handle end-of-file conditions for the lexical analyzer
In Yacc, what does the start parameter in the yyparse function specify?

a) The initial non-terminal symbol to begin parsing
b) The starting point of lexical analysis
c) The location of the grammar rules
d) The name of the output file
Answer: a) The initial non-terminal symbol to begin parsing
Which directive in Yacc is used to specify the location of the semantic actions?

a) %{ %}
b) %code
c) %insert
d) %start
Answer: a) %{ %}
What is the typical output file of the lex tool when used with Yacc?

a) lex.yy.c
b) y.tab.c
c) parser.c
d) scanner.c
Answer: a) lex.yy.c
How does Yacc manage precedence and associativity for operators?

a) Using the %left, %right, and %nonassoc directives
b) By defining operator functions in C code
c) Through lexical analysis
d) By generating multiple parsing tables
Answer: a) Using the %left, %right, and %nonassoc directives
Which Yacc directive is used to specify the external code to include at the beginning of the parser file?

a) %{ %}
b) %include
c) %start
d) %code
Answer: a) %{ %}
What is the primary role of the yyparse function in the context of Yacc-generated parsers?

a) To initiate the parsing process based on the defined grammar
b) To perform lexical analysis
c) To handle semantic actions
d) To manage syntax errors
Answer: a) To initiate the parsing process based on the defined grammar
Which of the following is used to define tokens in a Yacc file?

a) %token
b) %define
c) %nonassoc
d) %start
Answer: a) %token
What does the %type directive in Yacc specify?

a) The data types of semantic values for non-terminals
b) The types of tokens used in the grammar
c) The precedence of operators
d) The external libraries to include
Answer: a) The data types of semantic values for non-terminals
In Yacc, what is the purpose of the yydebug variable?

a) To enable debugging information for the parser
b) To define token types
c) To handle syntax errors
d) To specify the starting non-terminal symbol
Answer: a) To enable debugging information for the parser
Which file contains the generated lexical analyzer code when using Lex with Yacc?

a) lex.yy.c
b) y.tab.c
c) parser.c
d) scanner.c
Answer: a) lex.yy.c
How are semantic actions specified in Yacc grammar rules?

a) As C code enclosed in {} after the grammar rule
b) As C++ code enclosed in <> after the grammar rule
c) As Python code enclosed in [] after the grammar rule
d) As Java code enclosed in () after the grammar rule
Answer: a) As C code enclosed in {} after the grammar rule
What is the purpose of the %define directive in a Yacc file?

a) To set various parser options and attributes
b) To include external code
c) To specify token types
d) To define semantic actions
Answer: a) To set various parser options and attributes
Which of the following is NOT a feature of Yacc-generated parsers?

a) Automatic handling of syntax errors
b) Generation of a lexical analyzer
c) Provision for custom error recovery routines
d) Support for defining operator precedence
Answer: b) Generation of a lexical analyzer
What does the %union directive define in a Yacc file?

a) A union of data types for semantic values used in the parsing rules
b) A union of token types
c) A union of global variables
d) A union of external libraries
Answer: a) A union of data types for semantic values used in the parsing rules
What is the function of the yyerror function in Yacc?

a) To handle syntax errors and print error messages
b) To perform lexical analysis
c) To initialize the parser state
d) To execute semantic actions
Answer: a) To handle syntax errors and print error messages
In a Yacc file, how are terminal symbols typically defined?

a) Using the %token directive
b) Using the %define directive
c) Using the %start directive
d) Using the %union directive
Answer: a) Using the %token directive
Which file is generated by Lex when used with Yacc?

a) lex.yy.c
b) y.tab.c
c) parser.c
d) scanner.c
Answer: a) lex.yy.c
What does the %left directive specify in Yacc?

a) The precedence and associativity of left-associative operators
b) The starting non-terminal symbol
c) The data types for semantic values
d) The external libraries to include
Answer: a) The precedence and associativity of left-associative operators
Which of the following is a common output file for Yacc?

a) y.tab.c
b) lex.yy.c
c) parser.out
d) scanner.out
Answer: a) y.tab.c
How does Yacc handle ambiguities in grammar?

a) By providing conflict resolution strategies or requiring manual intervention
b) By automatically resolving all ambiguities
c) By generating multiple parsing tables
d) By ignoring ambiguous rules
Answer: a) By providing conflict resolution strategies or requiring manual intervention