CS 154 Formal Languages and Computability March 8 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.

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

Closure Properties of CFL's
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.
Fall 2004COMP 3351 Simplifications of Context-Free Grammars.
Prof. Busch - LSU1 Simplifications of Context-Free Grammars.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory VII.
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
CS5371 Theory of Computation
1 … NPDAs continued. 2 Pushing Strings Input symbol Pop symbol Push string.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 9, 2006.
Costas Buch - RPI1 Simplifications of Context-Free Grammars.
1 Converting NPDAs to Context-Free Grammars. 2 For any NPDA we will construct a context-free grammar with.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Fall 2004COMP 3351 NPDA’s Accept Context-Free Languages.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule substitute B equivalent grammar.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
CS 490: Automata and Language Theory Daniel Firpo Spring 2003.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
Normal forms for Context-Free Grammars
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
Finite State Machines Data Structures and Algorithms for Information Processing 1.
CS 3813 Introduction to Formal Languages and Automata Chapter 6 Simplification of Context-free Grammars and Normal Forms These class notes are based on.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
Context-Free Grammars Normal Forms Chapter 11. Normal Forms A normal form F for a set C of data objects is a form, i.e., a set of syntactically valid.
Pushdown Automata (PDAs)
Cs3102: Theory of Computation Class 6: Pushdown Automata Spring 2010 University of Virginia David Evans TexPoint fonts used in EMF. Read the TexPoint manual.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
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.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
CS 154 Formal Languages and Computability February 4 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
CS 154 Formal Languages and Computability February 11 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
CS 154 Formal Languages and Computability February 9 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
CS 154 Formal Languages and Computability February 11 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
CS 154 Formal Languages and Computability April 12 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
1 Chapter Pushdown Automata. 2 Section 12.2 Pushdown Automata A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations.
CS 154 Formal Languages and Computability March 17 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 154 Formal Languages and Computability March 10 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 154 Formal Languages and Computability March 15 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Theory of Languages and Automata By: Mojtaba Khezrian.
CS 154 Formal Languages and Computability May 12 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 154 Formal Languages and Computability April 28 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 154 Formal Languages and Computability March 22 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
Formal Languages, Automata and Models of Computation
Context-Free Grammars: an overview
Deterministic FA/ PDA Sequential Machine Theory Prof. K. J. Hintz
Complexity and Computability Theory I
Simplifications of Context-Free Grammars
PDAs Accept Context-Free Languages
Chapter 7 PUSHDOWN AUTOMATA.
NPDAs Accept Context-Free Languages
CHAPTER 2 Context-Free Languages
Principles of Computing – UFCFA3-30-1
… NPDAs continued.
Normal Forms for Context-free Grammars
Context-Free Languages
Presentation transcript:

CS 154 Formal Languages and Computability March 8 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak BNF for Simple Expressions 2 ::= ::= ( (+ ) | (- ) )* ::= ( (* ) | (/ ) )* ::= | - | ( ) ::= ::= (0|1|2|3|4|5|6|7|8|9)+

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak JavaCC Simple Expressions 3 double expression() throws NumberFormatException : { double v; } { v = simpleExpr() { return v; } } double simpleExpr() throws NumberFormatException : { double v, value; } { value = term() ( v = term() { value += v; } | v = term() { value -= v; } )* { return value; } } ::= ::= ( (+ ) | (- ) )*

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak JavaCC Simple Expressions, cont’d 4 double term() throws NumberFormatException : { double v, value; } { value = factor() ( v = factor() { value *= v; } | v = factor() { value /= v; } )* { return value; } } ::= ( (* ) | (/ ) )*

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak JavaCC Simple Expressions, cont’d 5 ::= | - | ( ) ::= double factor() throws NumberFormatException : { double value; } { value = primary() { return value; } | value = factor() { return -value; } | value = expression() { return value; } } double primary() throws NumberFormatException : { Token t; } { t = { return Double.parseDouble(t.image); } }

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak JavaCC Expression Tokens 6 SKIP : { } TOKEN : { )+> | | }

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak JavaCC Expression Parser 7 public class SimpleCalculator { public static void main(String[] args) { Reader sr = new InputStreamReader(System.in); SimpleCalculator calc = new SimpleCalculator(sr); try { for (;;) { System.out.print("\nExpression? "); double value = calc.calculate(); System.out.println("= " + value); } } catch (Exception ex) { ex.printStackTrace(); } } }

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak JavaCC Expression Parser, cont’d 8 double calculate() throws NumberFormatException : { double v; } { v = expression() { return v; } } Demo

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Assignment #4  Expand the JavaCC-generated Simple Calculator to a JavaCC-generated Super Calculator.  Expand the number token to include real numbers as we had earlier defined them.  Add the exponentiation operator ^ Highest precedence level  Add the relational operators == != >= Lowest precedence level 9

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Assignment #4, cont’d  Add the logical “or” operator || Same precedence level as + and –  Add the logical “and” operator && Same precedence level as * and /  Add the logical “not” operator ! Same precedence level as the unary –  All calculated values should be doubles.  Use 0 for false and (by default) 1 for true. All nonzero values are also considered true. 10 Now due Monday, March 14

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Simplifying Context-Free Grammars  We can convert a context-free grammar to an equivalent grammar that is somehow “simpler”.  An equivalent but simpler grammar may have more restrictions and is easier to work with.  Simpler does not necessarily mean fewer production rules. 11

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak λ -Free Grammars  We want to study context-free languages that do not contain the empty string λ. Let L be any context-free language. Let G = {V, T, S, P} be a context-free grammar for L – {λ} Create a new grammar by adding the new start symbol S 0 to V and the new rules The new grammar will generate L. Therefore, any nontrivial conclusions made for L – {λ} will also apply to L. 12

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak λ -Free Grammars, cont’d  For any context-free grammar G, we can construct a grammar such that  Unless otherwise specified, we will discuss only λ -free context-free languages. 13

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak A Substitution Rule  Let a context-free grammar G contain two different variables A and B.  Suppose G contains a production of the form and a production of the form  Then for each B in the right side of a production, we can substitute each of B ’s right sides: 14

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Remove Useless Productions  A variable of a grammar is useless if: It cannot be reached from the start variable, or It cannot derive a terminal string.  Example 1 : Variable A is useless because it cannot derive a terminal string. 15

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Remove Useless Productions, cont’d  Example 2 : Even though variable B can derive a terminal string … It’s useless because it cannot be reached from the starting variable S. 16

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Remove Useless Productions, cont’d  Example 3: C is useless since it cannot derive a terminal string. Draw a dependency graph to show that B is useless since it cannot be reached from S : Therefore: 17 Formal Languages and Automata, 5 th ed. Peter Linz Jones & Bartlett, 2012

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Remove λ Productions  In a context-free grammar, a λ -production is  Any variable A for which the derivation is possible is nullable.  To remove λ -productions from a grammar, add new productions where you replace all nullable variables in the right sides of productions with λ in every combination. 18

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Example Removal of λ Productions  Consider the productions  Variables A, B, and C are nullable. Replace each of them with λ in every combination. Example: Add to production A the rule with B replaced with λ and the rule with C replaced with λ : 19

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Example Removal of λ Productions, cont’d Similarly for production rule S, add rules where you replace A, B, and C in ABaC with λ in every combination:  Replace A with λ to get BaC  Replace B with λ to get AaC  Replace C with λ to get ABa  Replace both A and B with λ to get aC, etc. 20

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Remove Unit Productions  A unit production in a context-free grammar has the form where A and B are variables.  We also want to remove unit productions. 21

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Example Removal of Unit Productions  Consider the productions  Start with the non-unit productions 22

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Example Removal of Unit Productions, cont’d  Draw the dependency graph for the unit productions to add new rules:  The equivalent grammar: Note that B is now useless. 23 Formal Languages and Automata, 5 th ed. Peter Linz Jones & Bartlett, 2012

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Chomsky Normal Form  A context-free grammar where all productions are of the form or is in Chomsky normal form.  Example: is in Chomsky normal form.  But not:  Any context-free grammar G where has an equivalent grammar in Chomsky normal form. 24

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Example Conversion to Chomsky Normal Form  Convert to Chomsky normal form.  Introduce new variables for the terminals: 25

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Chomsky Normal Form Conversion, cont’d  Introduce more new variables as necessary: 26

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Greibach Normal Form  A context-free grammar is in Greibach normal form if all productions have the form where and  Similar to an s-grammar but without the restriction that the pair (A, a) occurs at most once.  Any context-free grammar G where has an equivalent grammar in Greibach normal form. 27

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Limitations of Finite Automata  Finite automata (FA) can recognize regular languages but not all context-free languages.  An FA has a strictly finite memory.  Example: is not regular because an FA cannot remember the count of a ’s.  Example: is not regular because an FA cannot recall in reverse order the symbols in w. 28

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Pushdown Automata  The machine class of pushdown automata (PDA) includes a stack for storage: 29 Formal Languages and Automata, 5 th ed. Peter Linz Jones & Bartlett, 2012

Computer Science Dept. Spring 2016: March 8 CS 154: Formal Languages and Computability © R. Mak Pushdown Automata, cont’d  Nondeterministic pushdown automata (NPDA) accepts exactly the family of context-free languages.  Deterministic pushdown automata (DPDA) are not equivalent to the nondeterministic ones. DPDA recognize a new family of deterministic context-free languages. Which are a proper subset of the context-free languages. 30