1. What is the primary role of a syntax analyzer (parser) in a compiler?
A) To translate the source code into machine code
B) To check the syntax of the source code and build a parse tree
C) To optimize the program for better performance
D) To generate intermediate code
Answer: B) To check the syntax of the source code and build a parse tree
2. Which of the following does a parser generate to represent the syntactic structure of a source program?
A) Abstract Syntax Tree (AST)
B) Intermediate Code
C) Symbol Table
D) Execution Code
Answer: A) Abstract Syntax Tree (AST)
3. The parser’s role in the syntax analysis phase is to:
A) Detect semantic errors in the code
B) Check if the code adheres to the grammar rules of the programming language
C) Translate the program into machine code
D) Generate optimization for faster execution
Answer: B) Check if the code adheres to the grammar rules of the programming language
4. Which type of parsing is used by most modern compilers for syntax analysis?
A) Recursive Descent Parsing
B) Shift-Reduce Parsing
C) Top-Down Parsing
D) Bottom-Up Parsing
Answer: D) Bottom-Up Parsing
5. Which of the following is a common output of a syntax analyzer?
A) Parse tree or Abstract Syntax Tree (AST)
B) Machine code
C) Optimized code
D) Symbol table
Answer: A) Parse tree or Abstract Syntax Tree (AST)
6. What does the parser do when it encounters a syntax error?
A) It stops the entire compilation process
B) It reports the error and generates a message
C) It generates machine code
D) It tries to fix the error automatically
Answer: B) It reports the error and generates a message
7. Which of the following is true about LL parsers (Top-Down Parsers)?
A) They start from the root and work their way down to the leaves of the syntax tree.
B) They use a stack to manage the parsing process.
C) They are primarily used for bottom-up parsing.
D) They generate intermediate code.
Answer: A) They start from the root and work their way down to the leaves of the syntax tree.
8. What is the difference between a lexical analyzer and a syntax analyzer?
A) The lexical analyzer checks the syntax, while the syntax analyzer breaks the source code into tokens.
B) The lexical analyzer checks the syntax, and the syntax analyzer detects semantic errors.
C) The lexical analyzer identifies tokens, and the syntax analyzer checks whether the arrangement of tokens follows the grammar.
D) The lexical analyzer generates machine code, and the syntax analyzer generates intermediate code.
Answer: C) The lexical analyzer identifies tokens, and the syntax analyzer checks whether the arrangement of tokens follows the grammar.
9. Which of the following is a limitation of a recursive descent parser?
A) It can handle all types of grammar.
B) It is difficult to implement for left-recursive grammars.
C) It is slower than other parsing techniques.
D) It cannot generate a parse tree.
Answer: B) It is difficult to implement for left-recursive grammars.
10. In which phase of the compiler pipeline does the parser operate?
A) Lexical Analysis
B) Syntax Analysis
C) Semantic Analysis
D) Code Generation
Answer: B) Syntax Analysis