Docsity

Predictive Parsing - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Predictive Parsing - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 14

2

The LL(1) Property

If A → α and A → β both

appear in the grammar, we

would like

FIRST( α) ∩ FIRST( β) = ∅

4

The LL(1) Property

FIRST( α) ∩ FIRST( β) = ∅

allows the parser to make a

correct choice with a

lookahead of exactly one

symbol!

5

What about ε-productions?

⇒They complicate the

definition of LL(1)

7

If A → α and A → β and

ε ∈ FIRST( α) , then we need

to ensure that FIRST( β) is

disjoint from FOLLOW( α),

too

8

FOLLOW( α)

is the set of all words in the grammar that can legally appear after an α.

10

FIRST and FOLLOW

X

FIRST FOLLOW

11

Define FIRST +( α) as

FIRST( α) ∪ FOLLOW( α), if

ε ∈ FIRST( α)

FIRST( α), otherwise

13

Given a grammar that has

the is LL(1) property

  • we can write a simple routine to recognize each lhs
  • code is simple and fast

14

Given a grammar that has

the is LL(1) property

  • we can write a simple routine to recognize each lhs
  • code is simple and fast

16

Consider

A → β 1 | β 2 | β 3 ,

which satisfies the LL(1)

property

FIRST+( α)∩FIRST +( β) = ∅

17

/* find an A */

if(token ∈ FIRST( β 1 )) find a β 1 and return true

else if(token ∈ FIRST( β 2 )) find a β 2 and return true

if(token ∈ FIRST( β 3 )) find a β 3 and return true

else error and return false

19

/* find an A */

if(token ∈ FIRST( β 1 )) find a β 1 and return true

else if(token ∈ FIRST( β 2 )) find a β 2 and return true

if(token ∈ FIRST( β 3 )) find a β 3 and return true

else error and return false

20

/* find an A */

if(token ∈ FIRST( β 1 )) find a β 1 and return true

else if(token ∈ FIRST( β 2 )) find a β 2 and return true

if(token ∈ FIRST( β 3 )) find a β 3 and return true

else error and return false