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

Slides:



Advertisements
Similar presentations
NFAε - NFA - DFA equivalence
Advertisements

Lecture 10: Context-Free Languages Contextually David Evans
Lecture 11 Context-Free Grammar. Definition A context-free grammar (CFG) G is a quadruple (V, Σ, R, S) where V: a set of non-terminal symbols Σ: a set.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
Regular Grammars Formal definition of a regular expression.
CS 3240 – Chapter 3.  How would you delete all C++ files from a directory from the command line?  How about all PowerPoint files that start with the.
CS5371 Theory of Computation
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
Fall 2005 CSE 467/567 1 Formal languages regular expressions regular languages finite state machines.
CS5371 Theory of Computation Lecture 6: Automata Theory IV (Regular Expression = NFA = DFA)
79 Regular Expression Regular expressions over an alphabet  are defined recursively as follows. (1) Ø, which denotes the empty set, is a regular expression.
CS5371 Theory of Computation Lecture 12: Computability III (Decidable Languages relating to DFA, NFA, and CFG)
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
Normal Forms for Context-Free Grammars Definition: A symbol X in V  T is useless in a CFG G=(V, T, P, S) if there does not exist a derivation of the form.
Grammars CPSC 5135.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Regular Expressions Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Translators.
Regular Grammars Chapter 7. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
Grammar G = (V N, V T, P, S) –V N : Nonterminal symbols –V T : Terminal symbols V N  V T = , V N ∪ V T = V – P : a finite set of production rules α 
Regular Grammars Chapter 7 1. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
CS 3813: Introduction to Formal Languages and Automata
Grammars A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
Regular Grammars Reading: 3.3. What we know so far…  FSA = Regular Language  Regular Expression describes a Regular Language  Every Regular Language.
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
Regular Expressions CS 130: Theory of Computation HMU textbook, Chapter 3.
1 1. Eliminate all  -transitions from the following FA without changing the number of states and the language accepted by the automaton. You should also.
 2005 SDU Lecture11 Decidability.  2005 SDU 2 Topics Discuss the power of algorithms to solve problems. Demonstrate that some problems can be solved.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Chapter 2. Formal Languages Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Topic 3: Automata Theory 1. OutlineOutline Finite state machine, Regular expressions, DFA, NDFA, and their equivalence, Grammars and Chomsky hierarchy.
Theory of Languages and Automata By: Mojtaba Khezrian.
PROGRAMMING LANGUAGES
5. Context-Free Grammars and Languages
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Chomsky Normal Form CYK Algorithm
Syntax Specification and Analysis
Theory of Languages and Automata
Linear Bounded Automata LBAs
Language and Grammar classes
Complexity and Computability Theory I
Natural Language Processing - Formal Language -
Context Sensitive Grammar & Turing Machines
CSCE 355 Foundations of Computation
Lecture 22 Pumping Lemma for Context Free Languages
The chomsky hierarchy Module 03.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
CS314 – Section 5 Recitation 3
Push-down Automata Section 3.3 Wed, Oct 27, 2004.
Discrete Math II Howon Kim
Context Free Languages
Finite-state automata
Context-Free Languages
REGULAR LANGUAGES AND REGULAR GRAMMARS
A HIERARCHY OF FORMAL LANGUAGES AND AUTOMATA
Regular Grammar.
LR(1) grammars The Chinese University of Hong Kong Fall 2010
Regular Grammars.
Chapter 7 Regular Grammars
5. Context-Free Grammars and Languages
Finite Automata Reading: Chapter 2.
Programming Language Concepts
Lecture Closure Properties for Regular Languages
Homework #5 (Models of Computation, Spring, 2001) Due: Section 1; February 27 Section 2; February 28 ** Please put your homework in the collection.
LR(1) grammars The Chinese University of Hong Kong Fall 2011
Recap Lecture 4 Regular expression of EVEN-EVEN language, Difference between a* + b* and (a+b)*, Equivalent regular expressions; sum, product and closure.
Presentation transcript:

Grammar types There are 4 types of grammars according to the types of rules: – General grammars – Context Sensitive grammars – Context Free grammars – Linear 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

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

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

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

Example S → aS | bA A → cA | ε

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

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

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.

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.

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

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

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.

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

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.

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.

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.