Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22)

Similar documents
Homework. Context Free Languages. Before We Start. Announcements. Plan for today. Languages. Any questions? Recall. 1st half. 2nd half.

CSc 453 Compilers and Systems Software. 6 : Top-Down Parsing I

Definition of Regular Expression

CSE 401 Midterm Exam 11/5/10 Sample Solution

CS 321 Programming Languages and Compilers. Bottom Up Parsing

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing

Theory of Computation CSE 105

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or

ECE 468/573 Midterm 1 September 28, 2012

LING/C SC/PSYC 438/538. Lecture 21 Sandiway Fong

CMSC 331 First Midterm Exam

Compilers Spring 2013 PRACTICE Midterm Exam

CMPSC 470: Compiler Construction

Dr. D.M. Akbar Hussain

Problem Set 2 Fall 16 Due: Wednesday, September 21th, in class, before class begins.

Assignment 4. Due 09/18/17

Fig.25: the Role of LEX

CSCE 531, Spring 2017, Midterm Exam Answer Key

acronyms possibly used in this test: CFG :acontext free grammar CFSM :acharacteristic finite state machine DFA :adeterministic finite automata

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

ASTs, Regex, Parsing, and Pretty Printing

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

Context-Free Grammars

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016

Context-Free Grammars

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

CS 236 Language and Computation. Alphabet. Definition. I.2.1. Formal Languages (10.1)

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

Lecture T4: Pattern Matching

CS 430 Spring Mike Lam, Professor. Parsing

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

Midterm I Solutions CS164, Spring 2006

MATH 25 CLASS 5 NOTES, SEP

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015

MA1008. Calculus and Linear Algebra for Engineers. Course Notes for Section B. Stephen Wills. Department of Mathematics. University College Cork

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

2014 Haskell January Test Regular Expressions and Finite Automata

Lexical Analysis: Constructing a Scanner from Regular Expressions

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis

Regular Expression Matching with Multi-Strings and Intervals. Philip Bille Mikkel Thorup

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an

Eliminating left recursion grammar transformation. The transformed expression grammar

TO REGULAR EXPRESSIONS

Improper Integrals. October 4, 2017

Context-Free Grammars

Lexical analysis, scanners. Construction of a scanner

Unit 5 Vocabulary. A function is a special relationship where each input has a single output.

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

Section 3.1: Sequences and Series

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table

binary trees, expression trees

10.5 Graphing Quadratic Functions

CS201 Discussion 10 DRAWTREE + TRIES

Math 464 Fall 2012 Notes on Marginal and Conditional Densities October 18, 2012

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

Example: 2:1 Multiplexer

Unit #9 : Definite Integral Properties, Fundamental Theorem of Calculus

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

COMP 423 lecture 11 Jan. 28, 2008

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS 1 COMPUTATION & LOGIC INSTRUCTIONS TO CANDIDATES

9 Graph Cutting Procedures

UNIT 11. Query Optimization

Digital Design. Chapter 6: Optimizations and Tradeoffs

a(e, x) = x. Diagrammatically, this is encoded as the following commutative diagrams / X

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number>

Creating Flexible Interfaces. Friday, 24 April 2015

Section 10.4 Hyperbolas

Tree Structured Symmetrical Systems of Linear Equations and their Graphical Solution

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Midterm 2 Sample solution

4452 Mathematical Modeling Lecture 4: Lagrange Multipliers

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

Math 142, Exam 1 Information.

Before We Begin. Introduction to Spatial Domain Filtering. Introduction to Digital Image Processing. Overview (1): Administrative Details (1):

The Fundamental Theorem of Calculus

Pointwise convergence need not behave well with respect to standard properties such as continuity.

such that the S i cover S, or equivalently S

COMBINATORIAL PATTERN MATCHING

Ma/CS 6b Class 1: Graph Recap

Homework. Announcements. Before We Start. Languages. Plan for today. Chomsky Normal Form. Final Exam Dates have been announced

From Dependencies to Evaluation Strategies

Context-Free Languages and Parse Trees

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Ambiguous Grammars and Compactification

What are suffix trees?

Reducing Costs with Duck Typing. Structural

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

Topic 2: Lexing and Flexing

SIMPLIFYING ALGEBRA PASSPORT.

Suffix Tries. Slides adapted from the course by Ben Langmead

Transcription:

Homework Context Free Lnguges III Prse Trees nd Homework #5 (due 10/22) From textbook 6.4,b 6.5b 6.9b,c 6.13 6.22 Pln for tody Context Free Lnguges Next clss of lnguges in our quest! Lnguges Recll. Wht is lnguge? Wht is clss of lnguges? Context Free Lnguges Context Free Lnguges(CFL) is the next clss of lnguges outside of Regulr Lnguges: Mens for defining: Context Free Grmmr Mchine for ccepting: Pushdown Automt CFLs nd Regulr Lnguges Venn digrm of lnguges Context Free Lnguges Regulr Lnguges Finite Lnguges 1

Context Free Grmmrs Let s formlize this bit: A context free grmmr (CFG) is 4-tuple: (V, Σ,, P) where V is set of vribles Σ is set of terminls V nd Σ re disjoint (I.e. V Σ= ) V, is your strt symbol Context Free Grmmrs Let s formlize this bit: Production rules Of the form A βwhere A V β (V ) * string with symbols from V nd We sy tht γ cn be derived from α in one step: A βis rule α = α 1 A α 2 γ = α 1 βα 2 α γ Pln for tody Ambiguous Grmmrs nd Prse Trees Questions? Prse Trees Grphicl mens to illustrte derivtion of string from grmmr Root of the tree = strt vrible Interior nodes = other vribles Children of nodes = ppliction of production rule Lef nodes = Terminl symbols Another exmple Find CFG to describe: L = { i b j c k i = k} B (1) c (2) B bb (3) B Λ (4) Cn lso write s B c B bb Λ Another exmple Let s derive string from L: bbcc c rule 2 cc rule 2 Bcc rule 1 bbcc rule 3 bbbcc rule 3 bbλcc rule 4 = bbcc 2

An inorder trversl of the tree will give the the string derived. Prse Tree c c B b b B B Λ Rule 2 Rule 2 Rule 1 Rule 3 Rule 3 Rule 4 Recll our exmple from lst time Defining the grmmr for lgebric expressions Production rules + (1) (2) * (3) / (4) () (5) (6) One more exmple Prse Tree how derivtion for + * + rule 1 + + * rule 3 + * + * + * + rule 1 + + * rule 3 + * + * One more exmple Prse Tree Another derivtion for + * * rule 3 * + * rule 1 + * + * * + * rule 3 * + * rule 1 + * + * 3

Prse trees + * * + A CFG is sid to be mbiguous if there is t lest 1 string in L(G) hving two or more distinct derivtions. me string, 2 derivtions Fmous progrmming lnguge mbiguity Dngling else <> if (<expr>) <> if (<expr>) <> else <> <some_other_> if (expr1) if (expr2) f(); else g(); Fmous progrmming lnguge mbiguity if ( expr ) else expr1 if ( expr ) g(x); To which if does the else belong? expr2 f(x); In this derivtion, the else belongs to the 1 st if Fmous progrmming lnguge mbiguity Fmous progrmming lnguge mbiguity if ( expr ) expr1 A wy to fix this <> <s1> <s2> <s1> if (<expr>) <s1> else <s1> <other> <s2> if (<expr>) <> if (<expr>) <s1> else <s2> if ( expr ) expr2 f(x); else g(x); <s1> represents if sttements with mtching else <s2> represent if sttements with t lest 1 unmtched if The <s1> in the rule for <s2> will ssure tht ll sttements between if nd else will not hve dngling else. 4

Fmous progrmming lnguge mbiguity s2 if ( expr ) s1 expr1 if ( expr ) s1 expr2 f(x); else s1 g(x); ome lnguges re inherently mbiguous All possible grmmrs tht generte the lnguge re mbiguous Unfortuntely, there is no lgorithm tht cn tells us whether grmmr is mbiguous or not. howing grmmr is mbiguous is esy Find string x in the L(G) tht hs two derivtions howing prticulr grmmr is not mbiguous is usully difficult. howing tht ny grmmr is not mbiguous is not possible. Derivtions Leftmost derivtions A leftmost derivtion is one where the leftmost vrible in the current string is lwys the first to get replced vi production rule. A rightmost derivtion is one where the rightmost vrible in the current string is lwys the first to get replced vi production rule. Derivtions + + + + + + As it turns out (we won t prove this) In unmbiguous grmmrs,leftmost derivtions will lwys be unique. In unmbiguous grmmrs,rightmost derivtions will lwys be unique. rightmost derivtion leftmost derivtion 5

Removing mbiguities ince some lnguges re inherently mbiguous This cnnot lwys be done However, On cse by cse bsis, mbiguities cn be eliminted Exmple Abbrevited grmmr for lgebric expressions Production rules + (1) * (2) () (3) (4) Exmple This grmmr hs two problems 1. Precedence of opertors is not respected * + should be interpreted s (*) + 2. equence of identicl opertors cn be grouped either from the left or the right + + cn be interpreted s either (+)+ or + ( + ) Exmple olution Introduce some new vribles Fctor expression tht cnnot be broken up by either * or + () Term expression tht cnnot be broken up by + All Fctors T * F Expression ll possible expression All Terms + T Exmple Our new grmmr + T T T T * F F F () Note tht ll recursion is leftmost * hs higher precedent thn + + + + * is interpreted s ((+) + ) + (*) Exmple + + + * + T + T T * F + T F F F 6

Exmple It cn be shown Tht every string x, tht is generted by this new grmmr, hs only one leftmost derivtion As such this new grmmr is unmbiguous Done using induction on the x. ummry A grmmr is mbiguous if there is string generted by the grmmr tht hs two distinct derivtions. ome lnguges re inherently mbiguous All grmmrs tht generte the lnguge re mbiguous There is no lgorithm to determine if ny given grmmr is mbiguous Proving grmmr to be mbiguous is esy Proving tht grmmr is not is hrd. Questions? 7