0% found this document useful (0 votes)
14 views

UNIT II

Uploaded by

sharmila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

UNIT II

Uploaded by

sharmila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIT II SYNTAX ANALYSIS

SET A

Role of Parser – Grammars – Error Handling – Context-free grammars –


Writing a grammar – Top Down Parsing - General Strategies Recursive
Descent Parser Predictive Parser-LL(1) Parser-Shift Reduce Parser-LR
Parser-LR (0)Item Construction of SLR Parsing Table - Introduction to
LALR Parser - Error Handling and Recovery in Syntax Analyzer-YACC.

I. Fill in the Blanks (5 x 1 = 5 Marks)

1. A parser generates a ______ tree to represent the syntactic structure of


the input.
Answer: Parse
2. ______ is a tool that generates a parser from a given context-free
grammar.
Answer: YACC
3. In ______ parsing, the parse tree is constructed from root to leaves.
Answer: Top-down
4. In an SLR parsing table, reductions are determined using the ______ set of
the grammar.
Answer: FOLLOW
5. The process of removing left recursion in a grammar is necessary for
______ parsing.
Answer: Predictive

II. Multiple Choice Questions (5 x 1 = 5 Marks)

1. Which type of grammar is used for syntax analysis?


a) Regular Grammar
b) Context-Free Grammar
c) Context-Sensitive Grammar
d) Unrestricted Grammar
Answer: b) Context-Free Grammar
2. What is the key property of an LL(1) grammar?
a) Right Factored
b) Left Recursive
c) Uses FOLLOW set for parsing
d) Can handle ambiguous grammars
Answer: a) Right Factored
3. Which parsing technique constructs the parse tree from leaves to the
root?
a) Recursive Descent Parsing
b) Top-Down Parsing
c) Bottom-Up Parsing
d) Predictive Parsing
Answer: c) Bottom-Up Parsing
4. The main difference between LR and LALR parsers is:
a) LALR parsers use reduced states for efficiency.
b) LR parsers cannot handle ambiguous grammars.
c) LALR parsers scan input right to left.
d) LALR parsers do not use lookahead.
Answer: a) LALR parsers use reduced states for efficiency.
5. YACC generates code for:
a) Lexical Analysis
b) Syntax Analysis
c) Intermediate Code Generation
d) Error Reporting
Answer: b) Syntax Analysis

III. Match the Following (5 x 1 = 5 Marks)

Column A Column B
1. FOLLOW set a) Handles syntax errors
2. Top-Down Parsing b) Lookahead for Predictive Parsing
3. YACC c) Generates parser from grammar
4. Shift-Reduce Parsing d) Bottom-up Parsing
5. Panic Mode Recovery e) Recursive Descent Parser

Answer:
1 - b, 2 - e, 3 - c, 4 - d, 5 - a

IV. True or False Statements (5 x 1 = 5 Marks)

1. Recursive Descent Parsing requires the grammar to be left-factored.


Answer: True
2. SLR parsers use both FOLLOW and FIRST sets for shift-reduce decisions.
Answer: False
3. A context-free grammar can always be converted into an LL(1)
grammar.
Answer: False
4. YACC generates parsers that can handle ambiguous grammars directly.
Answer: False
5. LALR parsers are more space-efficient than LR parsers.
Answer: True

V. One Mark Descriptive Questions (5 x 1 = 5 Marks)

1. If a user’s code has a missing semicolon, how does a syntax analyzer


handle it?
Answer: The syntax analyzer detects the error, skips to the next
statement, and reports the error with line number and description.
2. A grammar has left recursion. How should it be modified for predictive
parsing?
Answer: Left recursion should be eliminated by rewriting the grammar
into a non-left recursive form.
3. The input contains a syntax error, and the parser is in panic mode. What
happens next?
Answer: The parser skips input symbols until a synchronizing token
(like a semicolon or closing bracket) is found to resume parsing.
4. While constructing the SLR table, two states lead to a shift/reduce
conflict. What does this indicate about the grammar?
Answer: The grammar might be ambiguous or not suitable for SLR
parsing.
5. A user wants to add new rules to a grammar used in YACC. What steps
are necessary?
Answer: Update the YACC file with the new grammar rules, recompile
the YACC file, and integrate it with the lexical analyzer.
SET - B

I. Fill in the Blanks:

1. A _______________ is a set of rules that defines the syntax of a language.


Answer: Grammar
2. The ____________ parser constructs the syntax tree by trying to match the
leftmost non-terminal first.
Answer: Top-Down
3. In a _____________ parser, a shift operation involves moving an input
symbol onto the stack, and a reduce operation replaces a sequence of
symbols on the stack with a non-terminal.
Answer: Shift-Reduce
4. _______________ is a method used in error handling where the parser
discards symbols until it finds a valid continuation of the syntax.
Answer: Panic Mode
5. An LL(1) parser uses a ______________ to decide which rule to apply based
on the next input symbol and the current top of the stack.
Answer: Lookahead

II. Multiple Choice Questions:

1. Which of the following is a top-down parsing technique?


a) LL(1)
b) LR(1)
c) SLR
d) All of the above
Answer: a) LL(1)
2. In a shift-reduce parser, a __________ operation involves replacing a
sequence of symbols on the stack with a non-terminal.
a) Shift
b) Reduce
c) Accept
d) Reject
Answer: b) Reduce
3. Which type of grammar is used for LL(1) parsing?
a) Context-free grammar
b) Regular grammar
c) Context-sensitive grammar
d) Unrestricted grammar
Answer: a) Context-free grammar
4. Which of the following is true about an SLR parser?
a) It uses lookahead of more than one symbol
b) It is a type of bottom-up parser
c) It requires backtracking
d) It uses only regular expressions
Answer: b) It is a type of bottom-up parser
5. Which of the following techniques is used in error recovery by
discarding symbols until a valid recovery point is found?
a) Panic mode
b) Phrase-level recovery
c) Contextual recovery
d) Backtracking
Answer: a) Panic mode

III. Match the Following:

Column A Column B
1. LL(1) Parser a) Top-Down Parsing
2. LR Parser b) Bottom-Up Parsing
3. Context-Free Grammar c) Type of Grammar used by LL(1)
4. Shift-Reduce Parser d) A type of Bottom-Up Parser
5. Predictive Parser e) Uses lookahead to predict parsing rules

Answer:

1. LL(1) Parser → e) Uses lookahead to predict parsing rules


2. LR Parser → b) Bottom-Up Parsing
3. Context-Free Grammar → c) Type of Grammar used by LL(1)
4. Shift-Reduce Parser → d) A type of Bottom-Up Parser
5. Predictive Parser → a) Top-Down Parsing

IV. True or False:

1. Top-down parsers can handle left-recursive grammars directly.


Answer: False
2. LL(1) parsing requires only one symbol of lookahead to decide which
production rule to apply.
Answer: True
3. In a shift-reduce parser, the "shift" operation moves the input symbol
from the input stream onto the stack.
Answer: True
4. LR parsers use left-to-right scanning of the input and generate a syntax
tree by reducing to the start symbol.
Answer: True
5. In an SLR parser, the syntax tree is built from top to bottom.
Answer: False

V.One Mark Descriptive Questions:

1. What is the role of a parser in a compiler?


Answer: A parser checks the syntax of the source code by converting a
stream of tokens into a syntax tree or abstract syntax tree, ensuring that
the code follows the grammar rules of the programming language.
2. What is a context-free grammar?
Answer: A context-free grammar (CFG) is a type of formal grammar
where each production rule has a single non-terminal symbol on the
left-hand side and a string of terminals and/or non-terminals on the
right-hand side.
3. Explain recursive descent parsing.
Answer: Recursive descent parsing is a top-down parsing technique
where each non-terminal is represented by a function that tries to
match the input based on the production rules of that non-terminal.
4. What is an LL(1) parser?
Answer: An LL(1) parser is a type of predictive parser that uses a left-
to-right scan of the input and a single lookahead symbol to decide which
production to apply in the grammar.
5. What is an SLR parser and how is it different from LR parsers?
Answer: An SLR (Simple LR) parser is a type of bottom-up parser that
uses a single lookahead symbol and a simplified parsing table. It is less
powerful than more general LR parsers like LALR but can handle a
subset of context-free grammars.
SET - C

I. Fill in the Blanks:

1. ____________ parsing uses the current input symbol to predict the next
rule to apply, considering one symbol of lookahead.
Answer: Predictive
2. In an ____________ parser, the syntax tree is constructed by reducing the
input symbols in a bottom-up manner.
Answer: LR
3. ____________ grammar is the type of grammar used by parsers that can
handle LL(1) parsing, where the leftmost derivation is used.
Answer: Context-Free
4. In a ____________ parser, the parser applies a shift or reduce operation to
move or reduce the symbols on the stack based on the input.
Answer: Shift-Reduce
5. ____________ mode of error recovery discards input symbols until a valid
state is reached in the parser.
Answer: Panic

II. Multiple Choice Questions:

1. Which of the following is a bottom-up parsing technique?


a) LL(1)
b) Recursive Descent
c) SLR
d) All of the above
Answer: c) SLR
2. Which of the following is true about context-free grammars?
a) They can only be used in top-down parsing
b) They are used in both LL(1) and LR parsing
c) They are used in only LL(1) parsing
d) They are only used in regular expression matching
Answer: b) They are used in both LL(1) and LR parsing
3. Which of the following methods is used to handle error recovery in a
parser?
a) Lookahead
b) Panic mode
c) Backtracking
d) Depth-first search
Answer: b) Panic mode
4. A parser that handles a left-to-right scan of the input with one symbol of
lookahead is called a:
a) LR parser
b) SLR parser
c) LL(1) parser
d) Recursive Descent parser
Answer: c) LL(1) parser
5. Which of the following is a property of an LL(1) parser?
a) It uses left recursion
b) It is a top-down parser
c) It uses more than one lookahead symbol
d) It is a bottom-up parser
Answer: b) It is a top-down parser

III. Match the Following:

Column A Column B
1. Top-Down a) Uses a single lookahead symbol to decide on the next
Parsing production
2. Shift-Reduce b) Involves moving input symbols onto the stack and
Parsing reducing based on grammar rules
c) Suitable for predictive parsing with one symbol of
3. LL(1) Parser
lookahead
d) A bottom-up parsing technique used for handling
4. SLR Parser
context-free grammars
5. Context-Free e) Describes the structure of tokens used in parsers like
Grammar LL(1) and LR

Answer:

1. Top-Down Parsing → e) Describes the structure of tokens used in


parsers like LL(1) and LR
2. Shift-Reduce Parsing → b) Involves moving input symbols onto the stack
and reducing based on grammar rules
3. LL(1) Parser → a) Uses a single lookahead symbol to decide on the next
production
4. SLR Parser → d) A bottom-up parsing technique used for handling
context-free grammars
5. Context-Free Grammar → c) Suitable for predictive parsing with one
symbol of lookahead

IV. True or False:

1. The shift-reduce parser requires a fixed set of rules for parsing.


Answer: True
2. LL(1) parsing uses backtracking to handle ambiguities in the grammar.
Answer: False
3. An LR parser can parse a broader range of grammars than an LL(1)
parser.
Answer: True
4. In a top-down parser, the input is processed from the right to the left.
Answer: False
5. Error recovery in syntax analyzers is achieved through techniques like
panic mode and phrase-level recovery.
Answer: True

V. One Mark Descriptive Questions:

1. What is the purpose of the parser in the compilation process?


Answer: The parser checks whether the sequence of tokens generated
by the lexical analyzer follows the syntax rules defined by the grammar
of the programming language and constructs a syntax tree.
2. What is a shift-reduce parser?
Answer: A shift-reduce parser is a type of bottom-up parser where the
input is processed one symbol at a time. The parser can either "shift" an
input symbol onto the stack or "reduce" a sequence of symbols into a
non-terminal according to the grammar rules.
3. What are the advantages of LL(1) parsing?
Answer: LL(1) parsers are simple and efficient because they use one
symbol of lookahead to decide which production to apply, making them
easy to implement and fast to execute for languages with unambiguous
grammars.
4. What is a context-free grammar (CFG)?
Answer: A context-free grammar is a formal grammar where each
production rule has a single non-terminal symbol on the left-hand side
and a string of terminals and/or non-terminals on the right-hand side,
commonly used in parsing.
5. How does an SLR parser differ from a regular LR parser?
Answer: An SLR (Simple LR) parser is a simplified version of an LR
parser. It uses a smaller, less complex parsing table and is easier to
construct, but it is less powerful and can only handle a subset of context-
free grammars compared to general LR parsers.
SET - D

I. Fill in the Blanks:

1. A parser that uses a left-to-right scan of the input and constructs the
parse tree from the start symbol is called a ___________ parser.
Answer: Top-Down
2. In an LL(1) parser, the decision about which production rule to apply is
based on the current input symbol and _____________.
Answer: Lookahead symbol
3. The process of constructing a parsing table for a Shift-Reduce parser is
called ____________.
Answer: LR(0) Item Construction
4. In an LR parser, the state transitions are driven by ___________ actions
based on the current input symbol.
Answer: Shift/Reduce
5. ____________ parsing is a method of error handling where the parser
attempts to recover by deleting or inserting symbols in the input
stream.
Answer: Phrase-Level

II. Multiple Choice Questions:

1. Which of the following parsers is a type of bottom-up parser?


a) LL(1)
b) Recursive Descent
c) LR Parser
d) None of the above
Answer: c) LR Parser
2. What is the primary function of the predictive parser in LL(1) parsing?
a) It performs backtracking
b) It predicts the next production rule using one symbol of lookahead
c) It reduces input symbols to a non-terminal
d) It scans the input right-to-left
Answer: b) It predicts the next production rule using one symbol of
lookahead
3. Which of the following grammars cannot be parsed using LL(1) parsing?
a) Context-Free Grammar
b) Left-recursive Grammar
c) Regular Grammar
d) Ambiguous Grammar
Answer: b) Left-recursive Grammar
4. Which of the following parsing techniques uses the concept of a "stack"
to manage input symbols during the parsing process?
a) LL(1) Parsing
b) Recursive Descent Parsing
c) Shift-Reduce Parsing
d) Both b and c
Answer: d) Both b and c
5. In the context of LR parsing, what does the term "shift" refer to?
a) Replacing a sequence of symbols with a non-terminal
b) Moving an input symbol onto the stack
c) Scanning the input from right to left
d) Reducing the number of states in the parser
Answer: b) Moving an input symbol onto the stack

III. Match the Following:

Column A Column B
1. LR(0) Parsing a) Bottom-Up Parsing Technique
2. Recursive Descent Parsing b) Uses a stack to manage input
3. Phrase-Level Error Recovery c) Handles errors by skipping tokens
4. LL(1) Parser d) Top-Down Parsing Technique
5. Shift-Reduce Parsing e) A method used in Bottom-Up Parsing

Answer:

1. LR(0) Parsing → a) Bottom-Up Parsing Technique


2. Recursive Descent Parsing → d) Top-Down Parsing Technique
3. Phrase-Level Error Recovery → c) Handles errors by skipping tokens
4. LL(1) Parser → d) Top-Down Parsing Technique
5. Shift-Reduce Parsing → e) A method used in Bottom-Up Parsing

IV. True or False:

1. An LL(1) parser can handle left-recursive grammars directly.


Answer: False
2. In Shift-Reduce parsing, the "reduce" operation replaces a sequence of
symbols with a non-terminal.
Answer: True
3. Recursive Descent parsers are generally faster than LL(1) parsers.
Answer: False
4. The LR parser can handle more complex grammars than the LL(1)
parser.
Answer: True
5. In the context of YACC, error recovery can be achieved by using a state-
based mechanism.
Answer: True

V. One Mark Descriptive Questions:

1. What is the difference between Top-Down and Bottom-Up parsing?


Answer: Top-Down parsing starts with the start symbol and tries to
match the input with the grammar, constructing the syntax tree from
the top down. Bottom-Up parsing starts with the input symbols and
reduces them to the start symbol, constructing the syntax tree from the
bottom up.
2. What is the role of lookahead in LL(1) parsing?
Answer: In LL(1) parsing, the lookahead symbol is used to decide which
production rule to apply based on the next input symbol. It ensures that
the correct rule is chosen without ambiguity.
3. What is an LR parser and how does it differ from an LL(1) parser?
Answer: An LR parser is a bottom-up parser that can handle a broader
class of grammars, including those with right recursion, by scanning the
input left-to-right and reducing using a parsing table. In contrast, an
LL(1) parser is a top-down parser that requires one symbol of
lookahead and can only handle a more limited set of grammars.
4. What is the significance of an SLR parsing table?
Answer: The SLR parsing table helps in determining the shift and
reduce actions in an SLR parser. It simplifies the construction of the
parser by providing a more compact and manageable state machine
while still being able to handle context-free grammars.
5.
6. How does YACC assist in parsing and error handling?
Answer: YACC (Yet Another Compiler Compiler) is a tool that generates
parsers based on context-free grammar specifications. It uses an
LALR(1) parser and allows for error handling by defining error rules in
the grammar, which helps recover from syntax errors during parsing.
SET - E

I. Fill in the Blanks:

1. ____________ is the process of analyzing the source code to divide it into


meaningful tokens, such as keywords, identifiers, operators, and literals.
Answer: Lexical Analysis
2. A ____________ is used to specify the formal syntax of tokens, typically
using regular expressions or finite automata.
Answer: Lexical Analyzer
3. In ____________ parsing, a parser constructs the parse tree from the top to
the bottom, starting with the start symbol and predicting which rules to
apply.
Answer: Top-Down
4. ____________ is a method of error recovery in a parser where the parser
discards input symbols until it finds a valid point to continue parsing.
Answer: Panic Mode
5. A ____________ is a finite state machine that is used to represent the
behavior of a lexical analyzer and helps in recognizing tokens.
Answer: Finite Automaton

II.Multiple Choice Questions:

1. Which of the following is true for the lexical analyzer?


a) It handles syntax errors
b) It checks the grammar of the input
c) It divides the source code into tokens
d) It generates intermediate code
Answer: c) It divides the source code into tokens
2. In LL(1) parsing, what is the role of the lookahead symbol?
a) To select the correct production rule
b) To parse left-recursive grammar
c) To reduce the symbols on the stack
d) To handle syntax errors
Answer: a) To select the correct production rule
3. What type of grammar is used by a Shift-Reduce parser?
a) Regular Grammar
b) Context-Free Grammar
c) Context-Sensitive Grammar
d) Unrestricted Grammar
Answer: b) Context-Free Grammar
4. In the context of YACC, what does LALR stand for?
a) Left Adaptive LR
b) Lookahead LR
c) Lookahead Level LR
d) Lookahead LR(1)
Answer: d) Lookahead LR(1)
5. What is the primary advantage of using Finite Automata in lexical
analysis?
a) It simplifies error recovery
b) It can be easily implemented using a parser generator
c) It can efficiently recognize tokens with a fixed set of rules
d) It handles all kinds of syntax errors
Answer: c) It can efficiently recognize tokens with a fixed set of rules

III.Match the Following:

Column A Column B
1. Lexical Analyzer a) Uses shift and reduce actions to parse grammar
2. SLR Parser b) Bottom-Up Parsing
3. LL(1) Parser c) Predictive Top-Down Parsing
4. Finite Automata d) Recognizes patterns in tokens
5. YACC e) Generates parsers using LALR

Answer:

1. Lexical Analyzer → d) Recognizes patterns in tokens


2. SLR Parser → b) Bottom-Up Parsing
3. LL(1) Parser → c) Predictive Top-Down Parsing
4. Finite Automata → d) Recognizes patterns in tokens
5. YACC → e) Generates parsers using LALR

IV.True or False:

1. A parser in a compiler generates machine code from the source code.


Answer: False
2. Lexical analysis checks if the syntax of the tokens is correct.
Answer: False
3. In an LL(1) parser, the "1" refers to using one symbol of lookahead
during parsing.
Answer: True
4. The main task of a Shift-Reduce parser is to predict the next token in the
input stream.
Answer: False
5. YACC can be used to generate parsers for grammars that are ambiguous.
Answer: False

V.One Mark Descriptive Questions:

1. What is the role of a lexical analyzer in a compiler?


Answer: The lexical analyzer scans the source code and breaks it into
tokens, such as keywords, identifiers, literals, and operators, which are
then passed to the parser for further analysis.
2. Explain the concept of finite automata in the context of lexical
analysis.
Answer: Finite automata are mathematical models used to recognize
patterns in input strings. In lexical analysis, finite automata are used to
identify tokens by transitioning through states based on input symbols,
helping in the recognition of language patterns.
3. What is a context-free grammar (CFG)?
Answer: A context-free grammar is a formal grammar where the left-
hand side of every production rule consists of a single non-terminal
symbol, and the right-hand side consists of terminals and/or non-
terminals.
4. What is the purpose of the LR parsing method?
Answer: LR parsing is a bottom-up parsing technique that reads the
input from left to right and uses a rightmost derivation to construct a
syntax tree. It can handle a larger class of context-free grammars
compared to top-down parsing methods like LL(1).
5. What are the advantages of using an SLR parser over a general LR
parser?
Answer: An SLR parser is simpler to implement and requires less
memory than a full LR parser, while still being able to parse a significant
subset of context-free grammars, making it more efficient in many cases.

You might also like