Top Banner
Context-Free Languages
82
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Context Free Lang

Context-Free Languages

Page 2: Context Free Lang

Regular Languages

}0:{ nba nn }{ Rww

**ba *)( ba

Page 3: Context Free Lang

Regular Languages

}{ nnba }{ Rww

Context-Free Languages

Page 4: Context Free Lang

Context-Free Languages

Pushdown

Automata

Context-Free

Grammars

stack

automaton

Page 5: Context Free Lang

Grammars

Grammars express languages

Example: the English language

verbpredicate

nounarticlephrasenoun

predicatephrasenounsentence

_

_

Page 6: Context Free Lang

walksverb

runsverb

dognoun

catnoun

thearticle

aarticle

Page 7: Context Free Lang

A derivation of “the dog walks”:

walksdogthe

verbdogthe

verbnounthe

verbnounarticle

verbphrasenoun

predicatephrasenounsentence

_

_

Page 8: Context Free Lang

A derivation of “a cat runs”:

runscata

verbcata

verbnouna

verbnounarticle

verbphrasenoun

predicatephrasenounsentence

_

_

Page 9: Context Free Lang

Language of the grammar:

L = { “a cat runs”,

“a cat walks”,

“the cat runs”,

“the cat walks”,

“a dog runs”,

“a dog walks”,

“the dog runs”,

“the dog walks” }

Page 10: Context Free Lang

Notation

dognoun

catnoun

Variable Terminal

Production Rules

Page 11: Context Free Lang

Another Example

Grammar:

Derivation of sentence :

S

aSbS

abaSbS

ab

aSbS S

Page 12: Context Free Lang

aabbaaSbbaSbS

aSbS S

aabb

S

aSbSGrammar:

Derivation of sentence :

Page 13: Context Free Lang

Other derivations:

aaabbbaaaSbbbaaSbbaSbS

aaaabbbbaaaaSbbbb

aaaSbbbaaSbbaSbS

Page 14: Context Free Lang

Language of the grammar

S

aSbS

}0:{ nbaL nn

Page 15: Context Free Lang

More Notation

Grammar PSTVG ,,,

:V

:T

:S

:P

Set of variables

Set of terminal symbols

Start variable

Set of Production rules

Page 16: Context Free Lang

Example

Grammar :

S

aSbSG

PSTVG ,,,

}{SV },{ baT

},{ SaSbSP

Page 17: Context Free Lang

More Notation

Sentential Form:

A sentence that contains

variables and terminals

Example:

aaabbbaaaSbbbaaSbbaSbS

Sentential Forms sentence

Page 18: Context Free Lang

We write:

Instead of:

aaabbbS*

aaabbbaaaSbbbaaSbbaSbS

Page 19: Context Free Lang

In general we write:

If:

nww*

1

nwwww 321

Page 20: Context Free Lang

By default:

ww*

Page 21: Context Free Lang

Example

S

aSbS

aaabbbS

aabbS

abS

S

*

*

*

*

Grammar Derivations

Page 22: Context Free Lang

baaaaaSbbbbaaSbb

aaSbbS

S

aSbS

Grammar

Example

Derivations

Page 23: Context Free Lang

Another Grammar Example

Grammar :

A

aAbA

AbS

Derivations:

aabbbaaAbbbaAbbAbS

abbaAbbAbS

bAbS

⇒⇒⇒⇒

⇒⇒⇒

⇒⇒

G

Page 24: Context Free Lang

More Derivations

aaaabbbbbaaaaAbbbbb

aaaAbbbbaaAbbbaAbbAbS

bbaS

bbbaaaaaabbbbS

aaaabbbbbS

nn

Page 25: Context Free Lang

Language of a Grammar

For a grammar

with start variable : G

S

}:{)( wSwGL

String of terminals

Page 26: Context Free Lang

Example

For grammar :

A

aAbA

AbS

}0:{)( nbbaGL nn

Since: bbaS nn

G

Page 27: Context Free Lang

A Convenient Notation

A

aAbA|aAbA

thearticle

aarticle

theaarticle |

Page 28: Context Free Lang

Example

A context-free grammar :

S

aSbS

aabbaaSbbaSbS

G

A derivation:

Page 29: Context Free Lang

A context-free grammar :

S

aSbS

aaabbbaaaSbbbaaSbbaSbS

G

Another derivation:

Page 30: Context Free Lang

S

aSbS

)(GL

(((( ))))

}0:{ nba nn

Describes parentheses:

Page 31: Context Free Lang

S

bSbS

aSaS

abbaabSbaaSaS

A context-free grammar : G

A derivation:

Example

Page 32: Context Free Lang

S

bSbS

aSaS

abaabaabaSabaabSbaaSaS

A context-free grammar : G

Another derivation:

Page 33: Context Free Lang

S

bSbS

aSaS

)(GL }*},{:{ bawwwR

Page 34: Context Free Lang

S

SSS

aSbS

ababSaSbSSSS

A context-free grammar : G

A derivation:

Example

Page 35: Context Free Lang

S

SSS

aSbS

abababaSbabSaSbSSSS

A context-free grammar : G

A derivation:

Page 36: Context Free Lang

S

SSS

aSbS

}prefixanyin

)()( and

),()(:{

v

vnvn

wnwnw

ba

ba

() ((( ))) (( ))

)(GL

Describes

matched

parentheses:

Page 37: Context Free Lang

Definition: Context-Free Grammars

Grammar

Productions of the form: xA

String of variables

and terminals

),,,( PSTVG

Variables Terminal

symbols

Start

variable

Variable

Page 38: Context Free Lang

*},:{)(*

TwwSwGL

),,,( PSTVG

Page 39: Context Free Lang

Definition: Context-Free Languages

A language is context-free

if and only if

there is a context-free grammar

with

L

G)(GLL

Page 40: Context Free Lang

Derivation Order

ABS .1

A

aaAA

.3

.2

B

BbB

.5

.4

aabaaBbaaBaaABABS54321

Leftmost derivation:

aabaaAbAbABbABS32541

Rightmost derivation:

Page 41: Context Free Lang

|AB

bBbA

aABS

Leftmost derivation:

abbbbabbbbB

abbBbbBabAbBabBbBaABS

Rightmost derivation:

abbbbabbBbb

abAbabBbaAaABS

Page 42: Context Free Lang

Derivation Trees

Page 43: Context Free Lang

ABS

ABS |aaAA |BbB

S

BA

Page 44: Context Free Lang

ABS |aaAA |BbB

aaABABS

a a A

S

BA

Page 45: Context Free Lang

ABS |aaAA |BbB

aaABbaaABABS

S

BA

a a A B b

Page 46: Context Free Lang

ABS |aaAA |BbB

aaBbaaABbaaABABS

S

BA

a a A B b

Page 47: Context Free Lang

ABS |aaAA |BbB

aabaaBbaaABbaaABABS

S

BA

a a A B b

Derivation Tree

Page 48: Context Free Lang

aabaaBbaaABbaaABABS

yield

aab

baa

S

BA

a a A B b

Derivation Tree

ABS |aaAA |BbB

Page 49: Context Free Lang

Ambiguity

Page 50: Context Free Lang

aEEEEEE |)(||

aaa

E

EE

EE

a

a a

aaaEaa

EEaEaEEE

*

leftmost derivation

Page 51: Context Free Lang

aEEEEEE |)(||

aaa

E

EE

a a

EE a

aaaEaa

EEaEEEEEE

leftmost derivation

Page 52: Context Free Lang

aEEEEEE |)(||

aaa

E

EE

a a

EE a

E

EE

EE

a

a a

Two derivation trees

Page 53: Context Free Lang

The grammar aEEEEEE |)(||

is ambiguous:

E

EE

a a

EE a

E

EE

EE

a

a a

string aaa has two derivation trees

Page 54: Context Free Lang

string aaa has two leftmost derivations

aaaEaa

EEaEEEEEE

aaaEaa

EEaEaEEE

*

The grammar aEEEEEE |)(||

is ambiguous:

Page 55: Context Free Lang

Definition:

A context-free grammar is ambiguous

if some string has:

two or more derivation trees

G

)(GLw

Page 56: Context Free Lang

In other words:

A context-free grammar is ambiguous

if some string has:

two or more leftmost derivations

G

)(GLw

(or rightmost)

Page 57: Context Free Lang

Why do we care about ambiguity?

E

EE

a a

EE a

E

EE

EE

a

a a

aaa

take 2a

Page 58: Context Free Lang

E

EE

EE

E

EE

EE

222

2

2 2 2 2

2

Page 59: Context Free Lang

E

EE

EE

E

EE

EE

6222

2

2 2 2 2

2

8222

4

2 2

2

6

2 2

24

8

Page 60: Context Free Lang

E

EE

EE

6222

2

2 2

4

2 2

2

6

Correct result:

Page 61: Context Free Lang

• We want to remove ambiguity

• Ambiguity is bad for programming languages

Page 62: Context Free Lang

Left Recursion & Right Recursion

It is possible for a recursive-descent parser to loop forever.

Page 63: Context Free Lang

The same effect can be achieved by rewriting the productions for A in the following manner, using a new nonterminal R:

Page 64: Context Free Lang

The left-recursion-elimination technique sketched in previous Fig. can also be applied to productions containing semantic actions.

First, the technique extends to multiple productions for A.

Page 65: Context Free Lang
Page 66: Context Free Lang
Page 67: Context Free Lang
Page 68: Context Free Lang

Position of Parser

Page 69: Context Free Lang

• There are three general types of parsers for grammars: • UNIVERSAL

• TOP-DOWN

• BOTTOM-UP

• Universal parsing methods such as • Cocke-Younger-Kasami algorithm

• Earley's algorithm can parse any grammar

• These general methods are, however, too inefficient to use in production compilers.

• The methods commonly used in compilers can be classified as being either top-down or bottom-up.

• Top-down methods build parse trees from the top (root) to the bottom (leaves), while Bottom-up methods start from the leaves and work their way up to the root.

• In either case, the input to the parser is scanned from left to right, one symbol at a time.

Page 70: Context Free Lang

• The most efficient top-down and bottom-up methods work only for subclasses of grammars,

• but several of these classes, particularly, LL and

LR grammars, are expressive enough to describe most of the syntactic constructs in modern programming languages.

• Parsers implemented by hand often use LL

grammars; • for example, the predictive-parsing approach

• Parsers for the larger class of LR grammars are usually constructed using automated tools.

Page 71: Context Free Lang
Page 72: Context Free Lang
Page 73: Context Free Lang
Page 74: Context Free Lang
Page 75: Context Free Lang

Associativity of operators Our grammar gives left associativity.

That is, if you traverse the parse tree in postorder and perform the indicated arithmetic you will evaluate the string left to right.

If you wished to generate right associativity, you would change the productions

Page 76: Context Free Lang
Page 77: Context Free Lang
Page 78: Context Free Lang
Page 79: Context Free Lang
Page 80: Context Free Lang
Page 81: Context Free Lang
Page 82: Context Free Lang