Professional Documents
Culture Documents
UNIT 2 NOTES
and
easy-to-understand
syntactic
COMPILER DESIGN
UNIT 2 NOTES
The grammar in Fig. 4.2 defines simple arithmetic expressions. In this grammar, the terminal
symbols are id,+,-,*,( ,)
The nonterminal symbols are expression, term and factor, and expression is the start symbol
Notational Conventions
To avoid always having to state that "these are the terminals," "these are the nontermiaals ," and
so on, the following notational conventions for grammars are used
1. These symbols are terminals:
(a) Lowercase letters early in the alphabet, such as a, b, e.
(b) Operator symbols such as +, r, and so on.
(c) Punctuation symbols such as parentheses, comma, and so on.
(d) The digits 0,1,. . . ,9.
(e) Boldface strings such as id or if, each of which represents a single terminal symbol.
2. These symbols are nonterminals:
(a) Uppercase letters early in the alphabet, such as A, B, C.
(b) The letter S, which, when it appears, is usually the start symbol.
(c) Lowercase, italic names such as expr or stmt.
(d) When discussing programming constructs, uppercase letters may be used to represent non
terminals for the constructs. For example, nonterminals for expressions, terms, and factors are
often represented by E, T, and F, respectively.
3. Uppercase letters late in the alphabet, such as X, Y, 2, represent grammar symbols; that is,
either non terminals or terminals.
4. Lowercase letters late in the alphabet, chiefly u, v, . . . , x , represent (possibly empty) strings
of terminals.
5. Lowercase Greek letters, a, ,O, y for example, represent (possibly empty) strings of grammar
symbols. Thus, a generic production can be written as A + a, where A is the head and a the body.
6. A set of productions A -+ al, A + a2,. . . , A -+ a k with a common head A (call them Aproductions), may be written A + a1 / a s I . . I ak. Call a l , a2,. . . , a k the alternatives for A.
2
COMPILER DESIGN
UNIT 2 NOTES
7. Unless stated otherwise, the head of the first production is the start symbol
Using these conventions, the grammar of above example can be rewritten concisely as
Derivations
The construction of a parse tree can be made precise by taking a derivational view, in which
productions are treated as rewriting rules. Beginning with the start symbol, each rewriting step
replaces a non terminal by the body of one of its productions. This derivational view corresponds
to the top-down construction of a parse tree, but the precision afforded by derivations will be
especially helpful when bottom-up parsing is discussed. As we shall see, bottom-up parsing is
related to a class of derivations known as "rightmost" derivations, in which the rightmost non
terminal is rewritten at each step.
Types of derivations:
The two types of derivation are:
1. Left most derivation
2. Right most derivation.
In leftmost derivations, the leftmost non-terminal in each sentinel is always chosen first for
replacement.
In rightmost derivations, the rightmost non-terminal in each sentinel is always chosen first for
replacement.
Example:
Given grammar G : E E+E | E*E | ( E ) | - E | id Sentence to be derived : - (id+id)
LEFTMOST DERIVATION
RIGHTMOST DERIVATION
E-E
E-(E)
E-(E+E)
E-(id+E)
E-(id+id)
E-E
E-(E)
E-(E+E)
E-(E+id)
E-(id+id)
String that appear in leftmost derivation are called left sentinel forms.
String that appear in rightmost derivation are called right sentinel forms.
Sentinels:
3
COMPILER DESIGN
UNIT 2 NOTES
For each sentential form i in the derivation, we can construct a parse tree whose yield is i. The
process is an induction on i.
BASIS: The tree for 1 = A is a single node labeled A.
COMPILER DESIGN
UNIT 2 NOTES
COMPILER DESIGN
UNIT 2 NOTES
COMPILER DESIGN
UNIT 2 NOTES
such
as
identifiers,
CONTEXT-FREE GRAMMAR
1. It consists of a quadruple where S start symbol, P production, T terminal, V
variable or non- terminal.
2. It is used to check whether the given input is valid or not using derivation.
3. The context-free grammar has set of productions.
4. It has start symbol.
5. It is useful in describing nested structures such as balanced parentheses, matching
begin-ends and so on.
Eliminating Ambiguity
Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity. As an example,
we shall eliminate the ambiguity from the following "danglingelse" grammar:stmt +
Here "other" stands for any other statement. According to this grammar, the compound
conditional statement if El then S1 else if E2 then S2 else S3
has the parse tree shown in Fig. 4.8.' Grammar (4.14) is ambiguous since the string if El then if
E2 then S1 else S2. has the two parse trees shown in Fig. 4.9.
In all programming languages with conditional statements of this form, the first parse tree is
preferred. The general rule is, "Match each else with the closest unmatched then." This
disambiguating rule can theoretically be incorporated directly into a grammar, but in practice it is
rarely built into the productions.
COMPILER DESIGN
UNIT 2 NOTES
We can rewrite the dangling-else grammar (4.14) as the following unambiguous grammar. The
idea is that a statement appearing between a then and an else must be "matched" ; that is, the
interior statement must not end with an unmatched or open then. A matched statement is either
an if-then-else statement containing no open statements or it is any other kind of unconditional
statement. Thus, we may use the grammar in Fig. 4.10. This grammar generates the same strings
as the dangling-else grammar (4.14), but it allows only one parsing for string (4.15); namely, the
one that associates each else with the closest previous unmatched then.
COMPILER DESIGN
UNIT 2 NOTES
A A |
without changing the set of strings derivable from A.
Example : Consider the following grammar for arithmetic expressions:
EE+T|T
TT*F|F
F (E)|id
First eliminate the left recursion for E
ETE
E +TE |
Then eliminate for T as
TFT
T *FT |
Thus the obtained grammar after eliminating left recursion is
ETE
E+TE|
T FT
T*FT|
F (E)|id
Algorithm to eliminate left recursion:
1. Arrange the non-terminals in some order A1, A2 . . . An.
2. for i := 1 to n do begin
for j := 1 to i-1 do begin
replace each production of the form Ai Aj by the productions Ai 1 | 2 | . . . | k where
Aj 1 | 2 | . . . | k are all the current Aj- productions
end
eliminate the immediate left recursion among the Ai-productions
end
Left factoring:
Left factoring is a grammar transformation that is useful for producing a grammar suitable
for predictive parsing. When it is not clear which of two alternative productions to use to expand a
9
COMPILER DESIGN
UNIT 2 NOTES
non-terminal A, we can rewrite the A-productions to defer the decision until we have seen
enough of the input to make the right choice.
If there is any production A 1 | 2 , it can be rewritten as
A A
A 1 | 2
Consider the grammar ,
G : S iEtS | iEtSeS | a
Eb
Left factored, this grammar becomes
S iEtSS | a
S eS |
Eb
PARSING
It is the process of analyzing a continuous stream of input in order to determine its
grammatical structure with respect to a given formal grammar.
Parse tree:
Graphical representation of a derivation or deduction is called a parse tree. Each interior
node of the parse tree is a non-terminal; the children of the node can be terminals or nonterminals.
Types of parsing:
1.Top down parsing
2.Bottom up parsing
Top-down parsing : A parser can start with the start symbol and try to transform it to the
input string. Example : LL Parsers.
Bottom-up parsing :A parser can start with input and attempt to rewrite it into the start
symbol.Example : LR Parsers.
Top-down parsing:
Top-down parsing can be viewed as the problem of constructing a parse tree for the input string,
starting from the root and creating the nodes of the parse tree in preorder Equivalently, top-down
parsing can be viewed as finding a leftmost derivation for an input string.
Example
10
COMPILER DESIGN
UNIT 2 NOTES
Recursive-Descent Parsing
A recursive-descent parsing program consists of a set of procedures, one for each non terminal.
Execution begins with the procedure for the start symbol, which halts and announces success if
its procedure body scans the entire input string. Pseudo code for a typical non terminal appears in
Fig. 4.13. Note that this pseudo code is nondeterministic, since it begins by choosing the Aproduction to apply in a manner that is not specified.
General recursive-descent may require backtracking; that is, it may requirerepeated scans over
the input However, backtracking is rarely needed to parse programming language constructs, so
backtracking parsers are not seen frequently.
COMPILER DESIGN
UNIT 2 NOTES
T FT
T*FT
F (E) | id
Now we can write the procedure for grammar as follows:
Recursive procedure:
Procedure E()
begin
T( );
EPRIME( );
end
12
COMPILER DESIGN
UNIT 2 NOTES
Procedure EPRIME( )
begin
If input_symbol=+ then ADVANCE( );
T( );
EPRIME( );
end
Procedure T( )
begin
F( );
TPRIME( );
end
Procedure TPRIME( )
begin
If input_symbol=* then ADVANCE( );
F( );
TPRIME( );
end
Procedure F( )
begin
If input-symbol=id then ADVANCE( );
else if input-symbol=( then ADVANCE( );
E( );
else if input-symbol=) then ADVANCE( );
end
else ERROR( );
Stack implementation:
To recognize input id+id*id :
PROCEDURE
INPUT STRING
E( )
id+id*id
id+id*id
T( )
13
COMPILER DESIGN
UNIT 2 NOTES
id+id*id
F( )
id+id*id
ADVANCE( )
id+id*id
TPRIME( )
id+id*id
EPRIME( )
id+id*id
ADVANCE( )
id+id*id
T( )
id+id*id
F( )
id+id*id
ADVANCE( )
id+id*id
TPRIME( )
id+id*id
ADVANCE( )
id+id*id
F( )
id+id*id
ADVANCE( )
id+id*id
TPRIME( )
The table-driven parser in Fig. 4.19 has an input buffer, a stack containing a sequence of
grammar symbols, a parsing table constructed by Algorithm 4.31, and an output stream. The
input buffer contains the string to be parsed, followed by the endmarker $. We reuse the symbol
$ to mark the bottom of the stack, which initially contains the start symbol of the grammar on top
of $. The parser is controlled by a program that considers X, the symbol on top of the stack, and
a, the current input symbol. If X is a nonterminal, the parser chooses an X-production by
consulting entry M[X, a] of the parsing table IM. (Additional code could be executed here, for
example, code to construct a node in a parse tree.) Otherwise, it checks for a match between the
terminal X and current input symbol a
14
COMPILER DESIGN
UNIT 2 NOTES
Input buffer:
It consists of strings to be parsed, followed by $ to indicate the end of the input string.
Stack:
It contains a sequence of grammar symbols preceded by $ to indicate the bottom of the stack.
Initially, the stack contains the start symbol on top of $.
Parsing table:
It is a two-dimensional array M[A, a], where A is a non-terminal and a is a terminal.
Predictive parsing program:
The parser is controlled by a program that considers X, the symbol on top of stack, and a, the
current input symbol. These two symbols determine the parser action. There are three
possibilities:
1. If X = a = $, the parser halts and announces successful completion of parsing.
2. If X = a $, the parser pops X off the stack and advances the input pointer to the next
input symbol.
3. If X is a non-terminal , the program consults entry M[X, a] of the parsing table M. This
entry will either be an X-production of the grammar or an error entry.
If M[X, a] = {X UVW},the parser replaces X on top of the stack by WVU. If M[X, a] = error,
the parser calls an error recovery routine.
Algorithm for nonrecursive predictive parsing:
Input : A string w and a parsing table M for grammar G.
Output : If w is in L(G), a leftmost derivation of w; otherwise, an error indication.
Method : Initially, the parser has $S on the stack with S, the start symbol of G on top, and w$ in
the input buffer. The program that utilizes the predictive parsing table M to produce a parse for
the input is as follows:
set ip to point to the first symbol of w$;
repeat
let X be the top stack symbol and a the symbol pointed to by ip; if X is a terminal or $ then
if X = a then
15
COMPILER DESIGN
UNIT 2 NOTES
COMPILER DESIGN
UNIT 2 NOTES
F(E)|id
After eliminating left-recursion the grammar is
E TE
E+TE|
TFT
T*FT|
F (E)|id
First( ) :
FIRST(E) = { ( , id}
FIRST(E) ={+ , }
FIRST(T) = { ( , id}
FIRST(T) = {*, }
FIRST(F) = { ( , id }
Follow( ):
FOLLOW(E) = { $, ) }
FOLLOW(E) = { $, ) }
FOLLOW(T) = { +, $, ) }
FOLLOW(T) = { +, $, ) }
FOLLOW(F) = {+, * , $ , ) }
Predictive parsing table :
Stack
$E
Input
id+id*id $
17
Output
COMPILER DESIGN
UNIT 2 NOTES
$ET
$ETF
id+id*id $
id+id*id $
ETE
TFT
$ETid
$ET
$E
$ET+
id+id*id $
+id*id $
+id*id $
+id*id $
Fid
$ET
$ETF
$ETid
$ET
$ETF*
id*id $
id*id $
id*id $
*id $
*id $
$ETF
$ETid
$ET
id $
id $
$
$E
$
$
$
T
E +TE
TFT
Fid
T *FT
Fid
T
E
LL(1) grammar:
The parsing table entries are single entries. So each location has not more than one entry. This
type of grammar is called LL(1) grammar.
Consider this following grammar:
SiEtS | iEtSeS| a
Eb
After eliminating left factoring, we have
SiEtSS|a
S eS |
Eb
To construct a parsing table, we need FIRST() and FOLLOW() for all the non-terminals.
FIRST(S) = { i, a }
FIRST(S)={e,}
FIRST(E) = { b}
FOLLOW(S) = { $ ,e }
FOLLOW(S)={$,e}
FOLLOW(E) = {t}
18
COMPILER DESIGN
UNIT 2 NOTES
Parsing table:
Since there are more than one production, the grammar is not LL(1) grammar.
Actions performed in predictive parsing:
1. Shift
2. Reduce
3. Accept
4. Error
Implementation of predictive parser:
1. Elimination of left recursion, left factoring and ambiguous grammar.
2. Construct FIRST() and FOLLOW() for all non-terminals.
3. Construct predictive parsing table.
4. Parse the given input string using stack and parsing table.
Panic-mode error recovery is based on the idea of skipping symbols on the the input until a token
in a selected set of synchronizing tokens appears. Its effectiveness depends on the choice of
synchronizing set. The sets should bechosen so that the parser recovers quickly from errors that
are likely to occur in practice. Some heuristics are as follows:
1.As a starting point, place all symbols in FOLLOW(A) into the synchronizing set for
nonterminal A. If we skip tokens until an element of FOLLOW(A) is seen and pop A from the
stack, it is likely that parsing can continue.
2.It is not enough to use FOLLOW(A) as the synchronizing set for A. For example, if
semicolons terminate statements, as in C, then keywords that begin statements may not appear in
the FOLLOW set of the nonterminal representing expressions. A missing semicolon after an
assignment may therefore result in the keyword beginning the next statement being skipped.
Often, there is a hierarchical structure on constructs in a language; for example, expressions
appear within statements, which appear within blocks, and so on. We can add to the
synchronizing set of a lower-level construct the symbols that begin higher-level constructs. For
19
COMPILER DESIGN
UNIT 2 NOTES
example, we might add keywords that begin statements to the synchronizing sets for the
nonterminals generating expressions.
3. If we add symbols in FIRST(A) to the synchronizing set for nonterminal
A, then it may be possible to resume parsing according to A if a symbol
in FIRST(A) appears in the input.
4. If a nonterminal can generate the empty string, then the production deriving E can be used as a
default. Doing so may postpone some error detection, but cannot cause an error to be missed.
This approach reduces the number of nonterminals that have to be considered during error
recovery.
5. If a terminal on top of the stack cannot be matched, a simple idea is to pop the terminal, issue
a message saying that the terminal was inserted,and continue parsing. In effect, this approach
takes the synchronizing set of a token to consist of all other tokens.
20