Docsity

Top-down Parsing with a Parse Table (CS311), Study notes of Computer Science

Partial preview of the text

Download Top-down Parsing with a Parse Table (CS311) and more Study notes Computer Science in PDF only on Docsity!

CS311—Computational Structures

Top-down parsing

with a Parse Table

Lecture 11

Recall the theory:

a, a

pop

for each terminal a A

!!!!!!!!", A!!!!!!

pop, push(#)

for each rule A !# , # a sequence of

terminals and variables

push (S)

where S is the grammar's

start symbol

Recall the theory:

At each step, PDA can either

  1. read input a , iff a is on the stack (match), or

a, a

pop

for each terminal a A

!!!!!!!!", A!!!!!!

pop, push(#)

for each rule A !# , # a sequence of

terminals and variables

push (S)

where S is the grammar's

start symbol

Recall the theory:

At each step, PDA can either

  1. read input a , iff a is on the stack (match), or
  2. replace A on the stack with ω, where A →ω is a rule of

the grammar (derive).

a, a

pop

for each terminal a A

!!!!!!!!", A!!!!!!

pop, push(#)

for each rule A !# , # a sequence of

terminals and variables

push (S)

where S is the grammar's

start symbol

Recall the Practice

and moreConcat () =

if (couldBeSimple (!lookahead))

then

let val x = closure()

val y = moreConcat()

in case y of

NONE => SOME x

| SOME z => SOME(Concat(x,z))

end

else NONE

and couldBeSimple LeftParen = true

| couldBeSimple Hash = true

| couldBeSimple Zero = true

| couldBeSimple (Single _) = true

| couldBeSimple _ = false

moreConcat →

closure moreConcat

Top-down predictive parsers

Use an explicit stack instead of recursion.

Represent the transitions of the PDA in a

table, rather than as code

Choice of action of the parser (which

production to use to expend the stack

symbol) represented by three functions:

Nullable

First

Follow

Example First and Follow Sets

E → T Eʹ $

Eʹ → + T Eʹ

Eʹ → Λ

T → F Tʹ

Tʹ →

F Tʹ

Tʹ → Λ

F → ( E )

F → id

First E = { "(", "id"} Follow E = {")","$"}

First F = { "(", "id"} Follow F = {"+","

First T = { "(", "id"} Follow T = {{"+",")","$"}

First Eʹ = { "+", Λ } Follow E' = {")","$"}

First T' = { "

", Λ } Follow T' = {"+",")","$"}

  • First of a terminal is itself.
  • First can be extended to be

defined on a sequence of

symbols.

Nullable

if Λ is in First(symbol) then that symbol is

nullable.

Sometimes, rather than let Λ be a symbol, we

use an additional function Nullable.

‣

Nullable (E') = true

‣

Nullable(T') = true

‣

Nullable for all other symbols is false

E → T E' $

E' → + T E'

E' → Λ

T → F T'

T' → * F T'

T' → Λ

F → ( E )

F → id

Example First Computation

  • Terminals
    • First($) = {$} First() = {} First(+) = {+} ...
  • Empty Productions
    • (^) add Λ to First(E'), add Λ to First(T')
  • Other NonTerminals
    • (^) Computing from the lowest layer (F) up:

» (^) First(F) = {id , ( }

» (^) First(T') = { Λ, * }

» (^) First(T) = First(F) = {id, ( }

» (^) First(E') = { Λ, + }

» (^) First(E) = First(T) = {id, ( }

E → T E' $

E' → + T E'

E' → Λ

T → F T'

T' → * F T'

T' → Λ

F → ( E )

F → id

Computing FOLLOW

  • Use the following rules until nothing can be added

to any follow set:

  1. Place $ (the end of input marker) in FOLLOW(S) where S is the

start symbol.

(This is Hein’s rule 1)

  1. If A → a B b , then put everything in FIRST( b ) except Λ into

FOLLOW(B)

(This is Hein’s rule 3)

  1. If there is a production A → a B, or A → a B b

where FIRST( b ) contains Λ (i.e., nullable b ), then put everything

in FOLLOW(A) into FOLLOW(B)

(This is a combination of Hein’s rules 2 & 4)

For each production A → ω do steps 1–3 below:

  1. For each terminal a in First ω add A → ω to M[A, a ]
  2. If Λ is in First ω, add A → ω to M[A,b] for each terminal b

in Follow A.

  1. If Λ is in First ω and $ is in Follow A, add A → ω to M[A,$].

First E = { "(", "id"} Follow E = {")","$"}

First F = { "(", "id"} Follow F = {"+","

",”)”,"$"}

First T = { "(", "id"} Follow T = {{"+",")","$"}

First Eʹ = { "+", Λ } Follow E' = {")","$"}

First T' = { "

", Λ } Follow T' = {"+",")","$"}

Table from First and Follow

1 E → T E' $

2 E' → + T E'

3 | Λ

4 T → F T'

5 T' → * F T'

6 | Λ

7 F → ( E )

8 | id

terminals

+ * ) ( id $

n

o

n-

t

e

r

m

s

E

E’

T

T’

F

M[A,t]

1 1

2 3 3

4 4

6 5 6 6

7 8

Predictive Parsing Table

E

E’

T

T’

F

id

( ) (^) $

T E’ T E’

  • T E’ (^) Λ

Λ

F T’

F T’

Λ

  • F T’

Λ Λ

id ( E )

Example

Parse

E

E’

T

T’

F

id +^

( ) $

T E’ T E’

  • T E’ (^) Λ

Λ

F T’ F T’

Λ

  • F T’

Λ Λ

id ( E )

Stack Input

E x + y $

T E’ x + y $

F T’ E’ x + y $

id T’ E’ x + y $

T’ E’ + y $

E’ + y $

+ T E’ + y $

T E’ y $

F T’ E’ y $

id T’ E’ y $

T’ E’ $
E’ $

Top of stack