DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
BE- Computer Science and Engineering
Anna University Regulation: 2021
CS3501- COMPILER DESIGN
III Year/V Semester
Question Bank
Unit – II Syntax Analysis
Prepared By,
Mrs.J.SWEETLINE ARPUTHAM, AP/CSE
4931_GRACE College of Engineering, Thoothukudi
UNIT II
SYNTAX ANALYSIS
2 marks
1. What is the output of syntax analysis phase? What are the three general typesof
parsers for grammars?
Parser (or) parse tree is the output of syntax analysis phase.
General types of parsers:
1) Universal parsing
2) Top-down
3) Bottom-up
2. What are the different strategies that a parser can employ to recover from a
syntactic error?
Panic modePhrase
level
Error productions
Global correction
3. What are the goals of error handler in a parser?
The error handler in a parser has simple-to-state goals:
It should report the presence of errors clearly and accurately.
It should recover from each error quickly enough to be able to detect
subsequent errors.
It should not significantly slow down the processing of correct programs.
4. What is phrase level error recovery?
On discovering an error, a parser may perform local correction on the remaining input;
that is, it may replace a prefix of the remaining input by some string that allows theparser to
continue. This is known as phrase level error recovery.
5. How will you define a context free grammar?
A context free grammar consists of terminals, non-terminals, a start symbol, and
productions.
i. Terminals are the basic symbols from which strings are formed. “Token” isa
synonym for terminal. Ex: if, then, else.
ii. Nonterminals are syntactic variables that denote sets of strings, which help define
the language generated by the grammar. Ex: stmt, expr.
iii. Start symbol is one of the nonterminals in a grammar and the set of strings it
denotes is the language defined by the grammar. Ex: S.
iv. The productions of a grammar specify the manner in which the terminals and
nonterminals can be combined to form strings Ex: expr id
6. Define context free language. When will you say that two CFGs are equal?
A language that can be generated by a grammar is said to be a context free language. Iftwo
grammars generate the same language, the grammars are said to be equivalent.
7. Give the definition for leftmost and canonical derivations.
Derivations in which only the leftmost nonterminal in any sentential form is
CS3501_CD
4931_GRACE College of Engineering, Thoothukudi
replaced at each step are termed leftmost derivations
Derivations in which the rightmost nonterminal is replaced at each step are termed
canonical derivations.
8. What is a parse tree?
A parse tree may be viewed as a graphical representation for a derivation that filters
out the choice regarding replacement order. Each interior node of a parse tree is labeled by
some nonterminal A and that the children of the node are labeled from left to right by symbols
in the right side of the production by which this A was replaced in the derivation. The leaves
of the parse tree are terminal symbols.
9. What is an ambiguous grammar? Give an example.
A grammar that produces more than one parse tree for some sentence is said to be
ambiguous
An ambiguous grammar is one that produces more than one leftmost or
rightmost derivation for the same sentence.
Ex:
E E+E / E*E / id
10. Why do we use regular expressions to define the lexical syntax of a language?
i. The lexical rules of a language are frequently quite simple, and to describe them we do
not need a notation as powerful as grammars.
ii. Regular expressions generally provide a more concise and easier to understand
notation for tokens than grammars.
iii. More efficient lexical analyzers can be constructed automatically from regular
expressions than from arbitrary grammars.
iv. Separating the syntactic structure of a language into lexical and non lexical parts provides
a convenient way of modularizing the front end of a compiler into two manageable-sized
components.
11. When will you call a grammar as the left recursive one?
A grammar is a left recursive if it has a nonterminal A such that there is a
derivation A A for some string .
12. Define left factoring.
Left factoring is a grammar transformation that is useful for producing a grammar
suitable for predictive parsing. The basic idea is that when it is not clear which of two
alternative productions to use to expand a nonterminal “A”, we may be able to rewrite the “A”
productions to refer the decision until we have seen enough of the input to make the right
choice.
13. Left factor the following grammar:
S → iEtS | iEtSeS |a
E → b.
Ans:
The left factored grammar is, S
→ iEtSS′ | a
S′ → eS | εE
→b
14. What is parsing?
CS3501_CD
4931_GRACE College of Engineering, Thoothukudi
Parsing is the process of determining if a string of tokens can be generated by a grammar.
15. What is Top Down parsing?
Starting with the root, labeled, does the top- down construction of a parse tree with
the starting nonterminal, repeatedly performing the following steps.
i. At node n, labeled with non terminal “A”, select one of the productions for “A” and
construct children at n for the symbols on the right side of the production.
ii. Find the next node at which a sub tree is to be constructed.
16. What do you mean by Recursive Descent Parsing?
Recursive Descent Parsing is top down method of syntax analysis in which we execute
a set of recursive procedures to process the input. A procedure is associated with each
nonterminal of a grammar.
17. What is meant by Predictive parsing?
A special form of Recursive Descent parsing, in which the look-ahead symbol
unambiguously determines the procedure selected for each nonterminal, where no
backtracking is required.
18. Define Bottom Up Parsing.
Parsing method in which construction starts at the leaves and proceeds towardsthe
root is called as Bottom Up Parsing.
19. What is Shift-Reduce parsing?
A general style of bottom-up syntax analysis, which attempts to construct a parse tree
for an input string beginning at the leaves and working up towards the root.
20. Define handle. What do you mean by handle pruning?
An Handle of a string is a sub string that matches the right side of
production and whose reduction to the nonterminal on the left side of the
production represents one step along the reverse of a rightmost derivation.
The process of obtaining rightmost derivation in reverse is known as Handle
Pruning.
21. Define LR (0) items.
An LR (0) item of a grammar G is a production of G with a dot at some positionof
the right side. Thus the production A → XYZ yields the following four items,
A → .XYZ A
→ X.YZA →
XY.Z A →
XYZ.
22. What do you mean by viable prefixes?
The set of prefixes of right sentential forms that can appear on the stack of a shift-
reduce parser are called viable prefixes.
A viable prefix is that it is a prefix of a right sentential form that does not
continue the past the right end of the rightmost handle of that sentential form.
23. What is meant by an operator grammar? Give an example.
A grammar is operator grammar if,
CS3501_CD
4931_GRACE College of Engineering, Thoothukudi
i. No production rule involves “ ” on the right side.
ii. No production has two adjacent nonterminals on the right side..
Ex:
E → E+E | E-E | E*E | E/E | E E | (E) | -E | id
24. What are the disadvantages of operator precedence parsing? May/June 2007
i. It is hard to handle tokens like the minus sign, which has two different precedences.
ii. Since the relationship between a grammar for the language being parsed and the operator
– precedence parser itself is tenuous, one cannot always be sure the parser accepts exactly
the desired language.
iii. Only a small class of grammars can be parsed using operator precedence techniques.
25. State error recovery in operator-Precedence Parsing.
There are two points in the parsing process at which an operator-precedence parser
can discover the syntactic errors:
i. If no precedence relation holds between the terminal on top of the stack and the
current input.
ii. If a handle has been found, but there is no production with this handle as a right
side.
26. LR (k) parsing stands for what?
The “L” is for left-to-right scanning of the input, the “R” for constructing a rightmost
derivation in reverse, and the k for the number of input symbols of lookahead that are used in
making parsing decisions.
27. Define LR grammar.
A grammar for which we can construct a parsing table is said to be an
LR grammar.
28. What are kernel and non kernel items?
i. The set of items which include the initial item, S S, and all items whose dots
are not at the left end are known as kernel items.
ii. The set of items, which have their dots at the left end, are known as non kernel
items.
29. Why SLR and LALR are more economical to construct than canonical LR?
For a comparison of parser size, the SLR and LALR tables for a grammar always have
the same number of states, and this number is typically several hundred states for a language
like Pascal. The canonical LR table would typically have several thousand states for the
same size language. Thus, it is much easier and more economical to construct SLR and
LALR tables than the canonical LR tables.
30. What is ambiguous grammar? Give an example.
A grammar G is said to be ambiguous if it generates more than one parse trees for
sentence of language L(G).
Example: E-> E+E|E*E|id
CS3501_CD
4931_GRACE College of Engineering, Thoothukudi
PART B
1. Explain left recursion and Left Factoring.
2. Eliminate left recursion and left factoring for the following grammar.
E→E+T|E-T|T
T → a | b | ( E ).
3. What is an ambiguous and un ambiguous grammar?
Identify the following grammar is ambiguous or not.
E→E+E | E*E | (E)|-E |id for the sentence id+id*id
4. Illustrate the predictive parser for the following grammar.
S→ (L) | a
L→ L, S | S
5. Evaluate predictive parsing table and parse the string id+id*id. find FIRST and FOLLOW.
E→E+T | T T→T*F | F
F→(E) | id
6. Construct Stack implementation of shift reduce parsing for the grammar
E->E+E
E->E*E
E->(E)
E->id and the input string id1+id2*id3
7. Describe on detail about the role of parser.
8. Discuss about the context-free grammar.
9. What are the Error recovery techniques used in Predictive parsing? Explain in detail.
10. Analyze the following grammar is a LR(1) grammar and construct LALR parsing table.
S → Aa | bAc | dC | bda
A→ d.
Parse the input string bdc using the table generated.
11. Define SLR (1) parser. Describe the Steps for the SLR parser.
12. Predict the following grammar for generate the SLR parsing table.
E→E+T | T
T→T*F | F
13. Consider the following grammar
S → AS|b
A→SA|a.
Construct the SLR parse table for the grammar.
Show the actions of the parser for the input string “abab”.
CS3501_CD
4931_GRACE College of Engineering, Thoothukudi
14. Examine the following grammar using canonical parsing table.
E→E+TF→(E)
E → T F → id.
T→T*F
CS3501_CD