57
Context-Free Analysis Lecture Compilers SS 2009 Dr.-Ing. Ina Schaefer Software Technology Group TU Kaiserslautern Ina Schaefer Context-Free Analysis 1

Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Con

text

-Fre

eA

naly

sis

Lect

ure

Com

pile

rsS

S20

09

Dr.-

Ing.

Ina

Sch

aefe

r

Sof

twar

eTe

chno

logy

Gro

upTU

Kai

sers

laut

ern

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s1

Page 2: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Con

tent

ofLe

ctur

e

1.In

trodu

ctio

n:O

verv

iew

and

Mot

ivat

ion

2.S

ynta

x-an

dTy

peA

naly

sis

2.1

Lexi

calA

naly

sis

2.2

Con

text

-free

Syn

tax

Ana

lysi

s2.

3C

onte

xt-s

ensi

tive

Syn

tax

Ana

lysi

s3.

Tran

slat

ion

toTa

rget

Lang

uage

3.1

Tran

slat

ion

ofIm

pera

tive

Lang

uage

Con

stru

cts

3.2

Tran

slat

ion

ofO

bjec

t-Orie

nted

Lang

uage

Con

stru

cts

4.S

elec

ted

Asp

ects

ofC

ompi

lers

4.1

Inte

rmed

iate

Lang

uage

s4.

2O

ptim

izat

ion

4.3

Com

man

dS

elec

tion

4.4

Reg

iste

rAllo

catio

n4.

5C

ode

Gen

erat

ion

5.G

arba

geC

olle

ctio

n6.

XM

LP

roce

ssin

g(D

OM

,SA

X,X

SLT

)

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s2

Page 3: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Out

line

ofC

onte

xt-F

ree

Ana

lysi

s

1.S

peci

ficat

ion

ofPa

rser

s

2.Im

plem

enta

tion

ofPa

rser

sTo

p-D

own

Syn

tax

Ana

lysi

sR

ecur

sive

Des

cent

LL(k

)Par

sing

Theo

ryLL

Pars

erG

ener

atio

nB

otto

m-U

pS

ynta

xA

naly

sis

Prin

cipl

esof

LRPa

rsin

gLR

Pars

ing

Theo

ryS

LR,L

ALR

,LR

(k)P

arsi

ngLA

LR-P

arse

rGen

erat

ion

3.E

rror

Han

dlin

g

4.C

oncr

ete

and

Abs

tract

Syn

tax

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s3

Page 4: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Con

text

-Fre

eS

ynta

xA

naly

sis

Task

s•

Che

ck,i

fTok

enS

tream

(from

Sca

nner

)mat

ches

cont

ext-f

ree

synt

axof

lang

uage

s!

Err

orC

ase:

Err

orha

ndlin

g!

Cor

rect

ness

:Con

stru

ctio

nof

Syn

tax

Tree

Par

ser

Toke

n S

trea

m

Abst

ract

/ C

oncr

ete

Synta

x T

ree

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s4

Page 5: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Con

text

-Fre

eS

ynta

xA

naly

sis

(2)

Rem

arks

:•

Pars

ing

can

bein

terle

aved

with

othe

ract

ions

proc

essi

ngth

epr

ogra

m(e

.g.a

ttrib

utat

ion)

.•

Syn

tax

tree

cont

rols

impo

rtan

tpar

tsof

trans

latio

n.H

ence

,we

dist

ingu

ish

!C

oncr

ete

synt

axtre

eco

rres

pond

sto

cont

ext-f

ree

gram

mar

!A

bstra

ctsy

ntax

tree

aim

sat

furt

herp

roce

ssin

gst

eps,

com

pact

repr

esen

tatio

nof

esse

ntia

linf

orm

atio

n.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s5

Page 6: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Spe

cific

atio

nof

Pars

ers

2ge

nera

lspe

cific

atio

nte

chni

ques

•S

ynta

xD

iagr

ams

•C

onte

xt-F

ree

Gra

mm

ars

(ofte

nin

exte

nded

form

)

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s6

Page 7: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Con

text

-Fre

eG

ram

mar

s

Defi

nitio

nLe

tNan

dT

betw

oal

phab

ets,

with

N!

T="

and

!a

finite

subs

etof

N#

(N$

T)!

and

S%

N.T

hen

"=

(N,T

,!,S

)is

aco

ntex

t-fre

egr

amm

ar(C

FG)w

here

•N

isth

ese

tofn

on-te

rmin

als

•T

isth

ese

toft

erm

inal

s•

!is

the

seto

fpro

duct

ions

rule

s•

Sis

the

star

tsym

bol(

axio

m)

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s7

Page 8: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Con

text

-Fre

eG

ram

mar

s(2

)

Not

atio

ns:

•A

,B,C

,...

deno

teno

n-te

rmin

als

•a,

b,c,

...de

note

term

inal

s•

x,y,

z,..

.de

note

strin

gsof

term

inal

s,i.e

.x%

T!

•!,"

,#,$

,%,&

,'ar

est

rings

ofte

rmin

als

and

non-

term

inal

s,i.e

.!%

(N$

T)!

Pro

duct

ions

are

deno

ted

byA&

!.

The

nota

tion

A&

!|"|#|.

..is

anab

brev

iatio

nfo

rA&

!,A

&"

,A&

#,.

..

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s8

Page 9: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Der

ivat

ion

Let"

=(

N,T

,!,S

)be

aC

FG:

•$

isdi

rect

lyde

rivab

lefro

m%

in",%

prod

uces

$di

rect

ly,%'

$,i

fth

ere

exis

ts&

A'

=%

and

&!'

=$

and

A&

!%

!

•$

isde

rivab

lefro

m%

in",%'

!$

,ift

here

exis

ts%

0,..

.,%

nw

ith%

=%

0an

d$

=%

nan

dfo

rall

i%{0

,...

,n(

1}it

hold

sth

at%

i'

%i+

1.%

0,..

.,%

nis

the

deriv

atio

nof

$fro

m%

.•'

!is

the

refle

xive

,tra

nsiti

vecl

osur

eof'

.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s9

Page 10: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Der

ivat

ion

(2)

•Th

ede

rivat

ion

%0,

...,

%n

isa

left

deriv

atio

n(r

ight

deriv

atio

n),i

fin

%ith

ele

ft-m

ost(

right

-mos

t)no

n-te

rmin

alis

repl

aced

.Lef

tde

rivat

ion

step

sar

ede

note

dby

%'

lm$

.Rig

htde

rivat

ion

step

sar

ede

note

dby

%'

rm$

.

•Th

etre

e-lik

ere

pres

enta

tion

ofa

deriv

atio

nis

asy

ntax

tree.

•L(

")

={z%

T!|S'

!z}

isth

ela

ngua

gege

nera

ted

by".

•x%

L(")

isa

sent

ence

of".

•%%

(N$

T)!

with

S'

!%

isa

sent

entia

lfor

mof

".

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s10

Page 11: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Am

bigu

ityin

Gra

mm

ars

•A

sent

ence

isun

ambi

guou

sif

itha

sex

actly

one

synt

axtre

e.A

sent

ence

isam

bigu

ous

ifit

has

mor

eth

anon

esy

ntax

tree.

•Fo

reac

hsy

ntax

tree,

ther

eex

ists

exac

tlyon

left

deriv

atio

nan

dex

actly

one

right

deriv

atio

n.•

Thus

itho

lds:

Ase

nten

ceis

unam

bigu

ous

if-an

d-on

ly-if

itha

sex

actly

one

left

(rig

ht)d

eriv

atio

n.•

Agr

amm

aris

ambi

guou

sif

itco

ntai

nsan

ambi

guou

sse

nten

ce,

else

itis

unam

bigu

ous.

•Fo

rpro

gram

min

gla

ngua

ges,

unam

bigu

ous

gram

mar

sar

ees

sent

ial,

asth

ese

man

tics

and

the

trans

latio

nar

ede

fined

byth

esy

ntac

ticst

ruct

ure.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s11

Page 12: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Am

bigu

ityin

Gra

mm

ars

(2)

Exa

mpl

e1:

Gra

mm

arfo

rExp

ress

ions

"0

•S&

E•

E&

E+

E•

E&

E)

E•

E&

(E)

•E&

ID

Con

side

rthe

inpu

tstri

ng(a

v+

av))

bv+

cv+

dv

whi

chis

the

follo

win

gin

putt

oth

eco

ntex

t-fre

ean

alys

is(I

D+

ID))

ID+

ID+

ID

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s12

Page 13: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Am

bigu

ityin

Gra

mm

ars

(3)

Syn

tax

tree

for(

ID+

ID))

ID+

ID+

ID

54

© A

. P

oe

tzsch

-Heff

ter,

TU

Kais

ers

laute

rn25.04.2007

Be

isp

iele

: (

Me

hrd

eu

tigke

it)

1. B

eis

pie

l e

iner

Ausdru

cksgra

mm

atik:

!0:

S

E

, E

E

+ E

, E

E

* E

, E

(

E )

, E

ID

Betr

achte

die

Ein

gab

e: (a

v+

av)

*b

v +

cv +

dv)

Ein

ga

be z

ur

kf-

Analy

se: (

ID

+ ID

) *

ID

+ ID

+ ID

S "

"

"

"

"

E E

E

E

E

( ID

+

ID

)

*

ID

+

ID

+

ID

-S

ynta

xb

aum

ents

pri

cht nic

ht d

en ü

blic

hen

Rechenre

geln

.

-E

s g

ibt m

ehre

re S

ynta

xbäum

e g

em

äß

!0,

insbeson

dere

ist die

Gra

mm

atik m

ehrd

eutig.

•S

ynta

xtre

edo

esno

tmat

chco

nven

tiona

lrul

esof

arith

met

ic.

•Th

ere

are

seve

rals

ynta

xtre

esac

cord

ing

to"

0fo

rthi

sin

put,

henc

e"

0is

ambi

guou

s.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s13

Page 14: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Am

bigu

ityin

Gra

mm

ars

(4)

Exa

mpl

e2:

Am

bigu

ityin

if-th

en-e

lse

cons

truct

ifB1

then

ifB2

then

A:=

9el

seA:

=7

Firs

tDer

ivat

ion

55

© A

. P

oe

tzsch

-Heff

ter,

TU

Kais

ers

laute

rn25.04.2007

2.

Me

hrd

eu

tig

ke

it b

eim

if-t

he

n-e

lse

-Ko

nstr

ukt:

ifB

1th

en

if

B2

the

n

A:=

8e

lse

A

:= 7

IFT

HE

NE

LS

E

AN

W

IFT

HE

N

AN

W

AN

W

ZW

Z

W

IF I

D T

HE

N

IF I

D T

HE

N

ID E

Q C

O E

LS

E I

D E

Q C

O

ZW

Z

W

AN

W

AN

W

IFT

HE

NE

LS

E

AN

W

IFT

HE

N

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s14

Page 15: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Am

bigu

ityin

Gra

mm

ars

(5)

Sec

ond

Der

ivat

ion

55

© A

. P

oe

tzsch

-Heff

ter,

TU

Kais

ers

laute

rn25.04.2007

2. M

ehrd

eutigkeit b

eim

if-t

hen-e

lse-K

onstr

ukt:

ifB

1th

en

if B

2th

en

A:=

8e

lse A

:= 7

IFT

HE

NE

LS

E

AN

W

IFT

HE

N

AN

W

AN

W

ZW

ZW

IF ID

TH

EN

IF

ID

TH

EN

ID

EQ

CO

ELS

E ID

EQ

CO

ZW

ZW

AN

W

AN

W

IFT

HE

NE

LS

E

AN

W

IFT

HE

N

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s15

Page 16: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Am

bigu

ityin

Gra

mm

ars

(6)

Rem

arks

:•

Eac

hde

rivat

ion

corr

espo

nds

toex

actly

one

synt

axtre

e.In

reve

rse,

fore

ach

synt

axtre

e,th

ere

can

bese

vera

lder

ivat

ions

.•

Inst

ead

ofth

ete

rm"s

ynta

xtre

e"of

ten

also

the

term

s"s

truct

ure

tree"

or"d

eriv

atio

ntre

e"ar

eus

ed.

•Fo

reac

hla

ngua

ge,t

here

can

bese

vera

lgen

erat

ing

gram

mar

s,i.e

.th

em

appi

ngL:

Gra

mm

ar&

Lang

uage

isin

gene

raln

otin

ject

ive.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s16

Page 17: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Am

bigu

ityas

Gra

mm

arP

rope

rty

Am

bigu

ityis

agr

amm

arpr

oper

ty.T

hegr

amm

arfo

rexp

ress

ions

"0

isan

exam

ple

ofan

ambi

guou

sgr

amm

ar.

"0: •

S&

E•

E&

E+

E•

E&

E)

E•

E&

(E)

•E&

ID

57

© A

. P

oe

tzsch

-Heff

ter,

TU

Kais

ers

laute

rn25.04.2007

Be

isp

iel: (

Me

hrd

eu

tigke

it a

ls G

ram

ma

tike

ig.)

Die

ob

ige

Au

sd

rucksg

ram

ma

tik

!0:

S

E

, E

E

+ E

| E

* E

| E

(

E )

| E

ID

iste

in B

eis

pie

l fü

r e

ine

me

hrd

eu

tig

e G

ram

ma

tik:

S E

E

E

E

E

ID

+

ID

*ID

E

E

E

E

E S

Me

hrd

eu

tig

ke

it ist

zu

ch

st

ein

ma

l e

ine

Gra

mm

atik-

eig

en

sch

aft

.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s17

Page 18: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Am

bigu

ityas

Gra

mm

arP

rope

rty

(2)

But

ther

eex

ists

anun

ambi

guou

sgr

amm

arfo

rthe

sam

ela

ngua

ge:

"1: •

S&

E•

E&

T+

E|T

•T&

F)

T|F

•F&

(E)|ID

58

© A

. P

oe

tzsch

-Heff

ter,

TU

Kais

ers

laute

rn25.04.2007

Aber

es g

ibt

ein

ee

indeutig

e G

ram

matik f

ür

die

Spra

che:

!1:

S

E,

E

T +

E | T

, T

F

* T

| F

, F

(

E )

| I

D

S

E

E

E

E

F

F

F

F

T

T

T

T

( ID

+ ID

)

*

ID

+

IDFT

Le

se

n S

ie z

u A

bsch

nitt 2

.2.1

:

Wilh

elm

, M

aure

r:

•aus K

ap. 8, S

ynta

ktische A

naly

se, d

ie S

. 2

71 -

283

App

el:

•aus

Cha

p. 3, S

. 40 -

47

(Es g

ibt

ab

er

au

ch

ko

nte

xtf

reie

Sp

rach

en

, d

ie n

ur

du

rch

me

hrd

eu

tig

e G

ram

ma

tiken

besch

rie

be

n w

erd

en

.)In

aS

chae

fer

Con

text

-Fre

eA

naly

sis

18

Page 19: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Spe

cific

atio

nof

Pars

ers

Lite

ratu

re

Rec

omm

ende

dR

eadi

ng:

•W

ilhel

m,M

aure

r:C

hapt

er8,

pp.2

71-2

83(S

ynta

ctic

Ana

lysi

s)•

App

el:C

hapt

er3,

pp.4

0-47

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s19

Page 20: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Impl

emen

tatio

nof

Pars

ers

Ove

rvie

w•

Top-

Dow

nPa

rsin

g!

Rec

ursi

veD

esce

nt!

LL-P

arsi

ng!

LL-P

arse

rGen

erat

ion

•B

otto

m-U

pPa

rsin

g!

LR-P

arsi

ng!

LALR

,SLR

,LR

(k)-

Pars

ing

!LA

LR-P

arse

rGen

erat

ion

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s20

Page 21: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Met

hods

forC

onte

xt-F

ree

Ana

lysi

s

•M

anua

llyde

velo

ped,

gram

mar

-spe

cific

impl

emen

tatio

n(e

rror

-pro

ne,i

nflex

ible

)•

Bac

ktra

ckin

g(s

impl

e,bu

tine

ffici

ent)

•C

ocke

-You

nger

-Kas

ami-A

lgor

ithm

(196

7):

!fo

rall

CFG

sin

Cho

msk

yN

orm

alfo

rm!

base

don

idea

ofdy

nam

icpr

ogra

mm

ing

!Ti

me

Com

plex

ityO

(n3 )

,how

ever

linea

rcom

plex

ityde

sire

d•

Top-

Dow

n-M

etho

ds:f

rom

Axi

omto

Toke

nst

ream

•B

otto

m-u

p-M

etho

ds:f

rom

Toke

nst

ream

toA

xiom

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s21

Page 22: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Exa

mpl

e:To

p-D

own

Ana

lysi

sA

ccor

ding

to"

1:R

esul

tis

ale

ftde

rivat

ion.

60

© A

. P

oe

tzsch

-Heff

ter,

TU

Kais

ers

laute

rn25.04.2007

Be

isp

iel:

(To

p-d

ow

n-A

na

lyse

)

S E

=>

T

+

E

=

>

F

*

T

+

E

=

>

(

E

)

*

T

+

E

=

>

(

T

+

E

)

*

T

+

E

=>

(

F

+

E

)

*

T

+

E

=>

(

ID

+

E

)

*

T

+

E

=>

(

ID

+

T

)

*

T

+

E

=>

(

ID

+

F

)

*

T

+

E

=>

(

ID

+

ID

)

*

T

+

E

=>

(

ID

+

ID

)

*

F

+

E

=>

(

ID

+

ID

)

*

ID

+

E

=>

(

ID

+

ID

)

*

ID

+

T

=>

(

ID

+

ID

)

*

ID

+

F

=>

(

ID

+

ID

)

*

ID

+

ID

Erg

eb

nis

de

rtd

-An

aly

se

ist

ein

e L

inksa

ble

itu

ng

.

Gem

äß

!1

:

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s22

Page 23: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Exa

mpl

e:B

otto

m-U

pA

naly

sis

Acc

ordi

ngto

"1:

Res

ulti

sa

right

deriv

atio

n.

61

© A

. P

oe

tzsch

-Heff

ter,

TU

Kais

ers

laute

rn25.04.2007

Be

isp

iel:

(Bott

om

-up

-An

aly

se

)

(

ID

+

ID

)

*

ID

+

ID

<=

(

F

+

I

D

)

*

I

D

+

I

D

<

=

(

T

+

I

D

)

*

I

D

+

I

D

<

=

(

T

+

F)

*

ID

+

ID

<=

(

T

+

T

)

*

I

D

+

I

D

<

=

(

T

+

E

)

*

I

D

+

I

D

<

=

(

E

)

*

ID

+

ID

<=

F

*

ID

+

I

D

<

=

F

*

F

+

ID

<=

F

*

T

+

ID

<=

T

+

ID

<=

T

+

F

<=

T

+

T

<=

T

+

E

<=

E

<=

S

<=

Erg

eb

nis

de

rb

u-A

na

lyse

ist

ein

e R

ech

tsa

ble

itu

ng

.

Gem

äß

!1 :

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s23

Page 24: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Con

text

-free

Ana

lysi

sw

ithlin

earc

ompl

exity

•R

estri

ctio

nson

Gra

mm

ar(n

otev

ery

CFG

has

alin

earp

arse

r)•

Use

ofpu

sh-d

own

auto

mat

aor

syst

ems

ofre

curs

ive

proc

edur

es•

Usa

geof

look

ahea

dto

rem

aini

ngin

puti

nor

dert

ose

lect

next

prod

uctio

nru

leto

beap

plie

d

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s24

Page 25: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Syn

tax

Ana

lysi

sM

etho

dsan

dPa

rser

Gen

erat

ors

•B

asic

Kno

wle

dge

ofS

ynta

xA

naly

sis

ises

sent

ialf

orus

eof

Pars

erG

ener

ator

s.•

Pars

erge

nera

tors

are

nota

lway

sap

plic

able

.•

Ofte

n,er

rorh

andl

ing

has

tobe

done

man

ually

.•

Met

hods

unde

rlyin

gpa

rser

gene

ratio

nis

ago

odex

ampl

efo

rage

neric

tech

niqu

e(a

nda

high

light

ofco

mpu

ters

cien

ce!).

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s25

Page 26: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Top-

Dow

nS

ynta

xA

naly

sis

Edu

catio

nalO

bjec

tives

•G

ener

alP

rinci

ple

ofTo

p-D

own

Syn

tax

Ana

lysi

s•

Rec

ursi

veD

esce

ntPa

rsin

g(a

tan

Exa

mpl

e)•

Exp

ress

iven

ess

ofTo

p-D

own

Pars

ing

•B

asic

Con

cept

sof

LL(k

)Par

sing

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s26

Page 27: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Rec

usiv

eD

esce

nt

Bas

icId

ea

•E

ach

non-

term

inal

Ais

asso

ciat

edw

itha

proc

edur

e.Th

ispr

oced

ure

acce

pts

apa

rtia

lsen

tenc

ede

rived

from

A.

•Th

epr

oced

ure

impl

emen

tsa

finite

auto

mat

onco

nstru

cted

from

the

prod

uctio

nsst

artin

gfro

mA

•R

ecur

sion

ofgr

amm

aris

map

ped

tom

utua

lrec

ursi

vepr

oced

ures

such

that

stac

kof

high

erpr

ogra

min

gla

ngua

ges

can

beus

edfo

rim

plem

enta

tion.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s27

Page 28: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Con

stru

ctio

nof

Rec

ursi

veD

esce

ntPa

rser

Let"

" 1be

aC

FG(li

ke"

1)w

itha

term

inal

#de

notin

gth

een

dof

the

inpu

t.

"" 1: •

S&

E#

•E&

T+

E|T

•T&

F)

T|F

•F&

(E)|ID

Con

stru

ctite

mau

tom

aton

fore

ach

non-

term

inal

A.T

heite

mau

tom

aton

desc

ribes

the

anal

ysis

ofth

epr

oduc

tions

with

left

side

A.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s28

Page 29: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Item

Auto

mat

a

S&

E#

64

© A

. P

oe

tzsch

-Heff

ter,

TU

Kais

ers

laute

rn25.04.2007

Ko

nstr

uktio

n e

ine

s P

ars

ers

mit d

er

Me

tho

de

de

s r

eku

rsiv

en

Ab

stie

gs (

exe

mp

larisch

):

Se

i !

wie

!1, aber

mit R

and

zeic

hen #

, d.h

.

S

E #

, E

T

+ E

| T

, T

F

* T

| F

, F

(

E )

| I

D

Konstr

uie

re f

ür

jedes N

ichtt

erm

inal A

den s

og

ena

nnte

n

Item-Automaten

. E

r beschre

ibt d

ie A

naly

se d

erj

en

ige

n

Pro

duktion

en, d

ere

n lin

ke S

eite A

ist:

1

[S .E

#]

[S

E.#

][S

E

#.]

[E.T

+E

]

[E.T

]

[T.F

*T]

[ T

.F

]

[F.(

E)]

[F.I

D ]

[ET

+.E

][E

T+

E.]

[ET.+

E]

[ET.]

[ T

F.]

[TF.*

T]

[TF

*.T

][T

F*T.]

[FID.]

[F(.

E)]

[F(E.)

][F

(E).

]

E#

T+

E

F*

T

( ID

E)

E&

T+

E|T

64

© A

. P

oe

tzsch

-Heff

ter,

TU

Kais

ers

laute

rn25.04.2007

Ko

nstr

uktio

n e

ine

s P

ars

ers

mit d

er

Me

tho

de

de

s r

eku

rsiv

en

Ab

stie

gs (

exe

mp

larisch

):

Se

i !

wie

!1, aber

mit R

and

zeic

hen #

, d.h

.

S

E #

, E

T

+ E

| T

, T

F

* T

| F

, F

(

E )

| I

D

Konstr

uie

re f

ür

jedes N

ichtt

erm

inal A

den s

og

ena

nnte

n

Item-Automaten

. E

r beschre

ibt d

ie A

naly

se d

erj

en

ige

n

Pro

duktion

en, d

ere

n lin

ke S

eite A

ist:

1

[S .E

#]

[S

E.#

][S

E

#.]

[E.T

+E

]

[E.T

]

[T.F

*T]

[ T

.F

]

[F.(

E)]

[F.I

D ]

[ET

+.E

][E

T+

E.]

[ET.+

E]

[ET.]

[ T

F.]

[TF.*

T]

[TF

*.T

][T

F*T.]

[FID.]

[F(.

E)]

[F(E.)

][F

(E).

]

E#

T+

E

F*

T

( ID

E)

T&

F)

T|F

64

© A

. P

oe

tzsch

-Heff

ter,

TU

Kais

ers

laute

rn25.04.2007

Ko

nstr

uktio

n e

ine

s P

ars

ers

mit d

er

Me

tho

de

de

s r

eku

rsiv

en

Ab

stie

gs (

exe

mp

larisch

):

Se

i !

wie

!1, aber

mit R

and

zeic

hen #

, d.h

.

S

E #

, E

T

+ E

| T

, T

F

* T

| F

, F

(

E )

| I

D

Konstr

uie

re f

ür

jedes N

ichtt

erm

inal A

den s

og

ena

nnte

n

Item-Automaten

. E

r beschre

ibt d

ie A

naly

se d

erj

en

ige

n

Pro

duktion

en, d

ere

n lin

ke S

eite A

ist:

1

[S .E

#]

[S

E.#

][S

E

#.]

[E.T

+E

]

[E.T

]

[T.F

*T]

[ T

.F

]

[F.(

E)]

[F.I

D ]

[ET

+.E

][E

T+

E.]

[ET.+

E]

[ET.]

[ T

F.]

[TF.*

T]

[TF

*.T

][T

F*T.]

[FID.]

[F(.

E)]

[F(E.)

][F

(E).

]

E#

T+

E

F*

T

( ID

E)

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s29

Page 30: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Item

Auto

mat

a(2

)

F&

(E)|ID

64

© A

. P

oe

tzsch

-Heff

ter,

TU

Kais

ers

laute

rn25.04.2007

Ko

nstr

uktio

n e

ine

s P

ars

ers

mit d

er

Me

tho

de

de

s r

eku

rsiv

en

Ab

stie

gs (

exe

mp

larisch

):

Se

i !

wie

!1, aber

mit R

and

zeic

hen #

, d.h

.

S

E #

, E

T

+ E

| T

, T

F

* T

| F

, F

(

E )

| I

D

Konstr

uie

re f

ür

jedes N

ichtt

erm

inal A

den s

og

ena

nnte

n

Item-Automaten

. E

r beschre

ibt d

ie A

naly

se d

erj

en

ige

n

Pro

duktion

en, d

ere

n lin

ke S

eite A

ist:

1

[S .E

#]

[S

E.#

][S

E

#.]

[E.T

+E

]

[E.T

]

[T.F

*T]

[ T

.F

]

[F.(

E)]

[F.I

D ]

[ET

+.E

][E

T+

E.]

[ET.+

E]

[ET.]

[ T

F.]

[TF.*

T]

[TF

*.T

][T

F*T.]

[FID.]

[F(.

E)]

[F(E.)

][F

(E).

]

E#

T+

E

F*

T

( ID

E)

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s30

Page 31: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Rec

ursi

veD

esce

ntPa

rsin

gP

roce

dure

s

•Ite

mAu

tom

ata

can

bem

appe

dto

recu

rsiv

epr

oced

ures

.•

The

inpu

tis

ato

ken

stre

amte

rmin

ated

by#.

•Th

eva

riabl

ecu

rrSy

mbol

cont

ains

one

toke

nlo

okah

ead,

i.e.t

hefir

stsy

mbo

loft

hest

ream

.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s31

Page 32: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Rec

ursi

veD

esce

ntPa

rsin

gP

roce

dure

s(2

)

Pro

duct

ion:

S&

E#

void

S(){

E();

if(currSymbol==

’#’){

accept();

}else

{error();

}}

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s32

Page 33: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Rec

ursi

veD

esce

ntPa

rsin

gP

roce

dure

s(3

)P

rodu

ctio

n:E&

T+

E|T

void

E(){

T(); if(currSymbol==

’+’){

readSymbol();

E();

}} P

rodu

ctio

n:T&

F)

T|F

void

T(){

F(); if(currSymbol==

’*’){

readSymbol();

T();

}}

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s33

Page 34: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Rec

ursi

veD

esce

ntPa

rsin

gP

roce

dure

s(4

)

Pro

duct

ion:

F&

(E)|ID

void

F(){

if(currSymbol==

’(’){

readSymbol();

E();

if(currSymbol==

’)’){

readSymbol();

} else

error();

} else

if(currSymbol==

ID){

readSymbol();

} else

error();

}

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s34

Page 35: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Rec

ursi

veD

esce

ntPa

rsin

gP

roce

dure

s(5

)

Rem

arks

:•

Rec

ursi

veD

esce

nt!

isre

lativ

ely

easy

toim

plem

ent

!ca

nea

sily

beus

edw

ithot

hert

asks

(see

follo

win

gex

ampl

e)!

isa

typi

cale

xam

ple

fors

ynta

x-di

rect

edm

etho

ds(s

eeal

sofo

llow

ing

exam

ple)

•E

xam

ple

uses

one

toke

nlo

okah

ead.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s35

Page 36: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Rec

ursi

veD

esce

ntan

dE

valu

atio

n

Exa

mpl

e:In

terp

rete

rfor

Exp

ress

ions

usin

gre

curs

ive

desc

ent

intenv(Ident);//

ID->

int

//localvariable

int_result

stores

intermediate

results

intS(){

intint_result

:=E();

if(currSymbol==

’#’)

{return

int_result;

}else

{error();

return

error_result;

}}

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s36

Page 37: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Rec

ursi

veD

esce

ntan

dE

valu

atio

n(2

)

intE(){

intint_result

:=T();

if(currSymbol==

’+’){

readSymbol();

return

int_result

+E();

}} intT(){

intint_result

:=F();

if(currSymbol==

’*’){

readSymbol();

return

int_result

*T();

}}

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s37

Page 38: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Rec

ursi

veD

esce

ntan

dE

valu

atio

n(3

)

intF(){

intint_result;

if(currSymbol==

’(’){

readSymbol();

int_result

:=E();

if(currSymbol==

’)’){

readSymbol();

return

int_result;

}else

{error();

return

error_result;}

} else

if(currSymbol==

ID){

readSymbol();

return

env(code(ID));

}else

{error();

return

error_result;}

}In

aS

chae

fer

Con

text

-Fre

eA

naly

sis

38

Page 39: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Rec

ursi

veD

esce

ntan

dE

valu

atio

n(4

)

•E

xten

sion

ofPa

rser

with

Act

ions

/Com

puta

tions

can

easi

lybe

impl

emen

ted,

butm

ixin

gof

conc

eptu

ally

diffe

rent

task

san

dca

uses

prog

ram

sha

rdto

mai

ntai

n.•

Forw

hich

gram

mar

sdo

esth

ere

curs

ive

desc

entt

echn

ique

wor

k?&

LL(k

)Par

sing

Theo

ry

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s39

Page 40: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

LLPa

rsin

g

•B

asis

forT

own-

Dow

nS

ynta

xA

naly

sis

•Fi

rstL

:Rea

dIn

putf

rom

Left

toR

ight

•S

econ

dL:

Sea

rch

forL

eftD

eriv

atio

ns

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s40

Page 41: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

LL(k

)Gra

mm

ars

Defi

nitio

n(L

L(k)

Gra

mm

ar)

Let"

=(

N,T

,!,S

)be

aC

FGan

dk%

N.

"is

anLL

(k)g

ram

mar

,iff

oran

ytw

ole

ftde

rivat

ions

S'

! lmuA

!'

lmu"

!'

! lmux

and

S'

! lmuA

!'

lmu#

!'

! lmuy

itho

lds

that

ifpr

efix(

k,x)

=pr

efix(

k,y)

then

"=

#.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s41

Page 42: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

LL(k

)Gra

mm

ars

(2)

Rem

arks

:•

Agr

amm

aris

anLL

(k)g

ram

mar

iffo

rale

ftde

rivat

ion

with

kto

ken

look

ahea

dth

eco

rrec

tpro

duct

ion

fort

hene

xtde

rivat

ion

step

can

befo

und.

•A

Lang

uage

L*

#!

isLL

(k)i

fthe

reex

ists

LL(k

)gra

mm

ar"

with

L(")

=L.

•Th

ede

finiti

onof

LL(k

)gra

mm

ars

prov

ides

nom

etho

dto

test

ifa

gram

mar

has

the

LL(k

)pro

pert

y.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s42

Page 43: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Non

LL(k

)Gra

mm

ars

Exa

mpl

e1:

Gra

mm

arw

ithLe

ftR

ecur

sion

"2:

•S&

E#

•E&

E+

T|T

•T&

T)

F|F

•F&

(E)|ID

Elim

inat

ion

ofLe

ftR

ecur

sion

:R

epla

cepr

oduc

tions

offo

rmA&

A!|"

whe

re"

does

nots

tart

with

Aby

A&

"A"an

dA"&

!A" |(

.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s43

Page 44: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Non

LL(k

)Gra

mm

ars

(2)

Elim

inat

ion

ofLe

ftR

ecur

sion

:Fr

om"

2w

eob

tain

"3.

"2: •

S&

E#

•E&

E+

T|T

•T&

T)

F|F

•F&

(E)|ID

"3

•S&

E#

•E&

TE"

•E"&

+TE

" |(•

T&

FT"

•T"&)F

T|(

•F&

(E)|ID

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s44

Page 45: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Non

LL(k

)Gra

mm

ars

(3)

Exa

mpl

e2:

Gra

mm

ar"

4w

ithun

limite

dlo

okah

ead

•S

TM&

VAR

:=VA

R|ID

(ID

LIS

T)

•VA

R&

ID|ID

(ID

LIS

T)

•ID

LIS

T&

ID|ID

,ID

LIS

T

"4

isno

tan

LL(k

)gra

mm

arfo

rany

k.(P

roof

:cf.

Wilh

elm

,Mau

rer,

Exa

mpl

e8.

3.4,

p.31

9)

Tran

sfor

mat

ion

toLL

(2)g

ram

mar

"" 4:

•S

TM&

AS

S_C

ALL

|ID:=

VAR

•A

SS

_CA

LL&

ID(I

DLI

ST

)AS

S_C

ALL

_RE

ST

•A

SS

_CA

LL_R

ES

T&

:=VA

R|(

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s45

Page 46: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Non

LL(k

)Gra

mm

ars

(4)

Rem

ark:

The

trans

form

edgr

amm

ars

acce

ptth

esa

me

lang

uage

,but

prov

ide

othe

rsyn

tax

trees

.

From

ath

eore

tical

poin

tofv

iew

,thi

sis

acce

ptab

le.

From

apr

ogra

mm

ing

lang

uage

impl

emen

tatio

npe

rspe

ctiv

e,th

isis

inge

nera

lnot

acce

ptab

le.

Ther

ear

ela

ngua

ges

Lfo

rwhi

chno

LL(k

)gra

mm

ar"

exis

tsth

atge

nera

tes

the

lang

uage

,i.e

.L("

)=

L.(E

xam

ple:

Gra

mm

ar"

5)

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s46

Page 47: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Non

LL(k

)Gra

mm

ars

(5)

Exa

mpl

e3:

ForL

("5)

,the

reex

ists

noLL

(k)g

ram

mar

.

•S&

A|B

•A&

aAb|

0•

B&

aBbb

|1

We

show

that

ther

eis

nok

such

that

"5

isan

LL(k

)gr

amm

ar.

Pro

of.

Letk

bear

bitra

rybu

tfixe

d.C

hoos

etw

ode

rivat

ions

acco

rdin

gto

the

LL(k

)defi

nitio

nan

dsh

owth

atde

sipi

teof

equa

lpre

fixes

ofle

ngth

kth

ere

suts

from

"an

d#

are

note

qual

:

S'

! lmS'

Alm'

! lmak

0bk

S'

! lmS'

lmB'

! lmak

1b2k

Then

:pre

fix(k

,ak0b

k)

=pr

efix(

k,ak

1b2k

)=

ak,b

ut"

=A+=

B=

#.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s47

Page 48: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

FIR

ST

and

FOLL

OW

Set

s

Defi

nitio

nLe

t"=

(N

,T,!

,S)

bea

CFG

,k%

N.T

#k

={u%

T!|le

ngth

(u),

k}de

note

sal

lpre

fixes

ofle

ngth

atle

astk

.

We

defin

e:•

FIR

ST k

:(N$

T)!&

P(T

#k)

with

FIR

ST k

(!)

={p

refix

(k,u

)|!'

!u}

whe

repr

efix(

n,u)

=u

fora

llu

with

leng

th(U

),

n.•

FOLL

OW

k:(N$

T)!&

P(T

#k)

with

FOLL

OW

k(!

)=

{w|S'

!"!#-

w%

FIR

ST k

(#)}

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s48

Page 49: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

FIR

ST

and

FOLL

OW

Set

sin

Pars

eTr

ees

XS

FIRSTk(X)

FOLLOWk(X)

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s49

Page 50: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Cha

ract

eriz

atio

nof

LL(1

)Gra

mm

ars

Defi

nitio

n(R

educ

edC

FG)

AC

FG"

=(N

,T,!

,S)

isre

duce

dif

each

non-

term

inal

occu

rsin

ade

rivat

ion

and

each

non-

term

inal

deriv

esat

leas

tone

wor

d.

Lem

ma

Are

duce

dC

FGis

LL(1

)if-a

nd-o

nly-

iffo

rany

two

prod

uctio

nsA&

"an

dA&

#it

hold

sth

at

FIR

ST 1

(").

1FO

LLO

W1(

A)!

FIR

ST 1

(#).

1FO

LLO

W1(

A)

="

whe

reL 1.

1L 2

={p

refix

(1,v

w)|v%

L 1,w

%L 2

}

Rem

ark:

FIR

ST

and

FOLL

OW

sets

are

com

puta

ble,

such

this

crite

rion

can

bech

ecke

dau

tom

atic

ally.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s50

Page 51: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Exa

mpl

es:F

IRS

T kan

dFO

LLO

Wk

Che

ckth

atm

odifi

edex

pres

sion

gram

mar

"3

isLL

(1).

•S&

E#

•E&

TE"

•E"&

+TE

" |(•

T&

FT"

•T"&)F

T|(

•F&

(E)|ID

Com

pute

FIR

ST 1

and

FOLL

OW

1fo

reac

hno

n-te

rmin

al.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s51

Page 52: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Exa

mpl

es:F

IRS

T kan

dFO

LLO

Wk

(2)

•F&

(E)|ID

:FI

1((E

)).

1FO

LLO

W1(

F)!

FIR

ST 1

(ID

).

FOLL

OW

1(F

)=

{(}.

1FO

LLO

W1(

F)!{I

D}.

FOLL

OW

1(F

)=

"•

E"&

+TE

" |(:

FIR

ST 1

(+TE

" ).

1FO

LLO

W1(

E" )!

FIR

ST 1

(().

FOLL

OW

1(E" )

={+

}.

1FO

LLO

W1(

E" )!{(}.

FOLL

OW

1(E" )

={+

}!{

#,)}

="

•T"&)F

T|(

:FI

RS

T 1()

FT" ).

1FO

LLO

W1(

T" )!

FIR

ST 1

(().

FOLL

OW

1(T" )

={)

}.

1FO

LLO

W1(

T" )!{(}.

FOLL

OW

1(T" )

={)

}!{+

,#,)}

="

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s52

Page 53: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Pro

ofof

LLC

hara

cter

izat

ion

Lem

ma

•Le

ft-T

o-R

ight

Dir

ectio

n:"

isLL

(1)i

mpl

ies

FIR

ST

and

FOLL

OW

char

acte

rizat

ion.

Pro

ofby

Con

trad

ictio

n:S

uppo

setw

opr

oduc

tions

A&

"an

dA&

#w

ith"+=

#an

d$

=FI

RS

T 1("

).

1FO

LLO

W1(

A)!

FIR

ST 1

(#).

1FO

LLO

W1(

A)+=".

Then

ther

eex

ists

z%

$w

ithle

ngth

(z)

=1.

As

"is

redu

ced,

ther

ear

etw

ode

rivat

ions

:

S'

!$

A!'

$"!'

!$

zxS'

!$

A!'

$#!'

!$

zy

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s53

Page 54: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Pro

ofof

LLC

hara

cter

izat

ion

Lem

ma

(2)

Thus

,we

can

cons

truct

the

follo

win

gle

ftde

rivat

ions

:

S'

! lmuA

!'

lmu"

!'

! lmuz

xS'

! lmuA

!'

lmu#

!'

! lmuz

y

with

prefi

x(1,

zx)

=z

=pr

efix(

1,zy

)w

hich

cont

radi

cts

the

LL(1

)pr

oper

tyof

".

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s54

Page 55: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Pro

ofof

LLC

hara

cter

izat

ion

Lem

ma

(3)

•R

ight

-To-

Left

Dir

ectio

n:FI

RS

Tan

dFO

LLO

Wch

arac

teriz

atio

nim

plie

s"

isLL

(1).

Pro

ofby

Con

trad

ictio

n:S

uppo

se"

isno

tLL(

1).T

hen

ther

ear

etw

odi

ffere

ntde

rivat

ions

with

leng

th(z

)=

1:

S'

!$

A!'

$"!'

!$

zxS'

!$

A!'

$#!'

!$

zy

But

z%

FIR

ST 1

(").

1FI

RS

T 1(#

)w

hich

isa

cont

radi

ctio

n.

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s55

Page 56: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Pars

erG

ener

atio

nfo

rLL(

k)La

ngua

ges

LL(k

) Par

ser

Gen

erat

or

Gra

mm

ar

Table

for

Push

-Dow

n A

uto

mat

on/

Par

ser

Pro

gram

Err

or:

G

ram

mar

is

not

LL(k

)

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s56

Page 57: Analysis 2009 er - - TU KaiserslauternS) CFG: • ψ is le from φ in Γ, φ produces ψ, φ ⇒ ψ if xists σ A τ = φ and τ = ψ and A → α ∈ Π • ψ is le from ... Analysis

Impl

emen

tatio

nof

Pars

ers

Top-

Dow

nS

ynta

xA

naly

sis

Pars

erG

ener

atio

nfo

rLL(

k)La

ngua

ges

(2)

Rem

arks

:•

Use

ofpu

sh-d

own

auto

mat

aw

ithlo

okah

ead

•S

elec

tPro

duct

ion

from

Tabl

es•

Adv

anta

ges

over

botto

m-u

pte

chni

ques

iner

rora

naly

sis

and

erro

rha

ndlin

g

Exa

mpl

eS

yste

m:

AN

TLR

(htt

p://

www.

antl

r.or

g/)

Rec

omm

ende

dR

eadi

ngfo

rTo

p-D

own

Ana

lysi

s:•

Wilh

elm

,Mau

rer:

Cha

pter

8,S

ectio

ns8.

3.1.

toS

ectio

ns8.

3.4,

pp.

312

-329

Ina

Sch

aefe

rC

onte

xt-F

ree

Ana

lysi

s57