Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars –"— Presentation transcript:

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

2 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

3 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

4 Context free grammars The production rules must be of the form A ⟶ α – A is in V – α is in (V∪T)*

5 Linear grammars Right Linear Grammars: Rules of the forms A → ε A → a A → aB A,B: variables and a: terminal The Linear Grammars are either left or right: Left Linear Grammars: Rules of the forms A → ε A → a A → Ba A,B: variables and A: terminal

6 Example S → aS | bA A → cA | ε

7 Example S → aS | bA A → cA | ε S → aS → aaS → … → a…aS →a…abA →a…abcA → a…abccA → … → a…abc…c

8 Example S → aS | bA A → cA | ε This grammar produces the language L(a * bc * ) S → aS → aaS → … → a…aS →a…abA →a…abcA → a…abccA → … → a…abc…c

9 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 L R = {ww R : w in {a, b}* } is not a regular language.

10 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.

11 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

12 Examples 1)Transform the following Right Linear grammar in an equivalent NFA ε. S → aS | bA A → cA | ε Solution: SΑ f ca bε

13 2. NFA → Right Linear Grammar Suppose that I have an NFA (Q, Σ, δ, q 0, F,). I construct a right linear grammar (Q, Σ, q 0, P). For each transition δ(q i,a) = q j, I construct the rule q i → aq j in P. Furthermore, for every state q i in F I add the rule q i → ε in P.

14 Examples 2) Transform the following DFA to a right linear grammar Solution: q 0 → aq 1 | bq 0 q 1 → aq 1 | bq 0 | ε q1q1 q0q0 a ab b

15 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.

16 Example C → Bc B → Ab A → a The derivation of abc is: C → Bc → Abc → abc, or abc ← Abc ← Bc ← C So 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.

17 Example (continued) The Right Linear grammar with the rules A → Ba reversed is C → cB B → bA A → a and 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.


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

Similar presentations


Ads by Google