22
Theory of Computation (With Automata Theory) * Property of STI Page 1 of 22 Context-Free Languages Context-Free Languages Context-Free Grammars Introduction Finite automatons and regular expressions are tools that are used for describing regular languages. Context-free grammars are more powerful tools because they can also describe non- regular languages. In linguistics, a grammar is a system of rules by which sentences are constructed by putting together words of the language.

MELJUN CORTES Automata Lecture Context-free Languages 2

Embed Size (px)

DESCRIPTION

MELJUN CORTES Automata Lecture Context-free Languages 2

Citation preview

Page 1: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 1 of 22

Context-Free Languages

Context-Free Languages

Context-Free Grammars

Introduction

• Finite automatons and regular

expressions are tools that are

used for describing regular

languages.

• Context-free grammars are

more powerful tools because

they can also describe non-

regular languages.

• In linguistics, a grammar is a

system of rules by which

sentences are constructed by

putting together words of the

language.

Page 2: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 2 of 22

Context-Free Languages

• Example of a simple grammar, G1,

in English:

1. <sentence> →

<noun phrase> <predicate>

2. <noun phrase> →

<article> <noun>

3. <predicate> → <verb>

4. <article> → a

5. <article> → the

6. <noun> → boy

7. <noun> → girl

8. <verb> → smiles

9. <verb> → laughs

Page 3: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 3 of 22

Context-Free Languages

To form the sentence “the girl

smiles”:

<sentence> → <noun phrase>

<predicate>

<sentence> → <article> <noun>

<predicate>

<sentence> → <article> <noun>

<verb>

<sentence> → the <noun> <verb>

<sentence> → the girl <verb>

<sentence> → the girl smiles

Page 4: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 4 of 22

Context-Free Languages

• In theory of computation, a

grammar is a system of rules

by which strings are

constructed by putting together

symbols of the language.

• Example: The grammar, G2,

for language L = {w {0,1}* w

= wR} is

1. S → ε2. S → 0

3. S → 1

4. S → 0S0

5. S → 1S1

To form the string 1010101:

S → 1S1

S → 10S01

S → 101S101

S → 1010101

Page 5: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 5 of 22

Context-Free Languages

• A context-free grammars is

simply a set of rules called

substitution rules or

productions.

• The left-hand side of each

production has a symbol,

called a variable, followed by

an arrow and a string.

• The right-hand side of each

production has a string

composed of variables and

other symbols called

terminals. Variables are

symbols that can be replaced

while terminals are symbols

that cannot be replaced.

• One variable is assigned as

the start variable. It usually

appears on the left-hand side

of the first production.

Page 6: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 6 of 22

Context-Free Languages

• By following the rules of a

grammar, each string of that

language can be generated.

The procedure is:

1. Write down the rule that

contains the start variable.

It is usually the first rule of

the grammar.

2. Find a variable on the

right-hand side of the rule

written in step 1 and find

another rule that starts with

that variable. Replace or

substitute the variable with

the right-hand side of that

rule.

3. Repeat step 2 until no

variables remain.

The sequence of substitutions

performed to obtain a string is

called a derivation.

Page 7: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 7 of 22

Context-Free Languages

• For convenience, rules with the

same left-hand side variable may

be combined into a single rule with

their right-hand side strings

separated by a .

• For grammar G1:

1. <sentence> →

<noun phrase> <predicate>

2. <noun phrase> →

<article> <noun>

3. <predicate> → <verb>

4. <article> → a the

5. <noun> → boy girl

6. <verb> → smiles laughs

• For grammar G2:

1. S → є 0 1 0S0 1S1

Page 8: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 8 of 22

Context-Free Languages

• All strings generated or derived

from a grammar G constitute the

language of that grammar and is

written as L(G).

For grammar G1:

L(G1) = {the girl smiles, the girl

laughs, the boy smiles,

the boy laughs, a girl

smiles, a girl laughs, a

boy smiles, a boy laughs}

For grammar G2:

L(G2) = {w {0,1}* w = wR}

• Any language that can be

generated by some context-free

grammar is called a context-free

language.

Page 9: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 9 of 22

Context-Free Languages

Formal Definition of Context-Free

Grammars

• A context-free grammar is a

4-tuple (V, , R, S), where

1. V is a finite set of

variables,

2. is a finite set of

terminals,

3. R is a finite set of rules,

with each rule being a

variable on the left-hand

side and a string of

variables and/or terminals

on the right-hand side, and

4. S is the start variable.

Page 10: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 10 of 22

Context-Free Languages

• Examples:

Given grammar G3 as

1. A → 0A1 ε

The formal definition of

grammar G3 is

G3 = (V, , R, S)

where

V = {A}

= {0, 1}

R = {A → 0A1 ε}

S = A

Page 11: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 11 of 22

Context-Free Languages

Sample derivations using

grammar G3:

A → 0A1

→ 00A11

→ 0011

A → 0A1

→ 00A11

→ 000A111

→ 000111

A → 0A1

→ 00A11

→ 000A111

→ 0000A1111

→ 00001111

L(G3) = {0x1x x ≥ 0}

Page 12: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 12 of 22

Context-Free Languages

Given grammar G4 as

1. A → B1

2. B → 0B1 ε

The formal definition of

grammar G4 is

G4 = (V, , R, S)

where

V = {A, B}

= {0, 1}

R = {A → B1, B → 0B1 ε)

S = A

Page 13: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 13 of 22

Context-Free Languages

Sample derivations using

grammar G4:

A → B1

→ 0B11

→ 011

A → B1

→ 0B11

→ 00B111

→ 00111

A → B1

→ 0B11

→ 00B111

→ 000B1111

→ 0001111

L(G4) = {0x1x+1 x ≥ 0}

Page 14: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 14 of 22

Context-Free Languages

Given grammar G5 as

1. A → (A) AA ε

The formal definition of

grammar G5 is

G5 = (V, , R, S)

where:

V = {A}

= {( , )}

R = {A → (A) AA ε}

S = A

Page 15: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 15 of 22

Context-Free Languages

Sample derivations using

grammar G5:

A → (A)

→ ()

A → (A)

→ (AA)

→ ((A)A)

→ ((A)(A))

→ (( )(A))

→ (( )( ))

Page 16: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 16 of 22

Context-Free Languages

A → (A)

→ (AA)

→ ((A)A)

→ ((A)(A))

→ (( )(A))

→ (( )(AA))

→ (( )((A)A))

→ (( )((A)(A)))

→ (( )(( )(A)))

→ (( )(( )( )))

L(G5) is the language of all

strings of properly nested

parentheses.

Page 17: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 17 of 22

Context-Free Languages

More on Derivations

• Consider the following

grammar G6 whose rules are

1. S → A1B

2. A → 0A ε

3. B → 0B 1B ε

• The set of variables V = {S, A,

B}, the set of terminals = {0,

1}, and the start state is S.

• In the process of deriving a

string, there will be situations

where there will be more than

one variable on the right-hand

side.

Page 18: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 18 of 22

Context-Free Languages

• The derivation obtained by

substituting the leftmost

variable at each step is called

the leftmost derivation.

Example:

In deriving the string 00101

using leftmost derivation:

S → A1B

→ 0A1B

→ 00A1B

→ 001B

→ 0010B

→ 00101B

→ 00101

Page 19: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 19 of 22

Context-Free Languages

• The derivation obtained by

substituting the rightmost

variable at each step is called

the rightmost derivation.

Example:

In deriving the string 00101

using rightmost derivation:

S → A1B

→ A10B

→ A101B

→ A101

→ 0A101

→ 00A101

→ 00101

Page 20: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 20 of 22

Context-Free Languages

• Example:

Consider grammar G7 with the

following rules:

1. S → 0AB

2. A → 1B1

3. B → A ε

Give the leftmost and

rightmost derivation of the

string 01111.

Leftmost Rightmost

S → 0AB S → 0AB

→ 01B1B → 0A

→ 01A1B → 01B1

→ 011B11B → 01A1

→ 01111B → 011B11

→ 01111 → 01111

Page 21: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 21 of 22

Context-Free Languages

Parse Trees

• The derivations obtained from

a context-free grammar can be

represented graphically using

a tree structure called parse

trees or derivation trees.

• Parse trees give a visualization

of the entire derivation of a

string.

• The variables occupy the

internal nodes of a tree with

the start variable being the

root of the tree. The terminals

occupy the leaf at the bottom

• The children of an internal

node (variables) are the right-

hand side string of a rule used

to expand the variable.

Page 22: MELJUN CORTES Automata Lecture Context-free Languages 2

Theory of Computation (With Automata Theory)

* Property of STI

Page 22 of 22

Context-Free Languages

• Example: The parse tree for

the derivation of the string

00101 using grammar G6.

S

1

ε

BA

A0 B0

A0 B1

ε