Docsity

Recursive Descent Parsing: Algorithms and Examples - Prof. Chao Wen Tseng, Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 330: Organization of
Programming Languages
Recursive Descent Parser Example
CMSC 330 2
Recursive Descent Parser
• For terminals, create function match(a)
– If lookahead is ait consumes the lookahead by
advancing the lookahead to the next token, and returns
• For each nonterminal N, create a function parse_N
– Let N

1
| ... |
k
be the productions of N
– For each production N

i
(where
i
=
1
...
n
)
– If the lookahead is in First(
i
)
• C all parse_
1
( ); ... ; parse_
n
( )
CMSC 330 3
First Sets
• Definition
– First(
), for any terminal or nonterminal
, is the set of
initial terminals of all strings that
may expand to
• For a terminal a
– First(a) = { a }
• For a nonterminal N
– If N

, then add
to First(N)
– If N
1
2
...
n
, then add
• First(
1
) if
∉First(
1
)
• Else add { First(
1
) –
}
First(
2
...
n
)
CMSC 330 4
Left Recursion Elimination Algorithm
• Given grammar
– A
A
1
| A
2
| … | A
n
|
• Rewrite grammar as
– A

L
– L
1
L |
2
L | … |
n
L |
• Repeat as necessary
CMSC 330 5
Left Factoring Algorithm
• Given grammar
– A
x
1
| x
2
| … | x
n
|
• Rewrite grammar as
– A
xL |
– L
1
|
2
| … |
n
• Repeat as necessary
CMSC 330 6
Example 1 – Left Factoring
Consider the grammar
S
T + S | T
T
U * T | U
U
(S) | V
V
0 | 1 | ... | 9
Common prefix requires
applying left factoring
S
T+ S | T
T
U* T | U
Left factor S
S
T + S | T
S
T L
L
+ S |
Left factor T
T
U * T | U
T
U M
M
* T |
pf3

Partial preview of the text

Download Recursive Descent Parsing: Algorithms and Examples - Prof. Chao Wen Tseng and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

Recursive Descent Parser Example

CMSC 330 2

Recursive Descent Parser

  • For terminals, create function match(a)
    • If lookahead is a it consumes the lookahead by

advancing the lookahead to the next token, and returns

  • For each nonterminal N, create a function parse_N
    • Let N ^1 | ... | k be the productions of N
    • For each production N ^ i (where i =  1 ... n )
    • If the lookahead is in First( i)
      • Call parse_ 1 ( ); ... ; parse_ (^) n( )

CMSC 330 3

First Sets

  • Definition
    • First( ), for any terminal or nonterminal , is the set of

initial terminals of all strings that  may expand to

  • For a terminal a
    • First(a) = { a }
  • For a nonterminal N
    • If N ^ , then add to First(N)
    • If N  1  2 ... n, then add
      • First( 1 ) if ∉ First( 1 )
      • Else add { First( 1 ) – } First( 2 ... n)

CMSC 330 4

Left Recursion Elimination Algorithm

  • Given grammar
    • A A  1 | A 2 | … | A (^) n | 
  • Rewrite grammar as
    • A ^ L
    • L  1 L |  2 L | … | n L | 
  • Repeat as necessary

CMSC 330 5

Left Factoring Algorithm

  • Given grammar
    • A x 1 | x 2 | … | x (^) n | 
  • Rewrite grammar as
    • A xL | 
    • L  1 |  2 | … | n
  • Repeat as necessary

CMSC 330 6

Example 1 – Left Factoring

Consider the grammar

S T + S | T
T U * T | U
U (S) | V
V 0 | 1 | ... | 9

Common prefix requires

applying left factoring

S T + S | T

T U * T | U

Left factor S

S T + S | T

S T L

L + S | 

Left factor T

T U * T | U

T U M

M * T | 

CMSC 330 7

Example 1 – Computing First Sets

Grammar

S T L
L + S |
T U M
M * T |
U (S) | V
V 0 | 1 | ... | 9

Compute First Sets

First(V) = { 0…9 } First(U) = First( (S) ) First(V) = { ( } { (, 0…9 } = { (, 0…9 } First(M) = First( *T ) First( ) = { * } { } = { *, } First(T) = First( UM ) = First( U ) = { (, 0…9 } First(L) = First( +S ) First( ) = { + } { } = { +, } First(S) = First( TL ) = First( T ) = { (, 0…9 }

CMSC 330 8

Example 1 – Recursive Descent Parser

Grammar

S T L
L + S |
T U M
M * T |
U (S) | V
V 0 | 1 | ... | 9

Recursive descent parser

parse_S( ) { // S TL parse_T(); parse_L(); }

parse_L( ) { // L + S if (lookahead == “+”) { match(“+”); parse_S( ); }

// L 

else ; }

parse_T( ) { // T UM parse_U(); parse_M(); }

CMSC 330 9

Example 1 – Recursive Descent Parser

Grammar

S  T L

L  + S | 

T U M

M  * T | 

U  (S) | V

V  0 | 1 | ... | 9

Recursive descent parser parse_M( ) {

// M  * T

if (lookahead == “”) { match(“”); parse_T( ); }

else ; // M 

parse_U( ) {

if (lookahead == “(”) { // U (S)

match(“(”); parse_S( ); match(“)”); }

else parse_V( ); // U V

parse_V( ) {

if (lookahead == “0”) { // V  0

match(“0”);

} else if (lookahead == “1”) { // V  1

match(“1”); } else …

} else if (lookahead == “9”) { // V  9

match(“9”); } else error( ); } (^) CMSC 330 10

Example 1 – Parsing Input

Grammar

S  T L

L + S | 

T  U M

M  * T | 

U (S) | V

V  0 | 1 | ... | 9

Lookahead in red Parse “1+2” Remaining parse_S( ) “ 1 +2” parse_T( ) “ 1 +2” parse_U( ) “ 1 +2” parse_V( ) “ 1 +2” match(“1”) “ 1 +2” parse_M( ) “+2” parse_L( ) “+2” match(“+”) “+2” parse_S( ) “ 2 ” parse_T( ) “ 2 ” parse_U( ) “ 2 ” parse_V( ) “ 2 ” match(“2”) “ 2 ” “”

CMSC 330 11

Example 2 – Computing First Sets

Consider the grammar

S ABd | aBc

A 

B b | c

Compute First Sets

First(b) = {b} First(c) = {c} First(B) = First(b) First(c) = {b,c}

First( ) = { } First(A) = First( ) = { }

First(ABd) = {First(A) – } First(Bd) = { – } First(Bd) = First(Bd) = First(B) = {b, c} First(aBc) = { a }

First(S) = First(ABd) First(aBc) = {b, c} {a} = {a,b,c}

CMSC 330 12

Example 2 – Using First Sets

Grammar

S ABd | aBc

A 

B b | c

First sets for RHS

First(b) = {b} First(c) = {c}

First(ABd) = {b, c} First(aBc) = { a }

Grammar is predictive if…

First(ABd) First(aBc) = 

First(b) First(c) = 

And not left recursive…

Verify…

First(ABd) First(aBc)

= {b, c} { a }

First(b) First(c)

= {b} {c}

No overlap, grammar is predictive