LL(k)

LL(k)

Given a word

u

and a

context-free grammar

G

, how do we determine if

u

∈

L

⁢

(

G

)

?

There are in general two ways to proceed. Start from

u

, and proceed backward to find

v

such that

v

⇒

u

. Keep going until a

derivation

σ

⇒

*

u

is found. This procedure is known as the bottom-up parsing of

u

. The other method the top-down approach: begin with the

starting symbol

σ

, and work its way down to

u

, so

σ

⇒

*

u

.

As with the bottom-up approach, finding a derivation of

u

from the top-down may be time consuming, if one is lucky enough to find a derivation at all.

There is a class of

grammars

, known as the

L

⁢

L

⁢

(

k

)

grammars, which make the top-down parsing of a word natural and direct. The first

L

in

L

⁢

L

⁢

(

k

)

means scanning the symbols of

u

from left to right, the second

L

stands for finding a

leftmost derivation

(

⇒

L

) for

u

, and

k

means having the allowance to look at up to

k

symbols ahead while scanning.

Definition

. Let

G

=

(

Σ

,

N

,

P

,

σ

)

be a context-free grammar such that

σ

→

σ

is not a production of

G

, and

k

≥

0

an integer. Suppose

u

∈

L

⁢

(

G

)

with a

X

→

U

1

a production in a leftmost derivation of

u

:

σ

⇒

L

*

U

⁢

X

⁢

U

2

⇒

L

U

⁢

U

1

⁢

U

2

⇒

L

*

u

.

Let

n

=

|

U

|

+

k

and

v

be the prefix of

u

of length

n

(if

|

u

|

<

n

, then set

v

=

u

).

Then

G

is said to be

L

⁢

L

⁢

(

k

)

if for any

w

∈

L

⁢

(

G

)

, with

v

as a prefix, such that there is a production

X

→

W

1

in a leftmost derivation of

w

:

σ

⇒

L

*

U

⁢

X

⁢

W

2

⇒

L

U

⁢

W

1

⁢

W

2

⇒

L

*

w

,

implies that

W

1

=

U

1

.

In a leftmost derivation

D

u

of a word

u

, call a prefix

v

of

u

is a

leftmost

descendant

of a production

P

→

U

if

σ

⇒

*

v

⁢

P

⁢

U

′

⇒

v

⁢

U

⁢

U

′

⇒

*

u

is

D

u

. Then the definition above can be restated in words as follows:

Given a leftmost derivation

D

u

of a word

u

, a production used in

D

u

is uniquely determined up to

k

symbols beyond the prefix of

u

which is a leftmost descendant of the production. In other words, if

D

u

and

D

w

are leftmost derivations of

u

and

w

which agree on

k

symbols beyond the common prefix

v

, where

v

is both a leftmost descendant of

X

→

U

used in

D

u

, and a leftmost descendant of

X

→

W

used in

D

w

, then

X

→

U

and

X

→

W

are the same production, i.e.

U

=

W

.

Every

L

⁢

L

⁢

(

k

)

is

unambiguous

. Furthermore, every

L

⁢

L

⁢

(

k

)

grammar is

L

⁢

R

⁢

(

k

)

(

http://planetmath.org/LRk

).

Given a context-free grammar

G

and

k

≥

0

, there is an

algorithm

deciding whether

G

is

L

⁢

L

⁢

(

k

)

.

Examples

•

The grammar

G

over

Σ

=

{

a

,

b

}

, with productions

σ

→

a

2

⁢

σ

⁢

b

2

,

σ

→

a

and

σ

→

λ

is

L

⁢

L

⁢

(

2

)

but not

L

⁢

L

⁢

(

1

)

. It is not hard to see that

L

⁢

(

G

)

is the set

{

a

m

⁢

b

n

∣

n

⁢

is even, and

⁢

n

≤

m

≤

n

+

1

}

. On the other hand, the grammar

G

′

over

Σ

, with productions

σ

→

a

⁢

X

,

σ

→

λ

,

X

→

a

⁢

Y

⁢

b

,

X

→

λ

,

Y

→

a

⁢

X

⁢

b

,

Y

→

b

also generates

L

⁢

(

G

)

, but is

L

⁢

L

⁢

(

1

)

instead.

•

The grammar

G

over

{

a

,

b

,

c

}

, with productions

σ

→

X

,

σ

→

Y

,

X

→

a

⁢

X

⁢

b

,

X

→

a

⁢

b

,

Y

→

a

⁢

Y

⁢

c

,

Y

→

a

⁢

c

is not

L

⁢

L

⁢

(

k

)

for any

k

≥

0

.

Definition

A

language

is said to be

L

⁢

L

⁢

(

k

)

if it is generated by an

L

⁢

L

⁢

(

k

)

grammar. The family of

L

⁢

L

⁢

(

k

)

languages is denoted by

ℒ

⁢

ℒ

⁢

(

k

)

.

It is easy to see that an

L

⁢

L

⁢

(

0

)

contains no more than one word. Furthermore, it can be shown that

ℒ

⁢

ℒ

⁢

(

0

)

⊂

ℒ

⁢

ℒ

⁢

(

1

)

⊂

⋯

⊂

ℒ

⁢

ℒ

⁢

(

k

)

⊂

⋯

,

and the

inclusion

is strict. If

ℒ

⁢

ℒ

⁢

(

k

)

′

denotes the family of

λ

-free

L

⁢

L

⁢

(

k

)

languages, then

ℒ

⁢

ℒ

⁢

(

0

)

′

=

ℒ

⁢

ℒ

⁢

(

1

)

′

=

⋯

=

ℒ

⁢

ℒ

⁢

(

k

)

′

=

⋯

.

Given two

L

⁢

L

⁢

(

k

)

grammars

G

1

and

G

2

, there is an algorithm that decides if

L

⁢

(

G

1

)

=

L

⁢

(

G

2

)

.

References

1

A. Salomaa,

Formal Languages

, Academic Press, New York (1973).

Title

LL(k)

Canonical name

LLk

Date of creation

2013-03-22 19:00:54

Last modified on

2013-03-22 19:00:54

Owner

CWoo (3771)

Last modified by

CWoo (3771)

Numerical id

8

Author

CWoo (3771)

Entry type

Definition

Classification

msc 68Q05

Classification

msc 68Q42

Classification

msc 03D10

Related topic

LRk

Generated on Sat Feb 10 12:39:57 2018 by

LaTeXML