Complementing Non-CFLs If A and B are context free languages then: AR is a context-free language TRUE

Similar documents
Menu. Problem 5: PRIMES. Problem 5: PRIMES. Please read the PS2 Comments thoroughly! cs3102: Theory of Computation

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS 44 Exam #2 February 14, 2001

UNIVERSITY OF CALIFORNIA

Final Course Review. Reading: Chapters 1-9

Problems, Languages, Machines, Computability, Complexity

CMPT 755 Compilers. Anoop Sarkar.

Semantic Analysis. Lecture 9. February 7, 2018

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

BSCS Fall Mid Term Examination December 2012

CSCI 1260: Compilers and Program Analysis Steven Reiss Fall Lecture 4: Syntax Analysis I

Compiler Construction Using

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Lecture 8: Deterministic Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Theory of Programming Languages COMP360

CS2210: Compiler Construction Syntax Analysis Syntax Analysis

CS525 Winter 2012 \ Class Assignment #2 Preparation

Languages and Automata

CS154 Midterm Examination. May 4, 2010, 2:15-3:30PM

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Yet More CFLs; Turing Machines. CS154 Chris Pollett Mar 8, 2006.

Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

1. [5 points each] True or False. If the question is currently open, write O or Open.

Midterm I (Solutions) CS164, Spring 2002

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

CS502: Compilers & Programming Systems

COL728 Minor1 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

CMSC 330 Practice Problem 4 Solutions

Decision Properties for Context-free Languages

Closure Properties of CFLs; Introducing TMs. CS154 Chris Pollett Apr 9, 2007.

CSCI312 Principles of Programming Languages!

CT32 COMPUTER NETWORKS DEC 2015

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

EDA180: Compiler Construc6on. Top- down parsing. Görel Hedin Revised: a

Lecture 4: Syntax Specification

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

CS164: Midterm I. Fall 2003

Syntax Analysis Part I

CSE431 Translation of Computer Languages

Midterm I - Solution CS164, Spring 2014

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

Conflicts in LR Parsing and More LR Parsing Types

LL(k) Compiler Construction. Top-down Parsing. LL(1) parsing engine. LL engine ID, $ S 0 E 1 T 2 3

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

Specifying Syntax COMP360

Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Context Free Grammars. CS154 Chris Pollett Mar 1, 2006.

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Ambiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int

PS3 - Comments. Describe precisely the language accepted by this nondeterministic PDA.

CS Parsing 1

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Ambiguous Grammars and Compactification

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Lexical Analysis. Lecture 2-4

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Multiple Choice Questions

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

T.E. (Computer Engineering) (Semester I) Examination, 2013 THEORY OF COMPUTATION (2008 Course)

CMSC330 Spring 2017 Midterm 2

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

LL(k) Compiler Construction. Choice points in EBNF grammar. Left recursive grammar

CS 314 Principles of Programming Languages

UNIT I PART A PART B

COMP3131/9102: Programming Languages and Compilers

A simple syntax-directed

Introduction to Parsing. Lecture 5

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

Top down vs. bottom up parsing

Announcements. Application of Recursion. Motivation. A Grammar. A Recursive Grammar. Grammars and Parsing

Wednesday, August 31, Parsers

Programs as data Parsing cont d; first-order functional language, type checking

Theory Bridge Exam Example Questions Version of June 6, 2008

Question Points Score

LECTURE 3. Compiler Phases

CMSC 330, Fall 2009, Practice Problem 3 Solutions

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

Compiler Design Concepts. Syntax Analysis

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Context-Free Grammars

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Extra Credit Question

Lecture Notes on Bottom-Up LR Parsing

CSE 582 Autumn 2002 Exam 11/26/02

Transcription:

Lecture : Parsimonious Menu Fix proof from last class Interpretive Dance! Parsimonious (Parsimoniously) P Comments Available oday P will be returned uesday cs0: heory of Computation University of Virginia Computer cience David Evans http://w.cs.virginia.edu/evans Closure Properties of CFLs Complementing Non-CFLs If A and B are context free languages then: AR is a context-free language RUE A* L = { w Σ* is not a CFL. Is its complement? is a context-free language RUE A is a context-free language (complement)? A B is a context-free language RUE A B is a context-free language? CFG for L (L ) All odd length strings are in L Odd Even Even XY YX X ZXZ 0 Y ZYZ Z 0 Odd 0R R 0 R 0Odd Odd How can we prove this is correct? 5 What is the actual language? Yes. his CFG recognizes is: 00 0X X0 X 0X0 X 0X X0 0 ε Bogus Proof! 0X 0X0 00 L 4 odd generates all odd-length strings Odd 0R R 0 R 0Odd Odd Proof by induction on the length of the string. Basis. Odd generates all odd-length strings of length. here are two possible strings: 0 and. hey are produces from the rd and 4th rules. Induction. Assume Odd generates all odd-length strings of length n for n = k+, k 0. how it can generate all odd-length string of length n+. 6

Odd generates all odd-length strings Odd 0R R 0 R 0 Odd Odd Induction. Assume Odd generates all odd-length strings of length n for n = k+, k 0. how it can generate all odd-length string of length n+. All n+ length strings are of the form abt where t is an n- length string and a {0,, b {0,. here is some derivation from Odd * t (by the induction hypothesis). We can generate all four possibilities for a and b: 00t: Odd 0R 00 Odd * 00t 0t: Odd 0R 0 Odd * 0t 0t: Odd R 0 Odd * 0t t: Odd R Odd * 0t CFG for L (L ) Odd Even Even XY YX Odd 0R R 0 X ZXZ 0 R 0 Odd Odd Y ZYZ Z 0? Proof-by-leaving-as- Challenge Problem (note: you cannot use this proof technique in your answers) 7 8 Even trings how Even generates the set of all even-length strings that are not in L. Even XY YX X ZXZ 0 Y ZYZ Z 0 Proof by induction on the length of the string. Basis. Even generates all even-length strings of length 0 that are not in L. he only length 0 string is ε. ε is in L since ε = εε, so ε should not be generated by Even. ince Even does not contain any right sides that go to ε, this is correct. Closure Properties of CFLs If A and B are context free languages then: A R is a context-free language RUE A * is a context-free language RUE A is not necessarily a context-free language (complement) A B is a context-free language RUE A B is a context-free language? Left for you to solve (possibly on Exam ) 9 0 Where is English? 0 n n n 0 n n A English he cat likes fish. he cat the dog chased likes fish. he cat the dog the rat bit chased likes fish. his is a pumping lemma proof!

= DFA = CFG Chomsky s Answer (yntactic tructures, 957) Current Answer Most linguists argue that most natural languages are not contextfree But, it is hard to really answer this question: e.g., he cat the dog the rat bit chased likes fish. English? 4 Where is Java? 0 n n n 0 n n A Interpretive Dance 5 6 Where is Java? 0 n n n 0 n n A What is the Java Language? public class est { public static void main(tring [] a) { println("hello World!"); est.java:: cannot resolve symbol symbol : method println (java.lang.tring) // C:\users\luser\est.java public class est { public static void main(tring [] a) { println ("Hello Universe!"); In the Java Language est.java:: illegal unicode escape // C:\users\luser\est.java Not in the Java Language 7 8

// C:\users\luser\est.java public class est { public static void main(tring [] a) { println ("Hello Universe!"); canning error errors tatic semantic errors > javac est.java est.java:: illegal unicode escape // C:\users\luser\est.java est.java:6: 'class' or 'interface' expected est.java:7: 'class' or 'interface' expected est.java:4: cannot resolve symbol symbol : method println (java.lang.tring) location: class est println ("Hello World"); 4 errors Defining the Java Language { w w can be generated by the CFG for Java in the Java Language pecification { w a correct Java compiler can build a parse tree for w 9 0 +M M () number Programming languages are (should be) designed to make parsing easy, efficient, and unambiguous. Derivation + M M M * + * Unambiguous + * () number + * + * + * + * Ambiguity How can one determine if a CFG is ambiguous? uper-duper-challenge problem: create a program that solve the is this CFG ambiguous problem: Input: CFG Output: Yes (ambiguous)/ No (unambiguous) Warning: Undecidable Problem Alert! (Not only can you not do this, it is impossible for any program to do this.) (We will cover undecidable problems after pring Break) +M M () number Programming languages are (should be) designed to make parsing easy, efficient, and unambiguous. Derivation + M M M * + * 4 4

Easy and Efficient Easy - we can automate the process of building a parser from a description of a grammar Efficient the resulting parser can build a parse tree quickly (linear time in the length of the input) Recursive Descent Parse() { (); () { try { (); expect( + ); M(); catch { backup(); try { M(); catch {backup(); error(); M() { try { M(); expect( * ); (); catch try { (); catch { backup(); error (); () { try { expect( ( ); (); expect( ) ); catch ; try { number(); catch ; +M M () number Advantages: Easy to produce and understand Can be done for any CFG Problems: Inefficient (might not even finish) Nondeterministic 5 6 LL(k) (Lookahead-Left) A CFG is an LL(k) grammar if it can be parser deterministically with tokens lookahead +M M () number LL() grammar + + M M + M Look-ahead Parser Parse() { (); () { if (lookahead(, + )) { (); eat( + ); M(); else { M(); M() { if (lookahead(, * )) { M(); eat( * ); (); else { (); () { if (lookahead(0, ( )) { eat( ( ); (); eat( ) ); else { number(); +M M () number 7 8 JavaCC https://javacc.dev.java.net/ Input: Grammar specification Output: A Java program that is a recursive descent parser for the specified grammar Doesn t work for all CFGs: only for LL(k) grammars cheme Language Classes Java Python 0 n n 0 n n n LL(k) Languages Described by LL(k) Grammar 9 0 5

Next Week Monday (): Office Hours (Qi Mi in 6D) Monday (5:0): A help session uesday s class (Pieter Hooimeyer): starting to get outside the yellow circle: using grammars to solve security problems Wednesday (9:0am): Office Hours (Qi Mi in 6D) Wednesday (6pm): As Exam Review hursday: exam in class 6