Docsity

CFG Example and Recursive Descent Parser for LL(1) Grammars, Study notes of Computer Science

Partial preview of the text

Download CFG Example and Recursive Descent Parser for LL(1) Grammars and more Study notes Computer Science in PDF only on Docsity!

Compilers CS414-2008-

Parsing^ David Galles

Department of Computer ScienceUniversity of San Francisco

03-0:

Parsing Once we have broken an input file into a sequenceof tokens, the next step is to determine if thatsequence of tokens forms a syntactically correctprogram – parsing Parsing

a sequence of tokens == determining if the string of tokens could be generated by aContext-Free Grammar.

03-2:

Recursive Descent Parser Write Java code that repeatedly callsgetNextToken(), and determines if the stream ofreturned tokens can be generated by the CFG^ If so, end normally^ If not, call an error function

03-3:

Recursive Descent Parser

A Recursive Descent Parser is implemented as a suiteof recursive functions, one for each non-terminal in thegrammar:

ParseS

will terminate normally if the next tokens in the input stream can be derived from thenon-terminal

S

ParseL

will terminate normally if the next tokens in the input stream can be derived from thenon-terminal

L

ParseE

will terminate normally if the next tokens in the input stream can be derived from thenon-terminal

E

03-5:

LL(1) Parsers

These recursive descent parsers are also known asLL(1) parsers, for Left-to-right, Leftmost derivation, with1 symbol lookahead

The input file is read from left to right (starting withthe first symbol in the input stream, andproceeding to the last symbol). The parser ensures that a string can be derived bythe grammar by building a leftmost derivation. Which rule to apply at each step is decided uponafter looking at just 1 symbol.

03-6:

Building LL(1) Parsers

′^ S→

S$

S^ →

AB

S^ →

Ch A^ →

ef A^ →

ǫ B^ →

hg C^ →

DD

C^ →

fi D^ →

g ParseS

use rule

S^

→^

AB

on e, h

use the rule

S^

→^

Ch on f, g

03-8:

First sets

First(

S) is the set of all terminals that can start strings derived from

S^

(plus

ǫ, if

S^

can produce

ǫ)

′^ S→

S$

First(

′S) = {e, f, g, h}

S^ →

AB

First(

S) = {e, f, g, h}

S^ →

Ch

First(

A) = {e,

ǫ}

A^ →

ef^

First(

B) = {h}

A^ →

ǫ^

First(

C) = {f, g}

B^ →

hg^

First(

D) = {g}

C^ →

DD

C^ →

fi D^ →

g

03-9:

First sets

We can expand the definition of First sets to includestrings of terminals and non-terminals′^ S

S$

First(a

B) =

S^ →

AB

First(

BC

) =

S^ →

Ch

First(

AbC

) =

A^ →

ef^

First(

AC

) =

A^ →

ǫ^

First(ab

S) =

B^ →

hg^

First(

DDA

) =

C^ →

DD

C^ →

fi D^ →

g

03-11:

Calculating First sets For each non-terminal

S, set First(

S) = {}

For each rule of the form

S^

→^

γ, add First(

γ) to

First(

S)

Repeat until no changes are made

03-12:

Calculating First sets

Example:′^ S

S$

S^ →

AB

S^ →

Ch A^ →

ef A^ →

ǫ B^ →

hg C^ →

DD

C^ →

fi D^ →

g

03-14:

Follow Sets

Follow(

S) is the set of all terminals that can follow

S^

in

a (partial) derivation.′^ S

S$

Follow(

′ S) = { }

S^ →

AB

Follow(

S) = {$}

S^ →

Ch

Follow(

A) = {h}

A^ →

ef^

Follow(

B) = {$}

A^ →

ǫ^

Follow(

C) = {h}

B^ →

hg^

Follow(

D) = {h, g}

C^ →

DD

C^ →

fi D^ →

g

03-15:

Calculating Follow sets For each non-terminal

S, set Follow(

S) = {}

For each rule of the form

S^

→^

γ

For each non-terminal

S^1

in^ γ, where

γ^ is of the

form

αS

β 1 If First(

β) does not contain

ǫ, add all

elements of First(

β) to Follow(

S).^1

If First(

β) does contain

ǫ, add all elements of

First(

β)^

except

ǫ^ to Follow(

S), and add all^1

elements of Follow(

S) to Follow(

S).^1

If any changes were made, repeat.

03-17:

Parse Tables Each row in a parse table is labeled with anon-terminal Each column in a parse table is labeled with aterminal Each element in a parse table is either empty, orcontains a grammar rule^ Rule

S^

→^

γ^ goes in row

S, in all columns of

First(

γ). If First(

γ) contains

ǫ, then the rule

S^

→^

γ^ goes

in row

S, in all columns of Follow(

S).

03-18:

Parse Table Example

′^ S→

S$

First(

′ S) = {e, f, g, h}

Follow(

′ S) = { }

S^ →

AB

First(

S) = {e, f, g, h}

Follow(

S) = {$}

S^ →

Ch

First(

A) = {e,

ǫ}^

Follow(

A) = {h}

A^ →

ef^

First(

B) = {h}

Follow(

B) = {$}

A^ →

ǫ^

First(

C) = {f, g}

Follow(

C) = {h}

B^ →

hg^

First(

D) = {g}

Follow(

D) = {h, g}

C^ →

DD

C^ →

fi D^ →

g