Computability Joke. Context-free grammars Parsing. Chomsky

Slides:



Advertisements
Similar presentations
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Advertisements

Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
CFGs and PDAs Sipser 2 (pages ). Long long ago…
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Pushdown Automata Part II: PDAs and CFG Chapter 12.
CFGs and PDAs Sipser 2 (pages ). Last time…
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
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.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
Fall 2004COMP 3351 NPDA’s Accept Context-Free Languages.
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Finite State Machines Data Structures and Algorithms for Information Processing 1.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
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 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
Theory of Computation, Feodor F. Dragan, Kent State University 1 Regular expressions: definition An algebraic equivalent to finite automata. We can build.
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
The Pumping Lemma for Context Free Grammars. Chomsky Normal Form Chomsky Normal Form (CNF) is a simple and useful form of a CFG Every rule of a CNF grammar.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Design contex-free grammars that generate: L 1 = { u v : u ∈ {a,b}*, v ∈ {a, c}*, and |u| ≤ |v| ≤ 3 |u| }. L 2 = { a p b q c p a r b 2r : p, q, r ≥ 0 }
Context-free Languages
Computability Construct TMs. Decidability. Preview: next class: diagonalization and Halting theorem.
CHAPTER 1 Regular Languages
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
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.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Computability Regular expressions. Languages defined by regular expresses = Regular languages (languages recognized by FSM). Applications. Pumping lemma.
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.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
Costas Busch - LSU1 PDAs Accept Context-Free Languages.
1/29/02CSE460 - MSU1 Nondeterminism-NFA Section 4.1 of Martin Textbook CSE460 – Computability & Formal Language Theory Comp. Science & Engineering Michigan.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Formal Languages, Automata and Models of Computation
Closed book, closed notes
Context-Free Grammars: an overview
G. Pullaiah College of Engineering and Technology
CSE 105 theory of computation
NPDAs Accept Context-Free Languages
Simplifications of Context-Free Grammars
CSE 105 theory of computation
NPDAs Accept Context-Free Languages
Context-Free Languages
REGULAR LANGUAGES AND REGULAR GRAMMARS
Intro to Data Structures
CHAPTER 2 Context-Free Languages
Context-Free Grammars
Context-Free Languages
فصل دوم Context-Free Languages
Chapter 2 Context-Free Language - 01
CSE 105 theory of computation
CS21 Decidability and Tractability
Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars
… NPDAs continued.
CSE 105 theory of computation
Automata, Grammars and Languages
CSE 105 theory of computation
CSE 105 theory of computation
CSE 105 theory of computation
Presentation transcript:

Computability Joke. Context-free grammars Parsing. Chomsky Homework: Design grammar for [simple] computer language

Proof by induction Requires the subject domain to be classified by natural numbers: 0 or 1 or some starting point, and then all numbers following Prove a starting case, for example, N=1 Prove either if it is true for k, FOR ALL k, can prove it for k+1 if it is true for p<k, FOR ALL k, can prove it for k Think of induction step as short cut to proving theorem for 2, 3, 4, 5, … SO, with my screaming capital letters as a hint, what was wrong with the All horses are the same color proof?

Preview on proofs Another typical form of proof is by construction build / design the FSM, etc. Another is by contradiction: assuming result and show it leads to some falsehood One category is: assuming you can make a list of all Xs…. then some special example must be on the list but then ….

Hierarchy Moving from languages defined by FSM (aka finite state automata), equivalent to non-deterministic FSM, equivalent to regular expressions to Languages defined by Context-Free Grammars, equivalent to [non-deterministic ] push-down automata will turn out that deterministic PDA are less powerful. a FSM can be considered a special type of PDA

Grammar A grammar has a [finite] alphabet A (sometimes called terminals) plus a finite set V of variables. Starting symbol S is a member of V. A production rule is a mapping/substitution of strings A grammar has a finite set of production rules A context-free grammar has production rules of the form a single variable V to a string of symbols from A and V V string of letters from A and V A non-context-free production rule would be aVb adWb, meaning, when V is in the context of a and b, then you can substitute dW Can combine production rules using |

Derivations Applying the rules until there are no more variables, just terminals is a derivation. A string is in the language defined by the grammar if there is a derivation. Think of the variables as the parts of speech.

Example Let the alphabet of terminals be: (, ), +, *, v, w, x, y, z Let the variables be E, the starting symbol, think of it as expression F, factor OP, operator (I use two letters for readability) Rules are E  ( E ) | E OP E | F F  v | w | x | y | z OP  + | *

Sample derivation E E OP E E  ( E ) E  E OP E E  F F  v OP  + F  w etc. FINISH! Draw as a tree. Trees in computer science are upside down!

Parsing a string is producing a set of rules, often recorded using a tree, that derive (cover) the string. So for the string (x+y) E  ( E ) E  E OP E E  F  x E  R  y

Parsing If there isn't a parse tree, then the string isn't in the language, though it may require some proof…

Derivation vs Parsing Opposite directions The goal of parsing is to find a derivation that generates the string. In compiling, parsing produces information that directs the compiler to generate code.

Exercises Produce the tree(s) for x x + (v*w) x + y * z (x*y)+(v*w) When are trees the same and when are they different? ambiguity is when the trees are really different, not just expanded in a different order. This will be made formal next.

Left most derivation A derivation of a string w in a Grammar G is a leftmost derivation if at each step the leftmost remaining variable is the one replaced. A string is derived ambiguously in a CF grammar if it has two or more different leftmost derivations. A grammar G is ambiguous if it generates some string ambiguously.

Compare for ambiguity Variables E, T (for term), F (for factor), alphabet {a, +, *, (, ) } Rules: E  E+T | T T  T * F | F F  (E) | a Variables E, alphabet { a, +, *, (, ) } Rules: E  E+E | E*E | (E) | a Try each on the strings: a+a*a a+(a*a) (a+a)*a a+a+a+a

Regular languages All regular languages are context free languages! Proof: Consider the FSM that recognizes a language. Define the following Context-free grammar: alphabet for the FSM is the terminal alphabet let each state of the FSM be a variable. Let the initial state be the initial variable. Rules are: if there is an arrow from state V to state W labeled with letter a, then add the production rule: V  aW If state X is an accepting state, add the rule X  ∊ So…strings generated by the grammar are the strings in the language.

CF languages Each regular language is CF, but not vice versa… Recall B = {0i1i | i>=0}. B is strings with the a set number of 0s followed by the same number of 1s. This was shown to be non-regular. Let grammar be S  0S1 | ∊

Chomsky normal form A CF grammar is in Chomsky normal form if each rule is of the form A  BC A  a where A, B, and C are variables and a is any terminal and B and C are not the start variable S. It is permitted (but not required) to have the rule S  ∊ but no other variable can produce the empty string. There are several other normal forms.

Outline of proof for Chomsky NF Any context free language can be generated by a grammar in Chomsky normal form. Create a new start variable to prevent the start variable being on the right Eliminate A  ∊ rules. If there is a rule R  uAv, add rule R  uv. If R  uAvAw, add R  uvAw | uAvw | uvw Remove unit rules A  B. If B  u, then add A  u (unless previously removed) If A  u1u2…uk and k>=3, add new variable Ai and replace with A  u1A1, A1  u2A2, etc. If A  u1u2, replace with A  U1U2 and U1  u1 and U2  u2 Read on-line, Sipser text on reserve, videos, etc. for complete proof.

Example: B = {0i1i | i>=0} Convert S  0S1 | ∊ to CNF add new start and new rule: S0  S remove S  ∊ and add S  01 | 0S1 and S0  ∊ replace unit rule (S0  S): S0  01 | 0S1| ∊ and S  01 | 0S1 address other problems by creating new variables S0  A0A1 | A0A3 | ∊ S  A0A1 | A0A3 A0  0 A1  1 A3  SA1 Does this work (produce strings in the pattern)? Claim: yes, because notice that an A3 only arises if there was an A0 before it.

Intuition…. Context free grammars appear to be able to keep track of things…. Even the leftmost derivation rule still has something like recursion.

Preview Will define push-down automata, a type of machine equivalent to context-free grammars for defining languages Pumping lemma

Classwork/Homework Create a grammar for a simple programming language: assignment statements if statements function calls expressions can include function calls as well as operators and parentheses terminals are names and numbers (lexical units) plus operators (+ and *) and parentheses, brackets, and ;