grammar types there are 4 types of grammars according to the types of rules: – general grammars...

17
Grammar types • There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars – Linear grammars

Upload: brianne-alsbury

Post on 31-Mar-2015

259 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Grammar types

• There are 4 types of grammars according to the types of rules:

– General grammars– Context Sensitive grammars– Context Free grammars– Linear grammars

Page 2: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Grammar types

• There are 4 types of grammars according to the types of rules:

• Each type recognizes a set of languages.

– General grammars → RE languages– Context Sensitive grammars → CS languages– Context Free grammars → CF languages– Linear grammars → Regular languages

Page 3: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Chomsky Hierarchy

• There are 4 types of grammars according to the types of rules:

• Each type recognizes a set of languages. • Each language is a subset of the above– Recursively Enumerable languages ⊃ – Context Sensitive languages ⊃ – Context Free languages ⊃ – Regular languages

Page 4: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Context free grammars

• The production rules must be of the formA ⟶ α– A is in V – α is in (V∪T)*

Page 5: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Linear grammars

Right Linear Grammars:Rules of the forms

A → εA → aA → aB

A,B: variables anda: terminal

The Linear Grammars are either left or right:

Left Linear Grammars:Rules of the forms

A → εA → aA → Ba

A,B: variables andA: terminal

Page 6: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Example

S → aS | bAA → cA | ε

Page 7: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Example

S → aS | bAA → cA | ε

S → aS → aaS → … → a…aS →a…abA →a…abcA → a…abccA → … → a…abc…c

Page 8: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Example

S → aS | bAA → cA | ε

This grammar produces the language L(a*bc*)

S → aS → aaS → … → a…aS →a…abA →a…abcA → a…abccA → … → a…abc…c

Page 9: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Regular Languages ⊂ CF Languages

• A Linear Grammar is also a CF grammar: – Every production rule is of the form A⟶α, with A

being a variable and α being either ε, a or Ba (a string of variables and terminals).

• Thus the language produced by a linear grammar is a Context Free languages.

• We will see that linear grammars actually produce the Regular Languages, which means that all the Regular Languages are also CF.

• The converse is not true!!! For example LR = {wwR: w in {a, b}* } is not a regular language.

Page 10: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Right Linear Grammars produce exactly the Regular Languages

Two directions:1. Given a Right Linear grammar construct an

NFAε that recognizes the same language with the Right Linear grammar.

2. Given an NFA construct a Right Linear grammar that describes the same language with the NFA.

Page 11: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

1. Right Linear Grammar → NFAε

Suppose that I have a right linear grammar (V, Σ, S, P). I construct an NFAε (Q, Σ, δ, S, {f}).

• The set of states Q will be the set V U {f}, where f is a new symbol denoting a final state

• Productions in P have three possible forms:– A → ε : add the transition δ(Α,ε) = f– A → a : add the transition δ(A,a) = f– A → aB : add the transition δ(Α,a) = B

Page 12: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Examples

1) Transform the following Right Linear grammar in an equivalent NFAε.

S → aS | bAA → cA | ε

Solution:

S Α f

ca

b ε

Page 13: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

2. NFA → Right Linear Grammar

Suppose that I have an NFA (Q, Σ, δ, q0, F,). I construct a right linear grammar (Q, Σ, q0, P).

• For each transition δ(qi ,a) = qj, I construct the rule qi → aqj in P.

• Furthermore, for every state qi in F I add the rule qi → ε in P.

Page 14: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Examples

2) Transform the following DFA to a right linear grammar

Solution:q0 → aq1 | bq0

q1 → aq1 | bq0 | ε

q1q0a

ab

b

Page 15: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Left Linear Grammars

• It can be shown that Left Linear Grammars also produce the Regular Languages but this is not so straightforward.

• Actually, a Left Linear grammar produces the reverse of the language produced by the Right Linear grammar in which we reversed the rules A → Ba to A →aB.

• But the set of the reverse languages of all the Regular Languages is exactly the set of the Regular Languages. So the Left Linear Grammars produce the Regular Languages.

Page 16: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Example

C → BcB → AbA → aThe derivation of abc is:C → Bc → Abc → abc, or

abc ← Abc ← Bc ← CSo I should start creating the string abc from right to

left. But this is equivalent with creating the reverse of cba.

C → cB → cbA → cba and then take the reverse.

Page 17: Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –

Example (continued)

The Right Linear grammar with the rules A → Ba reversed is

C → cBB → bAA → aand it produces the reverse language. So, just create the NFAε for the language produced by

the Right Linear grammar and then compute the reverse (change start with final state and reverse the arrows). This is an NFAε for the Left Linear grammar.