63
Costas Busch - LSU 1 Context-Free Languages

Context free grammars

Embed Size (px)

Citation preview

Page 1: Context free grammars

Costas Busch - LSU 1

Context-Free Languages

Page 2: Context free grammars

Costas Busch - LSU 2

Regular Languages

}0:{ nba nn }{ Rww

**ba *)( ba

Context-Free Languages

Page 3: Context free grammars

Costas Busch - LSU 3

Context-Free Languages

PushdownAutomata

Context-FreeGrammars

stack

automaton

Page 4: Context free grammars

Costas Busch - LSU 4

Context-Free Grammars

Page 5: Context free grammars

Costas Busch - LSU 5

GrammarsGrammars express languages

Example: the English language grammar

verbpredicate

nounarticlephrasenoun

predicatephrasenounsentence

_

_

Page 6: Context free grammars

Costas Busch - LSU 6

sleepsverbrunsverb

dognouncatnoun

thearticleaarticle

Page 7: Context free grammars

Costas Busch - LSU 7

Derivation of string “the dog sleeps”:

sleepsdogtheverbdogtheverbnounthe

verbnounarticleverbphrasenounpredicatephrasenounsentence

__

Page 8: Context free grammars

Costas Busch - LSU 8

Derivation of string “a cat runs”:

runscataverbcataverbnouna

verbnounarticleverbphrasenounpredicatephrasenounsentence

__

Page 9: Context free grammars

Costas Busch - LSU 9

Language of the grammar:L = { “a cat runs”, “a cat sleeps”, “the cat runs”, “the cat sleeps”, “a dog runs”, “a dog sleeps”, “the dog runs”, “the dog sleeps” }

Page 10: Context free grammars

Costas Busch - LSU 10

catnoun

Variables

Sequence of Terminals (symbols)

Productions

predicatephrasenounsentence _

Sequence of Variables

Page 11: Context free grammars

Costas Busch - LSU 11

Another Example

SaSbSGrammar:

Variable

Sequence of terminals and variables

The right side may be

Page 12: Context free grammars

Costas Busch - LSU 12

Grammar:

Derivation of string :

SaSbS

abaSbS

ab

aSbS S

Page 13: Context free grammars

Costas Busch - LSU 13

Grammar:

Derivation of string :

aabbaaSbbaSbS

aSbS S

aabb

SaSbS

Page 14: Context free grammars

Costas Busch - LSU 14

aaabbbaaaSbbbaaSbbaSbS

aaaabbbbaaaaSbbbbaaaSbbbaaSbbaSbS

Other derivations:

SaSbSGrammar:

Page 15: Context free grammars

Costas Busch - LSU 15

SaSbS

}0:{ nbaL nn

Grammar:

Language of the grammar

Page 16: Context free grammars

Costas Busch - LSU 16

We write:

Instead of:

aaabbbS*

aaabbbaaaSbbbaaSbbaSbS

for zero or more derivation steps

A Convenient Notation

Page 17: Context free grammars

Costas Busch - LSU 17

nww*

1

nwwww 321

in zero or more derivation steps

In general we write:

If:

ww*Trivially:

Page 18: Context free grammars

Costas Busch - LSU 18

SaSbS

aaabbbS

abS

S

*

*

*

Example Grammar Possible Derivations

baaaaaSbbbbaaSbbS

Page 19: Context free grammars

Costas Busch - LSU 19

SaSbS

|aSbS

thearticleaarticle

theaarticle |

Another convenient notation:

Page 20: Context free grammars

Costas Busch - LSU 20

Formal Definitions

PSTVG ,,,

Set of variables

Set of terminal symbols

Start variable

Set of productions

Grammar:

Page 21: Context free grammars

Costas Busch - LSU 21

Context-Free Grammar:

All productions in are of the form

sA

String of variables and terminals

),,,( PSTVG

P

Variable

Page 22: Context free grammars

Costas Busch - LSU 22

|aSbS

PSTVG ,,,

}{SV },{ baT

},{ SaSbSP

variables terminals

productions

start variable

Example of Context-Free Grammar

Page 23: Context free grammars

Costas Busch - LSU 23

For a grammar with start variable G S

String of terminals or

*},:{)(*

TwwSwGL

Language of a Grammar:

Page 24: Context free grammars

Costas Busch - LSU 24

context-free grammar :

}0:{)( nbaGL nn

nnbaS

G

Example:

Since, there is derivation

for any 0n

|aSbS

Page 25: Context free grammars

Costas Busch - LSU 25

A language is context-freeif there is a context-free grammar

with

LG

)(GLL

Context-Free Language definition:

Page 26: Context free grammars

Costas Busch - LSU 26

since context-free grammar :

}0:{ nbaL nn

G

Example:

is a context-free language

generates LGL )(|aSbS

Page 27: Context free grammars

Costas Busch - LSU 27

|| bSbaSaS

abbaabSbaaSaS

Context-free grammar :G

Example derivations:

abaabaabaSabaabSbaaSaS

)(GL }*},{:{ bawwwR

Palindromes of even length

Another Example

Page 28: Context free grammars

Costas Busch - LSU 28

|| SSaSbS

ababSaSbSSSS

Context-free grammar :G

Example derivations:

abababaSbabSaSbSSSS

}prefixanyin )()( and

),()(:{

vvnvn

wnwnw

ba

ba

)(GL

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

Describes matched parentheses: )b (, a

Another Example

Page 29: Context free grammars

Costas Busch - LSU 29

Derivation Orderand

Derivation Trees

Page 30: Context free grammars

Costas Busch - LSU 30

Derivation Order Consider the following example grammar

with 5 productions:

ABS .1

A

aaAA.3.2

BBbB

.5

.4

Page 31: Context free grammars

Costas Busch - LSU 31

aabaaBbaaBaaABABS54321

Leftmost derivation order of string aab :

At each step, we substitute the leftmost variable

ABS .1

A

aaAA.3.2

BBbB

.5

.4

Derivation of aab

Page 32: Context free grammars

Costas Busch - LSU 32

aabaaAbAbABbABS32541

Rightmost derivation order of string aab:

At each step, we substitute the rightmost variable

ABS .1

A

aaAA.3.2

BBbB

.5

.4

Derivation of aab

Page 33: Context free grammars

Costas Busch - LSU 33

aabaaAbAbABbABS32541

Rightmost derivation of :aab

aabaaBbaaBaaABABS54321

Leftmost derivation of :aab

ABS .1

A

aaAA.3.2

BBbB

.5

.4

Page 34: Context free grammars

Costas Busch - LSU 34

Derivation TreesConsider the same example grammar:

aabaaBbaaABbaaABABS

And a derivation of :aab

ABS |aaAA |BbB

Page 35: Context free grammars

Costas Busch - LSU 35

ABS S

BA

ABS |aaAA |BbB

yield AB

Page 36: Context free grammars

Costas Busch - LSU 36

aaABABS

a a A

S

BA

ABS |aaAA |BbB

yield aaAB

Page 37: Context free grammars

Costas Busch - LSU 37

aaABbaaABABS S

BA

a a A B b

ABS |aaAA |BbB

yield aaABb

Page 38: Context free grammars

Costas Busch - LSU 38

aaBbaaABbaaABABS S

BA

a a A B b

ABS |aaAA |BbB

yieldaaBbBbaa

Page 39: Context free grammars

Costas Busch - LSU 39

aabaaBbaaABbaaABABS

yieldaabbaa

S

BA

a a A B b

Derivation Tree

ABS |aaAA |BbB

(parse tree)

Page 40: Context free grammars

Costas Busch - LSU 40

aabaaBbaaBaaABABS

aabaaAbAbABbABS

TRY IT OUT!

Sometimes, derivation order doesn’t matterLeftmost derivation:

Rightmost derivation:

Page 41: Context free grammars

Costas Busch - LSU 41

aabaaBbaaBaaABABS

aabaaAbAbABbABS S

BA

a a A B b

Give same derivation tree

Sometimes, derivation order doesn’t matterLeftmost derivation:

Rightmost derivation:

Page 42: Context free grammars

Costas Busch - LSU 42

Ambiguity

Page 43: Context free grammars

Costas Busch - LSU 43

Grammar for mathematical expressions

))(()( aaaaaaa

Example strings:

Denotes any number

aEEEEEE |)(||

Page 44: Context free grammars

Costas Busch - LSU 44

A leftmost derivationfor aaa

aaaEaaEEaEaEEE

*

aEEEEEE |)(||

PARSE TREE ???

Page 45: Context free grammars

Costas Busch - LSU 45

A leftmost derivationfor aaa

E

EE

EE

a

a a

aaaEaaEEaEaEEE

*

aEEEEEE |)(||

Page 46: Context free grammars

Costas Busch - LSU 46

aaaEaaEEaEEEEEE

Another leftmost derivationfor

aEEEEEE |)(||

aaa

PARSE TREE ???

Page 47: Context free grammars

Costas Busch - LSU 47

E

EE

a a

EE a

aaaEaaEEaEEEEEE

Another leftmost derivationfor

aEEEEEE |)(||

aaa

Page 48: Context free grammars

Costas Busch - LSU 48

aaa E

EE

a a

EE a

E

EE

EE

a

a a

Two derivation treesfor

aEEEEEE |)(||

Page 49: Context free grammars

Costas Busch - LSU 49

E

EE

EE

E

EE

EE

2

2 2 2 2

2

222 aaa

take 2a

Page 50: Context free grammars

Costas Busch - LSU 50

E

EE

EE

E

EE

EE

6222

2

2 2 2 2

2

8222

4

2 2

2

6

2 2

24

8

Good Tree Bad Tree

Compute expression result using the tree

Page 51: Context free grammars

Costas Busch - LSU 51

Two different derivation treesmay cause problems in applications whichuse the derivation trees:

• Evaluating expressions

• In general, in compilers for programming languages

Page 52: Context free grammars

Costas Busch - LSU 52

Ambiguous Grammar:A context-free grammar is ambiguousif there is a string which has:

two different derivation trees or two leftmost derivations

G)(GLw

(Two different derivation trees give two different leftmost derivations and vice-versa)

Page 53: Context free grammars

Costas Busch - LSU 53

E

EE

a a

EE a

E

EE

EE

a

a a

string aaa has two derivation trees

aEEEEEE |)(||

this grammar is ambiguous since

Example:

Page 54: Context free grammars

Costas Busch - LSU 54

string aaa has two leftmost derivations

aaaEaaEEaEEEEEE

aaaEaaEEaEaEEE

*

aEEEEEE |)(||

this grammar is ambiguous also because

Page 55: Context free grammars

Costas Busch - LSU 55

IF_STMT if EXPR then STMT| if EXPR then STMT else STMT

Another ambiguous grammar:

Variables Terminals

Very common piece of grammar in programming languages

Page 56: Context free grammars

Costas Busch - LSU 56

If expr1 then if expr2 then stmt1 else stmt2

TWOParse Trees

or Derivation Trees

???

Page 57: Context free grammars

Costas Busch - LSU 57

If expr1 then if expr2 then stmt1 else stmt2IF_STMT

expr1 then

elseif expr2 then

STMT

stmt1

if

IF_STMT

expr1 then else

if expr2 then

STMT stmt2if

stmt1

stmt2

Two derivation trees

Page 58: Context free grammars

Costas Busch - LSU 58

In general, ambiguity is bad and we want to remove it

Sometimes it is possible to find a non-ambiguous grammar for a language

But, in general ιt is difficult to achieve this

Page 59: Context free grammars

Costas Busch - LSU 59

aEEE

EEEEEE

)(aEFFFTTTTEE

|)(||

Ambiguous Grammar Non-Ambiguous

Grammar

Equivalent

generates the same language

A successful example:

Page 60: Context free grammars

Costas Busch - LSU 60

aaaFaaFFaFTaTaTFTTTEE

E

E T

T F

F

a

T

F

a

a

aEFFFTTTTEE

|)(||

Unique derivation treefor aaa

Page 61: Context free grammars

Costas Busch - LSU 61

}{}{ mmnmnn cbacbaL

0, mn

An un-successful example:

every grammar that generates this language is ambiguous

L is inherently ambiguous:

Page 62: Context free grammars

Costas Busch - LSU 62

}{}{ mmnmnn cbacbaL

||11

aAbAAcSS

||22

bBcBBaSS

21 | SSS

Example (ambiguous) grammar for :L

Page 63: Context free grammars

Costas Busch - LSU 63

The string Lcba nnn has always two different derivation trees(for any grammar)

S

1S

S

2S

1S c 2Sa

For example