Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

K.

RAMAKRISHNAN COLLEGE OF ENGINEERING,


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING, III CSE

UCS1602 - COMPILER DESIGN


UNIT II – SYNTAX ANALYSIS
PART - A

1. What is the role of a parser?


Parser obtains a string of tokens from the lexical analyzer and verifies that the string can be generated by
the grammar for the source language. The parser also reports any syntax errors. It must also recover from
commonly occurring errors so that it can continue processing the remainder of its input
token
source Lexical parse Rest of intermediate
program analyzer
PARSER tree front end representation
get next
token
Symbol
table

2. List the factors to be considered for top-down parsing.


For top down parsing the grammar must be free of
i) Ambiguity
ii) Left recursion and
iii) Left factors.

3. Give two examples for each of top down parser and bottom up parser.
Top down parser
Recursive descent parser
Predictive parser
Bottom up parser
Operator precedence parser
Simple LR (SLR) parser

4. Define concrete and abstract syntax with example.


A concrete syntax tree or parse tree is an (ordered, rooted) tree that represents the syntactic structure of
a string according to some formal grammar. In a parse tree, the interior nodes are labeled by non-
terminals of the grammar, while the leaf nodes are labeled by terminals of the grammar. Parse trees are
distinct from abstract syntax trees (also known simply as syntax trees), in that their structure and
elements more concretely reflect the syntax of the input language.

An abstract syntax tree (AST), or just syntax tree, is a tree representation of the abstract syntactic
structure of source code written in a programming language. Each node of the tree denotes a construct
occurring in the source code. The syntax is 'abstract' in the sense that it does not represent every detail
that appears in the real syntax.

CONCRETE SYNTAX TREE ABSTRACT SYNTAX TREE


K. RAMAKRISHNAN COLLEGE OF ENGINEERING,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING, III CSE

5. Derive the string and construct a syntax tree for the input string ceaedbe using the grammar
S->SaA|A, A->AbB|B, B->cSd|e

S A AbB BbB cSdbB cSaAdbB cAaAdbB

cBaAdbB ceaAdbB ceaBdbB ceaedbB ceaedbe

6. Construct an equivalent unambiguous grammar for the grammar S-> aSbS | bSaS | ε

7. What is a predictive parser?


An efficient non-backtracking form of top-down parser is called a predictive parser. To construct a
predictive parser, we must know, given the current input symbol ‘a’ and the nonterminal A to be
expanded, which one of the alternatives of production
A α 1 | α2 | . . . | αn

is the unique alternative that derives a string beginning with ‘a’. (i.e) The proper alternative must be
detectable by looking at only the first symbol it derives.

8. Write the rule to eliminate left recursion in a grammar.

A grammar is left recursive if it has a nonterminal A such that there is a derivation


A A α for some string α. If there is a left recursive production of the format
A A α | β , it could be replaced by the non-left-recursive productions

A β AI

AI α AI | ε

without changing the set of strings derivable from A.

9. What is the significance of look ahead in LR(1) items?


K. RAMAKRISHNAN COLLEGE OF ENGINEERING,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING, III CSE

For a grammar to be LR(1), we must be able to recognize the occurrence of the right side of a
production, having seen all of what is derived from that right side with 1 input symbol of look ahead.
This requirement is far less stringent that LL grammars. LR grammars can describe more languages that
LL grammars.

10. Eliminate left recursion from the following grammar.


S->(L) | a
L -> L,S | S

Grammar after elimination of left recursion is :

S -> (L) | a
L -> S LI
LI -> ,S LI | ε

11. Define LR(0) items.

An LR(0) item of a grammar G is a production of G with a dot at some position of the right side. Thus,
production A → XYZ yields the four items
A→ .XYZ
A→ X.YZ
A→ XY.Z
A→ XYZ.

12. What is a CLR?

CLR : Canonical LR parser


It uses additional information in the set of items. The additional information is the look ahead character.
It is available as the second component of the items.

Eg. [A->α.β, a] - ‘a’ is the look ahead character.

Look ahead character is used to resolve shift reduce conflicts that may arise in SLR parsers for certain
grammars. CLR parser produces right most derivation in reverse.

13. Eliminate left factoring if there is one from the following grammar.
S AaB | Aac | d
A d
B ε
Ans:
The productions of S has left factors (S AaB | Aac )
Elimination of Left factors :
Productions of the form A αβ1 | αβ2 can be rewritten as

A αA’
A’ β 1 | β2

S AaB | Aac can be rewritten as


K. RAMAKRISHNAN COLLEGE OF ENGINEERING,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING, III CSE

S AaS’
S’ B|c

14. What is handle pruning?


A process of replacement of right hand side of the production by left hand side is called reduction.
Handle pruning is the process of obtaining a right most derivation in reverse called canonical reduction
sequence.

15. Check whether the following is an operator grammar.


S ASBS
S BSAS | ε
Ans: Conditions for an operator grammar are
i) There should not be any ε – production
ii) There should not be two adjacent non terminals
The given grammar violates both the conditions. Hence it is not an operator grammar.

16. Check whether the grammar is ambiguous or not?


S aSbS | bSaS | ε
Soln: The grammar is said to be ambiguous, if there is more than one left most derivation or more than
one rightmost derivation or more than one parse tree
Let w= abab

Parse tree 1 Parse tree 2


17. Consider the following grammar:
S->(L) | a
L ->L,S | S
Find the parse tree for the sentences i) (a,a) and ii) (a,(a,a))

Parse tree for (a,a) Parse tree for (a, (a,a))

18. Define operator grammar. [JUN/JUL 2009]


K. RAMAKRISHNAN COLLEGE OF ENGINEERING,
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING, III CSE

A grammar is said to be operator grammar if it possess the following properties:


1. No production on the right side is ε.
2. There should not be any production with two adjacent non terminals at the right hand side.
eg.
E→ EAE| (E)|id
A→ + | - | * | / E→ E+E | E-E | E*E | E/E | (E) | id
is not an operator grammar is an operator grammar

19. Define ambiguity of a grammar. Give example


A grammar is said to be ambiguous, if there is more than one left most derivation or more than one
rightmost derivation or more than one parse tree.
EX. w=id+id*id
The grammar E E+E |E*E|id
has 2 parse trees for the same string w=id+id*id.

20. When a grammar is said to be a CLR grammar? List out the steps in CLR parsing.

A grammar whose CLR parsing table has no multiply deifned entries is said to be LR(1) or CLR
grammar. A grammar for which a CLR parser can be constructed is said to be a CLR grammar.

21. List the Steps in CLR parsing

i) Construction of augumented grammar G’


ii) Compute FIRST, FOLLOW
iii) Define CLOSURE
iv) Define GOTO
v) Construct canonical collection of LR(1) items
vi) Construct CLR parsing table
vii) Construct stack implementation and hence check the given string is accepted by G or not.
22. List out the steps in LALR parsing.
i) Construction of augumented grammar G’
ii) Compute FIRST, FOLLOW
iii) Define CLOSURE
iv) Define GOTO
v) Construct canonical collection of LR(1) items
vi) Construct LALR(1) items
vii) Construct LALR parsing table
viii) Construct stack implementation and hence check the given
ix) string is accepted by G or not.

You might also like