Professional Documents
Culture Documents
Chapter 3 - UNIT 1
Chapter 3 - UNIT 1
Chapter 3 - UNIT 1
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.
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
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:
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)
S → iEtS | iEtSeS | a
E→b
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: