Chapter 3 - UNIT 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

UNIT 1 – CHPATER 3 : Syntax Analysis

Role of the parser

The parser or syntactic analyzer obtains a string of tokens from the lexical
analyzer and verifies that the string can be generated by the grammar for the
source language. It reports any syntax errors in the program. It also recovers
from commonly occurring errors so that it can continue processing its input.

1. It verifies the structure generated by the tokens based on the grammar.


2. It constructs the parse tree.
3. It reports the errors.
4. It performs error recovery.
Parsing Techniques
 Universal Parsing Technique. (Discussed in Offline Class)
 Top down Parsing Technique. (Discussed in Offline Class)
 Bottom up Parsing Technique. (Discussed in Offline Class)
Top down parsing Techniques
 Issues or Problems in Top down parsing. (Discussed in Offline Class)
 Computation of FIRST and FOLLOW sets. (Discussed in Offline Class)
 The Top down Parsing Techniques are :
o Recursive Descent Parsing.
o Predictive Parsing.
o LL (1) Parsing.
Recursive Descent Parsing Technique

o Recursive descent parsing is one of the top-down parsing techniques


that uses a set of recursive procedures to scan its input.
o This parsing method may involve backtracking, that is, making repeated
scans of the input.
o A parse that uses collection of recursive procedures for parsing the given
input string is called Recursive Descent Parsing Technique.
o Basic steps for defining the procedures for Recursive Descent Parsing
Technique.
 If the input symbol is non terminal then a call to the procedure
corresponding to the non terminal is made.
 If the input symbol is terminal then it is matched with the look a head
from input. The look ahead pointer has to the advanced on matching of
the input symbol.
 For each non terminal a separate procedure is written and body of the
procedure (code) is RHS of the corresponding non terminal.
 The RHS of the production rule is directly converted in to a program
code symbol by symbol.
 If the production rule has many alternates then all these alternates has
to be combined into a single body of procedure.
Example:
Consider the grammar for arithmetic expressions
E → E+T | T
T → T*F | F
F → (E) | id
Solution:
After eliminating the left-recursion the grammar becomes,
E → TE’
E’ → +TE’ | ε
T → FT’
T’ → *FT’ | ε
F → (E) | id
Now we can write the procedure for grammar as follows:

Recursive procedures:
Procedure E() E → TE’
begin
T( );
EPRIME( );
End
Procedure EPRIME( ) E’ → +TE’
begin
If input_symbol=’+’ then
ADVANCE( );
T( );
EPRIME( );
end
Procedure T( ) T → FT’
begin
F( );
TPRIME( );
End
Procedure TPRIME( ) T’ → *FT’ | ε
begin
If input_symbol=’*’ then
ADVANCE( );
F( );
TPRIME( );
End

Procedure F( ) F → (E) | id
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: Parsing the input string: id+id*id

Conclusion: Therefore the given input string is successfully parsed and


gets accepted.
Predictive Parsing Technique
 Predictive parsing is a special case of recursive descent parsing where no
backtracking is required.
 The key problem of predictive parsing is to determine the production to
be applied for a non-terminal in case of alternatives.
The table-driven predictive parser has an input buffer, stack, a parsing table
and an output stream.
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:
o If X = a = $, the parser halts and announces successful completion of parsing.
o If X = a ≠ $, the parser pops X off the stack and advances the input pointer to the
next input symbol.
o 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 UVW
If M[X, a] = error, the parser calls an error recovery routine.
Algorithm for construction of predictive parsing table:
Input : Grammar G
Output : Parsing table M
Method :
1. For each production A → α of the grammar, do steps 2 and 3.
2. For each terminal a in FIRST(α), add A → α to M[A, a].
3. If ε is in FIRST(α), add A → α to M[A, b] for each terminal b in FOLLOW(A).
If ε is in FIRST(α) and $ is in FOLLOW(A) , add A → α to M[A, $].
4. Make each undefined entry of M be error.
Example:
Consider the following grammar:
E → E+T | T
T → T*F | F
F → (E) | id
Solution:
o Step 1: Issues in TOP DOWN PARSING.
o Step 2: Compute First () and Follow () for all non-terminals.
o Step 3: Construction of Predictive parsing Table.
o Step 4: Parsing the Input String.

Step 1:
After eliminating left-recursion the grammar is:
E → TE’
E’ → +TE’ | ε
T → FT’
T’ → *FT’ | ε
F → (E) | id
Step 2:

Compute First ( ):
FIRST(E) = { ( , id}
FIRST(E’) ={+ , ε }
FIRST(T) = { ( , id}
FIRST(T’) = {*, ε }
FIRST(F) = { ( , id }
Compute Follow ( ):
FOLLOW(E) = { $, ) }
FOLLOW(E’) = { $, ) }
FOLLOW(T) = { +, $, ) }
FOLLOW(T’) = { +, $, ) }
FOLLOW(F) = {+, * , $ , ) }

Step 3:
Construction of predictive parsing table: (From the production if α is not an ε
then apply Rule 2 and if the if α is an ε then apply Rule 3)
 E → TE’ is of the form A → α then apply Rule 2(i.e find FIRST(α))
o Add the above production into FIRST(T)
 E’ → +TE’ is of the form A → α then apply Rule 2(i.e find FIRST(α))
o Add the above production into FIRST(+)
 E’ → ε is of the form A → α then apply Rule 3(i.e find FOLLOW(A))
o Add the above production into FOLLOW (E’)
 T → FT’ is of the form A → α then apply Rule 2(i.e find FIRST(α))
o Add the above production into FIRST(F)
 T’ → *FT’ is of the form A → α then apply Rule 2(i.e find FIRST(α))
o Add the above production into FIRST(*)
 T’ → ε is of the form A → α then apply Rule 3(i.e find FOLLOW(A))
o Add the above production into FOLLOW(T’)
 F → (E) is of the form A → α then apply Rule 2(i.e find FIRST(α))
o Add the above production into FIRST(()
 F →id is of the form A → α then apply Rule 2(i.e find FIRST(α))
o Add the above production into FIRST(id)
Step 4:

Conclusion: Therefore the given input string is successfully parsed and


gets accepted.
LL (1) Parsing Technique
 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.
 Here LL(1) means :
o Here the 1st L represents that the scanning of the Input will be
done from Left to Right manner and
o The second L shows that in this parsing technique we are going to
use Left most Derivation Tree.
o And finally, the 1 represents the number of look-ahead, which
means how many symbols you are going to see when you want to
make a decision.

 Algorithm to construct LL (1) Parsing Table :


o Step 1: Issues in TOP DOWN PARSING.
o Step 2: Compute First () and Follow () for all non-terminals.
o Step 3: Construction of Predictive parsing Table.(if it is LL(1) then
go for Step 4.
o Step 4: Parsing the Input String.
Example 1:
Consider the following grammar:
E → E+T | T
T → T*F | F
F → (E) | id
Check whether the given grammar is LL(1) or Not.

Solution:
o Step 1: Issues in TOP DOWN PARSING.
o Step 2: Compute First () and Follow () for all non-terminals.
o Step 3: Construction of Predictive parsing Table.(if it is LL(1) then
go for Step 4.
o Step 4: Parsing the Input String.

Step 1:
After eliminating left-recursion the grammar is:
E → TE’
E’ → +TE’ | ε
T → FT’
T’ → *FT’ | ε
F → (E) | id
Step 2:

Compute First ( ):
FIRST(E) = { ( , id}
FIRST(E’) ={+ , ε }
FIRST(T) = { ( , id}
FIRST(T’) = {*, ε }
FIRST(F) = { ( , id }
Compute Follow ( ):
FOLLOW(E) = { $, ) }
FOLLOW(E’) = { $, ) }
FOLLOW(T) = { +, $, ) }
FOLLOW(T’) = { +, $, ) }
FOLLOW(F) = {+, * , $ , ) }
Step 3:
 Construction of predictive parsing table: (From the production if α is
not an ε then apply Rule 2 and if the if α is an ε then apply Rule 3)

 E → TE’ is of the form A → α then apply Rule 2(i.e find FIRST(α))


o Add the above production into FIRST(T)
 E’ → +TE’ is of the form A → α then apply Rule 2(i.e find FIRST(α))
o Add the above production into FIRST(+)
 E’ → ε is of the form A → α then apply Rule 3(i.e find FOLLOW(A))
o Add the above production into FOLLOW (E’)
 T → FT’ is of the form A → α then apply Rule 2(i.e find FIRST(α))
o Add the above production into FIRST(F)
 T’ → *FT’ is of the form A → α then apply Rule 2(i.e find FIRST(α))
o Add the above production into FIRST(*)
 T’ → ε is of the form A → α then apply Rule 3(i.e find FOLLOW(A))
o Add the above production into FOLLOW(T’)
 F → (E) is of the form A → α then apply Rule 2(i.e find FIRST(α))
o Add the above production into FIRST(()
 F →id is of the form A → α then apply Rule 2(i.e find FIRST(α))
o Add the above production into FIRST(id)
 Therefore the given grammar is LL(1) grammar, then go to Step 4:
Step 4:

Conclusion: Therefore the given input string is successfully parsed and


gets accepted.
Example 2:
Consider this following grammar:

S → iEtS | iEtSeS | a

E→b

Check whether the given grammar is LL(1) or Not.


Solution:
o Step 1: Issues in TOP DOWN PARSING.
o Step 2: Compute First () and Follow () for all non-terminals.
o Step 3: Construction of Predictive parsing Table.(if it is LL(1) then
go for Step 4.
o Step 4: Parsing the Input String.

Step 1:
 Here no need to eliminate the left recursion.
 And need to apply left factoring process.
After eliminating left factoring, we have

S → iEtSS’ | a

S’→ eS | ε

E→b

Step 2:
Compute First () and Follow ( ):
FIRST(S) = { i, a }

FIRST(S’) = {e, ε }

FIRST(E) = { b}
FOLLOW(S) = { $ ,e }
FOLLOW(S’) = { $ ,e }
FOLLOW(E) = {t}

Step 3:

Construction of predictive parsing table: (From the production if α is not an ε


then apply Rule 2 and if the if α is an ε then apply Rule 3)

 S → iEtSS’ is of the form A → α then apply Rule 2

o Add the above production into FIRST(i)

 S →a is of the form A → α then apply Rule 2

o Add the above production into FIRST(a)

 S’→ eS is of the form A → α then apply Rule 2

o Add the above production into FIRST(e)

 S’→ε is of the form A → α then apply Rule 3

o Add the above production into FOLLOW(S’)

 E→b is of the form A → α then apply Rule 2

o Add the above production into FIRST(b)


 Since there are more than one production in some fields in parsing table.
 Therefore the given grammar is not LL(1) grammar.

You might also like