Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.

Slides:



Advertisements
Similar presentations
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory VII.
Advertisements

Theory Of Automata By Dr. MM Alam
CS5371 Theory of Computation
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
COP4020 Programming Languages
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
1 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Context-Free Grammars
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Chapter 5 Context-Free Grammars
Grammars CPSC 5135.
Context-free Languages
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
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 α 
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
Midterm 1 Breakdown >79 3 >29 7 >69 5 >19 5 >59 7 >49 9 >39 7.
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Chapter 5 Context-free Languages
Context-Free Languages
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Regular Grammars Reading: 3.3. What we know so far…  FSA = Regular Language  Regular Expression describes a Regular Language  Every Regular Language.
Chapter 5 Context-Free Languages Regular language are effective in describing certain simple patterns. But it is not enough to describe programming languages.
CSCI 2670 Introduction to Theory of Computing September 14, 2005.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Dept. of Computer Science & IT, FUUAST Theory of Computation 2 Regular Expressions Regular Expression.
Theory of Languages and Automata By: Mojtaba Khezrian.
AUTOMATA THEORY. Chapter 05 CONTEX-FREE GRAMMERS AND LANGUAGES.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Chapter 2. Formal Languages Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Chapter 1 INTRODUCTION TO THE THEORY OF COMPUTATION.
5. Context-Free Grammars and Languages
CONTEXT-FREE LANGUAGES
Formal Language & Automata Theory
G. Pullaiah College of Engineering and Technology
Recap lecture 33 Example of trees, Polish Notation, examples, Ambiguous CFG, example,
Recap Lecture 34 Example of Ambiguous Grammar, Example of Unambiguous Grammer (PALINDROME), Total Language tree with examples (Finite and infinite trees),
Context free grammar.
CSE 105 theory of computation
FORMAL LANGUAGES AND AUTOMATA THEORY
PARSE TREES.
Context-Free Languages
5. Context-Free Grammars and Languages
Finite Automata and Formal Languages
Chapter 2 Context-Free Language - 01
COSC 3340: Introduction to Theory of Computation
CSCI 432 Computer Science Theory
Presentation transcript:

Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages

A context-free language (CFL) is a formal language that can be defined by a context-free grammar (CFG). The set of all context-free languages is identical to the set of languages accepted by pushdown automata. Dept. of Computer Science & IT, FUUAST Automata Theory 3 Automata Theory V DEF. The class of context-free languages generalizes the class of regular languages, i.e., every regular language is a context-free language. The reverse of this is not true, i.e., every context-free language is not necessarily regular. For example, as we will see {0 k 1 k | k>=0} is context-free but not regular.

Dept. of Computer Science & IT, FUUAST Automata Theory 4 Automata Theory V Formally, a Context-Free Grammar (CFG) is a 4-tuple : G = (V, T, P, S) V-A finite set of variables or non-terminals T-A finite set of terminals (V and T do not intersect) P-A finite set of productions, each of the form A –> α, where A is in V and αis in (V U T)*, α may be ε S-A starting non-terminal (S is in V)

Dept. of Computer Science & IT, FUUAST Automata Theory 5 Automata Theory V Definition of Context-Free Grammars

Dept. of Computer Science & IT, FUUAST Automata Theory 6 Automata Theory V Definition of Context-Free Grammars ….. Continued Grammar G = (V, T, P, S)

Dept. of Computer Science & IT, FUUAST Automata Theory 7 Automata Theory V Example: Palindrome The grammar G pal for the palindrome is represented by G pal = ( {P}, {0,1}, A, P ). A represents above productions

Dept. of Computer Science & IT, FUUAST Automata Theory 8 Automata Theory V Right-Linear Grammar Context-Free Grammar G = (V, T, P, S) Productions forms: V  T*V or V  T* Left-Linear Grammar Productions forms: V  VT* or V  T*

Dept. of Computer Science & IT, FUUAST Automata Theory 9 Automata Theory V Examples: Grammar G = ({E,I}, T, P, E) E = (a+b)(a+b+0+1)* T = {+, *, (,), a, b, 0, 1} 1.E  I 2.E  E + E 3.E  E * E 4.E  (E) 5.I  a 6.I  b 7.I  Ia 8.I  Ib 9.I  I0 10.I  I1 Production Rules: Derivations:

Dept. of Computer Science & IT, FUUAST Automata Theory 10 Automata Theory V Grammar G = ( {S}, {a, b}, P, S) Production Rules: S  aSa S  bSb S   Derivation: S  aSa  aaSaa  aabSbaa  aabbaa

Dept. of Computer Science & IT, FUUAST Automata Theory 11 Automata Theory V Leftmost Derivations At each step of derivation we replace the leftmost variable by one of its production bodies Grammar G = ( {S}, {a, b}, P, S) Production Rules: 1.S  aSS 2.S  b Derivation S  aSS (Rule 1)  aaSSS (Rule 1)  aabSS (Rule 2)  aabaSSS (Rule 1)  aababSS (Rule 2)  aababbS (Rule 2)  aababbb (Rule 2)

Dept. of Computer Science & IT, FUUAST Automata Theory 12 Automata Theory V Rightmost Derivations At each step of derivation we replace the rightmost variable by one of its production bodies Grammar G = ( {S}, {a, b}, P, S) Production Rules: 1.S  aSS 2.S  b Derivation S  aSS (Rule 1)  aSb (Rule 2)  aaSSb (Rule 1)  aaSaSSb (Rule 1)  aaSaSbb (Rule 2)  aaSabbb (Rule 2)  aababbb (Rule 2)

Dept. of Computer Science & IT, FUUAST Automata Theory 13 Automata Theory V Sentential Forms Strings produced by derivation from the start symbol are termed as ‘Sentential Forms’. If the derivation is leftmost then the strings are left-sentential form. If the derivation is rightmost then the strings are right-sentential form. Derivation Trees or Parse Trees

Dept. of Computer Science & IT, FUUAST Automata Theory 14 Automata Theory V Derivations:

Dept. of Computer Science & IT, FUUAST Automata Theory 15 Automata Theory V Given a CFG with production rules as: S  aA A  aB B  bB B  a and productions as: S  aA  aaB  aabB  aaba Draw parse tree

Dept. of Computer Science & IT, FUUAST Automata Theory 16 Automata Theory V Given a CFG with production rules as: S  aAB A  Bba B  bB B  c and productions as: S  aAB  a(Bba)B  acbaB  acba(bB)  acbabc Draw parse tree

Dept. of Computer Science & IT, FUUAST Automata Theory 17 Automata Theory V Given a CFG with production rules as: S  aSb S  ab and productions as: S  aSb  aaSbb  aaabbb Draw parse tree

Dept. of Computer Science & IT, FUUAST Automata Theory 18 Automata Theory V Ambiguity 1.Ambiguity can be removed by introducing operator precedence 2.Ambiguity can be reduced by allowing association or grouping from left or right

Dept. of Computer Science & IT, FUUAST Automata Theory 19 Automata Theory V Ambiguity

Dept. of Computer Science & IT, FUUAST Automata Theory 20 Automata Theory V Application of Context-Free Grammars 1)Compiler Parsers 2)HTML 3)XML

Dept. of Computer Science & IT, FUUAST Automata Theory 21 Automata Theory V End of Chapter 5