LR(k)

LR(k)

Given a word

u

and a

context-free grammar

G

, how do we determine if

u

∈

L

⁢

(

G

)

?

One way is to look for any

subword

v

of

u

such that there is a

production

A

→

v

. If this is successful, we may replace

v

with

A

in

u

to obtain a word

w

, so that

w

⇒

u

. We may then repeat the process on

w

to obtain another word

x

such that

x

⇒

w

(if successful). In the end, if everything works successfully, we arrive at the starting non-terminal symbol

σ

, and get a

derivation

σ

⇒

*

u

as a result, so that

u

∈

L

⁢

(

G

)

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

u

.

In general, unless one is very lucky, successfully finding a derivation

σ

⇒

*

u

requires many trials and errors, since at each stage, for a given word

u

, there may be several words

w

such that

w

⇒

u

.

Nevertheless, there is a particular family of context-free grammars, called the

L

⁢

R

⁢

(

k

)

grammars

, which make the bottom-up parsing described above straightforward in the sense that, given a word

u

, once a word

w

is found such that

w

⇒

R

u

, any other word

w

′

such that

w

′

⇒

R

u

forces

w

′

=

w

. Here,

⇒

R

is known as the

rightmost derivation

(meaning that

u

is obtained from

w

by replacing the rightmost non-terminal in

w

). The

L

in

L

⁢

R

⁢

(

k

)

means scanning the symbols of

u

from left to right,

R

stands for finding a rightmost derivation for

u

, and

k

means having the allowance to look at up to

k

symbols ahead while scanning.

The details are as follows:

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

is any sentential form over

Σ

with the following setup:

U

=

U

1

⁢

U

2

⁢

U

3

where

•

U

3

is a terminal word,

•

X

→

U

2

a production, and

•

σ

⇒

R

*

U

1

⁢

X

⁢

U

3

⇒

R

U

.

Let

n

=

|

U

1

⁢

U

2

|

+

k

, and

Z

the prefix of

U

of length

n

(if

|

U

|

<

n

, then set

Z

=

U

).

Then

G

is said to be

L

⁢

R

⁢

(

k

)

if

W

is another sentential form having

Z

as a prefix, with the following setup:

W

=

W

1

⁢

W

2

⁢

W

3

, where

•

W

3

is a terminal,

•

Y

→

W

2

is a production, and

•

σ

⇒

R

*

W

1

⁢

Y

⁢

W

3

⇒

R

W

,

implies that

W

1

=

U

1

,

Y

=

X

,

and

W

2

=

U

2

.

Simply put, if

D

U

and

D

W

are the rightmost derivations of

U

and

W

respectively, and if the prefix of

U

obtained by including

k

symbols beyond the last replacement in

D

U

is also a prefix of

W

, then the prefix of

U

′

obtained by including

k

symbols beyond the last replacement in

D

U

is also a prefix of

W

′

, where

U

′

and

W

′

are words at the next to the last step in

D

U

and

D

W

respectively. In particular, if

U

=

W

, then

U

′

=

W

′

. This implies that any

derivable

in an

L

⁢

R

⁢

(

k

)

grammar has a unique rightmost derivation, hence

Proposition 1

.

Any

L

⁢

R

⁢

(

k

)

grammar is

unambiguous

.

Examples

.

•

Let

G

be the grammar consisting of one non-terminal symbol

σ

(which is also the final non-terminal symbol), two terminal symbols

a

,

b

, with productions

σ

→

a

⁢

σ

⁢

b

,

σ

→

σ

⁢

b

and

σ

→

b

.

Then

G

is not

L

⁢

R

⁢

(

k

)

for any

k

≥

0

.
For

instance

, look at the following two derivations of

U

=

a

2

⁢

σ

⁢

b

3

:

σ

⇒

*

a

⁢

σ

⁢

b

2

⇒

a

2

⁢

σ

⁢

b

3

and

σ

⇒

*

a

2

⁢

σ

⁢

b

2

⇒

a

2

⁢

σ

⁢

b

3

Here,

U

1

=

a

,

U

2

=

σ

⁢

b

. Let

k

=

1

. Then the criteria in the definition are satisfied. Yet,

W

1

=

a

2

≠

U

1

. Therefore,

G

is not

L

⁢

R

⁢

(

1

)

.

•

Note that the grammar

G

above generates the

language

L

=

{

a

m

⁢

b

n

⁢

∣

n

>

⁢

m

}

, which can also be generated by the grammar with three non-terminal symbols

σ

,

X

,

Y

, with

σ

the final non-terminal symbol, where the productions are given by

σ

→

X

⁢

Y

,

X

→

a

⁢

X

⁢

b

,

X

→

λ

,

Y

→

Y

⁢

b

,

and

Y

→

b

.

However, this grammar is

L

⁢

R

⁢

(

1

)

.

Determining whether a context-free grammar is

L

⁢

R

⁢

(

k

)

is a non-trivial problem. Nevertheless, an

algorithm

exists for determining, given a context-free grammar

G

and a non-negative integer

k

, whether

G

is

L

⁢

R

⁢

(

k

)

. On the other hand, without specifying

k

in advance, no algorithms exist that determine if

G

is

L

⁢

R

⁢

(

k

)

for

some

k

.

Definition

. A language is said to be

L

⁢

R

⁢

(

k

)

if it can be generated by an

L

⁢

R

⁢

(

k

)

grammar.

Theorem 1

.

Every

L

⁢

R

⁢

(

k

)

language is

deterministic context-free

. Every deterministic context-free language is

L

⁢

R

⁢

(

1

)

.

Hence, deterministic context-free languages are the same as

L

⁢

R

⁢

(

1

)

languages.

References

1

A. Salomaa,

Formal Languages

, Academic Press, New York (1973).

2

J.E. Hopcroft, J.D. Ullman,

Formal Languages and Their

Relation

to

Automata

, Addison-Wesley, (1969).

Title

LR(k)

Canonical name

LRk

Date of creation

2013-03-22 19:00:31

Last modified on

2013-03-22 19:00:31

Owner

CWoo (3771)

Last modified by

CWoo (3771)

Numerical id

11

Author

CWoo (3771)

Entry type

Definition

Classification

msc 03D10

Classification

msc 68Q42

Classification

msc 68Q05

Synonym

L

⁢

R

⁢

(

k

)

Related topic

LLk

Generated on Thu Feb 8 20:00:56 2018 by

LaTeXML