context sensitive grammar - dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · de nition...

30
Context Sensitive Grammar Aparna S Vijayan Department of Computer Science and Automation December 2, 2011 Aparna S Vijayan (CSA) CSG December 2, 2011 1 / 12

Upload: vonguyet

Post on 27-Mar-2018

306 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Grammar

Aparna S Vijayan

Department of Computer Science and Automation

December 2, 2011

Aparna S Vijayan (CSA) CSG December 2, 2011 1 / 12

Page 2: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Contents

1 Definition of CSG

2 Context Sensitive Language(CSL)

3 Example of CSL

4 Chomsky Hierachy

Aparna S Vijayan (CSA) CSG December 2, 2011 2 / 12

Page 3: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Contents

1 Definition of CSG

2 Context Sensitive Language(CSL)

3 Example of CSL

4 Chomsky Hierachy

Aparna S Vijayan (CSA) CSG December 2, 2011 2 / 12

Page 4: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Contents

1 Definition of CSG

2 Context Sensitive Language(CSL)

3 Example of CSL

4 Chomsky Hierachy

Aparna S Vijayan (CSA) CSG December 2, 2011 2 / 12

Page 5: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Contents

1 Definition of CSG

2 Context Sensitive Language(CSL)

3 Example of CSL

4 Chomsky Hierachy

Aparna S Vijayan (CSA) CSG December 2, 2011 2 / 12

Page 6: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Contents

1 Definition of CSG

2 Context Sensitive Language(CSL)

3 Example of CSL

4 Chomsky Hierachy

Aparna S Vijayan (CSA) CSG December 2, 2011 2 / 12

Page 7: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Definition

Context Sensitive Grammar(Type1 Grammar)

A context-sensitive grammar (CSG) is an unrestricted grammar inwhich every production has the formα→ β with |β| ≥ |α| (where α and β are strings of nonterminals andterminals).

The concept of context-sensitive grammar was introduced by NoamChomsky in the 1950.

In every derivation the length of the string never decreases.

The term ”context-sensitive” comes from a normal form for thesegrammars,where each production is of the form α1Aα2→α1βα2,withβ 6= ε.

They permit replacement of variable A by string β only in the”context ” α1 - α2.

Aparna S Vijayan (CSA) CSG December 2, 2011 3 / 12

Page 8: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Formal Definition

A Context Sensitive Grammar is a 4-tuple , G = (N,Σ, P, S) where:

N=Set of non terminal symbols.

Σ=Set of terminal symbols.

S=Start symbol of the production.

P=Finite set of productions.All rules in P are of the form α1Aα2→α1βα2.

A ∈ N ( A is a single nonterminal)α1, α2, β ∈ (N ∪ Σ)+.

Aparna S Vijayan (CSA) CSG December 2, 2011 4 / 12

Page 9: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Formal Definition

A Context Sensitive Grammar is a 4-tuple , G = (N,Σ, P, S) where:

N=Set of non terminal symbols.

Σ=Set of terminal symbols.

S=Start symbol of the production.

P=Finite set of productions.All rules in P are of the form α1Aα2→α1βα2.

A ∈ N ( A is a single nonterminal)α1, α2, β ∈ (N ∪ Σ)+.

Aparna S Vijayan (CSA) CSG December 2, 2011 4 / 12

Page 10: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Formal Definition

A Context Sensitive Grammar is a 4-tuple , G = (N,Σ, P, S) where:

N=Set of non terminal symbols.

Σ=Set of terminal symbols.

S=Start symbol of the production.

P=Finite set of productions.All rules in P are of the form α1Aα2→α1βα2.

A ∈ N ( A is a single nonterminal)α1, α2, β ∈ (N ∪ Σ)+.

Aparna S Vijayan (CSA) CSG December 2, 2011 4 / 12

Page 11: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Formal Definition

A Context Sensitive Grammar is a 4-tuple , G = (N,Σ, P, S) where:

N=Set of non terminal symbols.

Σ=Set of terminal symbols.

S=Start symbol of the production.

P=Finite set of productions.All rules in P are of the form α1Aα2→α1βα2.

A ∈ N ( A is a single nonterminal)α1, α2, β ∈ (N ∪ Σ)+.

Aparna S Vijayan (CSA) CSG December 2, 2011 4 / 12

Page 12: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Formal Definition

A Context Sensitive Grammar is a 4-tuple , G = (N,Σ, P, S) where:

N=Set of non terminal symbols.

Σ=Set of terminal symbols.

S=Start symbol of the production.

P=Finite set of productions.

All rules in P are of the form α1Aα2→α1βα2.

A ∈ N ( A is a single nonterminal)α1, α2, β ∈ (N ∪ Σ)+.

Aparna S Vijayan (CSA) CSG December 2, 2011 4 / 12

Page 13: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Formal Definition

A Context Sensitive Grammar is a 4-tuple , G = (N,Σ, P, S) where:

N=Set of non terminal symbols.

Σ=Set of terminal symbols.

S=Start symbol of the production.

P=Finite set of productions.All rules in P are of the form α1Aα2→α1βα2.

A ∈ N ( A is a single nonterminal)α1, α2, β ∈ (N ∪ Σ)+.

Aparna S Vijayan (CSA) CSG December 2, 2011 4 / 12

Page 14: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Language

The language generated by the Context Sensitive Grammar is calledcontext sensitive language.

If G is a Context Sensitive Grammar thenL(G)=

{w∣∣(w ∈ Σ∗) ∧

(S ⇒+

G w)}

.

Eg 1 of a context sensitive grammarG = {{S ,A,B,C , a, b, c} , {a, b, c} ,P,S}where P is the set of rules.

S→ aSBCS → aBCCB → BCaB → abbB → bbbC → bccC →cc

The language generated by this grammar is {anbncn|n ≥ 1} .

Aparna S Vijayan (CSA) CSG December 2, 2011 5 / 12

Page 15: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Language

The language generated by the Context Sensitive Grammar is calledcontext sensitive language.

If G is a Context Sensitive Grammar thenL(G)=

{w∣∣(w ∈ Σ∗) ∧

(S ⇒+

G w)}

.

Eg 1 of a context sensitive grammarG = {{S ,A,B,C , a, b, c} , {a, b, c} ,P,S}where P is the set of rules.

S→ aSBCS → aBCCB → BCaB → abbB → bbbC → bccC →cc

The language generated by this grammar is {anbncn|n ≥ 1} .

Aparna S Vijayan (CSA) CSG December 2, 2011 5 / 12

Page 16: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Language

The language generated by the Context Sensitive Grammar is calledcontext sensitive language.

If G is a Context Sensitive Grammar thenL(G)=

{w∣∣(w ∈ Σ∗) ∧

(S ⇒+

G w)}

.

Eg 1 of a context sensitive grammarG = {{S ,A,B,C , a, b, c} , {a, b, c} ,P,S}where P is the set of rules.

S→ aSBCS → aBCCB → BCaB → abbB → bbbC → bccC →cc

The language generated by this grammar is {anbncn|n ≥ 1} .Aparna S Vijayan (CSA) CSG December 2, 2011 5 / 12

Page 17: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Language

The derivation for the string aabbcc is

S ⇒ aSBC⇒ aaBCBC⇒ aabCBC⇒ aabBCC⇒ aabbCC⇒ aabbcC⇒ aabbcc

Aparna S Vijayan (CSA) CSG December 2, 2011 6 / 12

Page 18: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Language

The derivation for the string aabbcc is

S ⇒ aSBC

⇒ aaBCBC⇒ aabCBC⇒ aabBCC⇒ aabbCC⇒ aabbcC⇒ aabbcc

Aparna S Vijayan (CSA) CSG December 2, 2011 6 / 12

Page 19: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Language

The derivation for the string aabbcc is

S ⇒ aSBC⇒ aaBCBC

⇒ aabCBC⇒ aabBCC⇒ aabbCC⇒ aabbcC⇒ aabbcc

Aparna S Vijayan (CSA) CSG December 2, 2011 6 / 12

Page 20: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Language

The derivation for the string aabbcc is

S ⇒ aSBC⇒ aaBCBC⇒ aabCBC

⇒ aabBCC⇒ aabbCC⇒ aabbcC⇒ aabbcc

Aparna S Vijayan (CSA) CSG December 2, 2011 6 / 12

Page 21: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Language

The derivation for the string aabbcc is

S ⇒ aSBC⇒ aaBCBC⇒ aabCBC⇒ aabBCC

⇒ aabbCC⇒ aabbcC⇒ aabbcc

Aparna S Vijayan (CSA) CSG December 2, 2011 6 / 12

Page 22: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Language

The derivation for the string aabbcc is

S ⇒ aSBC⇒ aaBCBC⇒ aabCBC⇒ aabBCC⇒ aabbCC

⇒ aabbcC⇒ aabbcc

Aparna S Vijayan (CSA) CSG December 2, 2011 6 / 12

Page 23: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Language

The derivation for the string aabbcc is

S ⇒ aSBC⇒ aaBCBC⇒ aabCBC⇒ aabBCC⇒ aabbCC⇒ aabbcC

⇒ aabbcc

Aparna S Vijayan (CSA) CSG December 2, 2011 6 / 12

Page 24: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Context Sensitive Language

The derivation for the string aabbcc is

S ⇒ aSBC⇒ aaBCBC⇒ aabCBC⇒ aabBCC⇒ aabbCC⇒ aabbcC⇒ aabbcc

Aparna S Vijayan (CSA) CSG December 2, 2011 6 / 12

Page 25: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Example2 CSG L = (#a = #b = #c)

G2 =({S ,A,B,C , a, b, c} , {a, b, c} ,P,S)

S→ ABC

S →ABCS

AB→ BA

AC → CA

BC → CB

BA → AB

CA →AC

CB → BC

A → a

B →b

C →cAparna S Vijayan (CSA) CSG December 2, 2011 7 / 12

Page 26: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Hierarchy of Formal Languages

Aparna S Vijayan (CSA) CSG December 2, 2011 8 / 12

Page 27: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Relation between Formal Languages

The CFL’s not containing ε are properly contained in the contextsensitive languages

Not all Context Sensitive Languages are Context Free.

Every Context Sensitive Language is recusive.

Aparna S Vijayan (CSA) CSG December 2, 2011 9 / 12

Page 28: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Chomsky Hierachy

Described by Chomsky in 1956.

Four classes of language Type 3,Type2,Type1,Type 0 from mostrestrictive to most general(Unestricted).

Each level of hierarchy can be characterized by a class of grammar.

Aparna S Vijayan (CSA) CSG December 2, 2011 10 / 12

Page 29: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

Chomsky Hierarchy

Type Language productions Device3 Regular A → aB, A → a Finite Automaton

2 CFL A → α Pushdown Automaton

1 CSL α→ β,|β| ≥ |α| Linear Bounded Automaton

0 RE α→ β Turing Machine

A,B→ Nonterminals

α,β→ string of terminals and nonterminals

a→ terminal symbol

Aparna S Vijayan (CSA) CSG December 2, 2011 11 / 12

Page 30: Context Sensitive Grammar - Dronadrona.csa.iisc.ernet.in/~deepakd/atc-2011/csl.pdf · De nition Context Sensitive Grammar(Type1 Grammar) A context-sensitive grammar (CSG) is an unrestricted

References

Introduction to Automata Theory Languages and Computation by ”JOHN EHOPCROFT”,”JEFFERY D.ULLMAN”.

Introduction to Languages and the Theory of Computation ”JOHN MARTIN”

http://en.wikipedia.org/wiki/Context-sensitive grammar

http://adammikeal.org/courses/compute/Chomsky

Aparna S Vijayan (CSA) CSG December 2, 2011 12 / 12