Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.

Slides:



Advertisements
Similar presentations
4.5 Inherently Ambiguous Context-free Language For some context-free languages, such as arithmetic expressions, may have many different CFG’s to generate.
Advertisements

Erik Jonsson School of Engineering and Computer Science FEARLESS Engineering CS 4384 – 001 Automata Theory Thursday: Context-Free.
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
CFGs and PDAs Sipser 2 (pages ). Last time…
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
CS5371 Theory of Computation
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
Courtesy Costas Busch - RPI1 Context-Free Languages.
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
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.
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
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]
Context-Free Grammars – Chomsky Normal Form Lecture 16 Section 2.1 Wed, Sep 26, 2007.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
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.
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Grammars CPSC 5135.
Context-free Languages
Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
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.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
CS 3240: Languages and Computation Context-Free Languages.
CONTEXT FREE GRAMMAR presented by Mahender reddy.
Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein.
Midterm 1 Breakdown >79 3 >29 7 >69 5 >19 5 >59 7 >49 9 >39 7.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
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.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CSCI 2670 Introduction to Theory of Computing September 14, 2005.
Dept. of Computer Science & IT, FUUAST Theory of Computation 2 Regular Expressions Regular Expression.
CSE 311 Foundations of Computing I Lecture 20 Context-Free Grammars and Languages Autumn 2012 CSE
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
5. Context-Free Grammars and Languages
Context-Free Grammars: an overview
Formal Language & Automata Theory
Relationship to Left- and Rightmost Derivations
Context-free Languages
5. Context-Free Grammars and Languages
CS21 Decidability and Tractability
CHAPTER 2 Context-Free Languages
Context-Free Languages
Relationship to Left- and Rightmost Derivations
Chapter 2 Context-Free Language - 01
Context-Free Grammars
Presentation transcript:

Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming languages

Context-free Grammars [Section 2.1] Example:A -> 0A1 A -> B B -> # Terminology: - substitution rules (productions) - variables (including the start variable) – typically upper-case - terminals – typically lower-case, other symbols - derivation, parse tree

Context-free Grammars [Section 2.1] Def 2.2: A context-free grammar is a 4-tuple (V, §,R,S), where - V is a finite set of variables - § is a finite set of terminals, V Å § = ; - R is a finite set of rules, each rule is of the form A -> w where A 2 V and w 2 (V [ § )* - S 2 V is the start variable If A -> w 2 P, then we write uAv => uwv (read “uAv yields uwv”), and we write u =>* v (read “u derives v”) if u=v or if there exists a sequence u 1,u 2,…,u k such that u => u 1 => u 2 => … u k => v The language of the grammar is { w 2 § * | S=>*w }.

Context-free Grammars [Section 2.1] Examples: give context-free grammars for the following languages: - { a i b j c k | i=k, i,j,k ¸ 0 } - { a i b j c k | i=j, i,j,k ¸ 0 } - strings over { (, ) } that are well-parenthesized - strings over { 0,1 } that contain equal number of 0’s and 1’s

Ambiguity [Section 2.1] Example: [EXPR] ->[EXPR] + [EXPR] | [EXPR] x [EXPR] | ( [EXPR] ) | a Give a derivation (and parse trees) for the string a+axa. Notice: for every parse tree there is a unique left-most derivation.

Ambiguity [Section 2.1] Def 2.7: A context-free grammar is called ambiguous if there exists a string that can be generated by two different left-most derivations. Note: Some context-free languages do not have unambigous grammars (e.g { a i b j c k | i=j or j=k } ). These are called inherently ambiguous. Example: give an unambigous CFG for the language of arithmetic expressions over { +, x, a }

Ambiguity [Section 2.1] Def 2.7: A context-free grammar is called ambiguous if there exists a string that can be generated by two different left-most derivations. For every x 2 \Sigma let y… L = {a^ib^jc^k | i,j,k\geq 0}. Example: give an unambigous CFG for the language of arithmetic expressions over { +, x, a }

Chomsky Normal Form [Section 2.1] Def 2.8: A CFG is in Chomsky normal form if every rule is of the form A -> BC or A -> a, where B,C 2 V-{S}, and a 2 §. Thm 2.9: Any CFL can be generated by a CFG in Chomsky normal form. Note: what about CFL that contains ε ? Why a normal form ?

Chomsky Normal Form [Section 2.1] Thm 2.9: Any CFL can be generated by a CFG in Chomsky normal form. “Proof” by example: S -> ASA | aBThings to fix: 1. A -> B | S 2. B -> b | ε 3. 4.