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

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

MA513: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 18 Date: September 12, 2011

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Ambiguous Grammars and Compactification

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

Context-Free Grammars

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

Context-Free Languages and Parse Trees

Context-Free Grammars and Languages (2015/11)

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

JNTUWORLD. Code No: R

Syntax Analysis Part I

CT32 COMPUTER NETWORKS DEC 2015

Glynda, the good witch of the North

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

CMSC 330: Organization of Programming Languages

Plan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

CMSC 330: Organization of Programming Languages

Lecture 8: Context Free Grammars

CS525 Winter 2012 \ Class Assignment #2 Preparation

Decision Properties for Context-free Languages

The Turing Machine. Unsolvable Problems. Undecidability. The Church-Turing Thesis (1936) Decision Problem. Decision Problems

UNIT I PART A PART B

COP 3402 Systems Software Syntax Analysis (Parser)

Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

CMSC 330: Organization of Programming Languages. Context Free Grammars

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

Syntax Analysis Check syntax and construct abstract syntax tree

CMSC 330: Organization of Programming Languages

DVA337 HT17 - LECTURE 4. Languages and regular expressions

Introduction to Syntax Analysis

Context-Free Grammars

Chapter 18: Decidability

(a) R=01[((10)*+111)*+0]*1 (b) ((01+10)*00)*. [8+8] 4. (a) Find the left most and right most derivations for the word abba in the grammar

Models of Computation II: Grammars and Pushdown Automata

Multiple Choice Questions

Introduction to Syntax Analysis. The Second Phase of Front-End

Limits of Computation p.1/?? Limits of Computation p.2/??

Principles of Programming Languages COMP251: Syntax and Grammars

Lecture 11: Context-free grammars

Definition: two derivations are similar if one of them precedes the other.

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

Compiler Design Concepts. Syntax Analysis

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

CS 314 Principles of Programming Languages

CMSC 330, Fall 2009, Practice Problem 3 Solutions

Context Free Languages

Context Free Languages and Pushdown Automata

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

Normal Forms for CFG s. Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form

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

Skyup's Media. PART-B 2) Construct a Mealy machine which is equivalent to the Moore machine given in table.

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

Formal Languages. Grammar. Ryan Stansifer. Department of Computer Sciences Florida Institute of Technology Melbourne, Florida USA 32901

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

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

Parsing. For a given CFG G, parsing a string w is to check if w L(G) and, if it is, to find a sequence of production rules which derive w.

Announcements. Written Assignment 1 out, due Friday, July 6th at 5PM.

CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

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

Introduction to Parsing. Lecture 8

Context-Free Grammars

EECS 6083 Intro to Parsing Context Free Grammars

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

CS 44 Exam #2 February 14, 2001

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

Compiler Construction

Context Free Grammars, Parsing, and Amibuity. CS154 Chris Pollett Feb 28, 2007.

Compilers Course Lecture 4: Context Free Grammars

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl


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

Introduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

CONTEXT FREE GRAMMAR. presented by Mahender reddy

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

Lexical and Syntax Analysis

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

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

Context-Free Grammars

Final Course Review. Reading: Chapters 1-9

announcements CSE 311: Foundations of Computing review: regular expressions review: languages---sets of strings

AUBER (Models of Computation, Languages and Automata) EXERCISES

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Lexical and Syntax Analysis. Top-Down Parsing

Introduction to Parsing. Lecture 5

Outline. Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

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

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Optimizing Finite Automata

Transcription:

Homework Context Free Languages Homework #2 returned Homework #3 due today Homework #4 Pg 133 -- Exercise 1 (use structural induction) Pg 133 -- Exercise 3 Pg 134 -- Exercise 8b,c,d Pg 135 -- Exercise 22 Pg 145 -- Exercise 13 Due 10 / 7 Announcements Exam 1 Next class (Oct 2 nd ) 1 hour Cover Regular Languages (up to and including today s lecture) Closed book (1 sheet study guide okay) Before We tart Any questions? Remainder of next class will be a problem session Plan for today 1st half Context Free Grammars and Languages 2nd half Regular Languages and Context Free Languages / L-ystems Languages Recall. What is a language? What is a class of languages? 1

Regular Languages For the past several weeks, we have been looking at Regular Languages: language: Regular Expression Machine for accepting: Finite Automata Last class we discovered Venn-diagram of languages Is there something out here? Regular Languages YE! Finite Languages Context Free Languages Context Free Languages(CFL) is the next class of languages outside of Regular Languages: Language / grammar: Context Free Grammar Machine for accepting: Pushdown Automata Grammars Wikipedia says: languages can be described as a system of symbols and the grammars (rules) by which the symbols are manipulated Grammar is the study of rules governing the use of language. Grammars Grammars Let s redefine grammars for C Theory use: 1. Terminals = et of symbols that form the strings of the language being defined 2. Variables = et of symbols representing categories 3. tart ymbol = variable that represents the base category that defines our language 4. Production rules = set of rules that recursively define the language Let s formalize this a bit: A grammar is a 4-tuple: (V, T, P, ) where V is a set of variables T is a set of terminals P is a set of production rules V and T are disjoint (I.e. V T = ) V, is your start symbol 2

General Grammars Production Rules Of the form A B A is a string of terminals and variables B is a string of terminals and variables To apply a rule, replace any occurrence of A with the string B. Grammars Let s formalize this a bit: Production rules We say that γ can be derived from α in one step: A β is a rule α = α 1 A α 2 γ = α 1 β α 2 α γ We write α * γ if γ can be derived from α in zero or more steps. Context Free Grammars Production Rules Of the form A B A is a variable B is a string, combining terminals and variables To apply a rule, replace an occurrence of A with the string B. We say that the grammar is context-free since this substitution can take place regardless of where A is. Context Free Grammars The language generated by a grammar Let G = (V, T, P, ) The language generated by G, L(G) L(G) = { x T * * x} A language L is a Context Free Language (CFL) iff there is a CFG G, such that L = L(G) Recall our friend: Palindromes A palindrome is a string that is the same read left to right or right to left First half of a palindrome is a mirror image of the second half s: a, b, aba, abba, babbab. Recursive definition for palindromes (pal) over Σ 1. λ pal 2. For any a Σ, a pal 3. For any x pal and a Σ, axa pal 4. No string is in pal unless it can be obtained by rules 1-3 3

A CFG for palindromes over {a,b} Base cases 1. P λ 2. P a 3. P b Recursion 4. P apa 5. P bpb Back to C Theory Building the palindrome abba using grammar P apa (Rule 4) P abpba (Rule 5) P ab λ ba (Rule 1) P abba Another Find a CFG to describe: L = {x {0,1} * n 0 (x) = n 1 (x)} Basic idea (define recursively) λ is certainly in the language For all strings in the language, if we add a 0 and 1 to the string, the result is in the language. The concatenation of any two strings in the language will also be in the language Another Find a CFG to describe: L = {x {0,1} * n 0 (x) = n 1 (x)} λ (1) 01 (2) 10 (3) (4) Another Let s derive a string from L 00110011 rule 4 01 01 rule 2 0011 0011 rule 2 00 λ 11 00 λ 11 rule 1 = 00110011 Yet Another example Find a CFG to describe: L = {a i b j c k i = k} Number of a s equals the number of c s with any number of b s between them Use variable B to represent b j Every time you add a to the left of B you need to add c to the right. 4

Yet Another example Find a CFG to describe: L = {a i b j c k i = k} B (1) ac (2) B bb (3) B λ (4) Can also write as B ac B bb λ Another example Let s derive a string from L: aabbbcc ac rule 2 aacc rule 2 aabcc rule 1 aabbcc rule 3 aabbbcc rule 3 aabbbbcc rule 3 aabbb λ cc rule 4 = aabbbcc Defining the grammar for algebraic expressions: Let a = a numeric constant et of binary operators = {+, -, *, /} Expressions can be parenthesized Defining the grammar for algebraic expressions: G = (V, T, P, ) V = {} T = { a, -, +, *, /, (, ) } = P = see next slide Defining the grammar for algebraic expressions Production rules + (1) (2) * (3) / (4) () (5) a (6) how derivation for a + (a * a) / a + rule 1 a + a + / rule 4 a + () / rule 5 a + ( * ) / rule 3 a + (a * ) / a + (a * a) / a + (a * a) / a 5

Parse Trees Graphical means to illustrate a derivation of a string from a grammar Root of the tree = start variable Interior nodes = other variables Children of nodes = application of a production rule Leaf nodes = Terminal symbols Another example Find a CFG to describe: L = {a i b j c k i = k} B (1) ac (2) B bb (3) B λ (4) Can also write as B ac B bb λ Another example Let s derive a string from L: aabbcc ac rule 2 aacc rule 2 aabcc rule 1 aabbcc rule 3 aabbbcc rule 3 aabb λ cc rule 4 = aabbcc Parse Tree An inorder traversal of the tree will give the the string derived. Rule 2 a c a c Rule 2 B Rule 1 b B Rule 3 b B Rule 3 λ Rule 4 Recall our example from last time Defining the grammar for algebraic expressions Production rules + (1) (2) * (3) / (4) () (5) a (6) how derivation for a + a * a + rule 1 a + a + * rule 3 a + a * a + a * a 6

Parse Tree + a * + rule 1 a + a + * rule 3 a + a * a + a * a Another derivation for a + a * a * rule 3 * a + * a rule 1 a + * a a + a * a Parse Tree Parse trees * + a * rule 3 * a + * a rule 1 a + * a a + a * a + a * ame string, 2 derivations * + a Ambiguity A CFG is said to be ambiguous if there is at least 1 string in L(G) having two or more distinct derivations. ome grammars are inherently ambiguous. Ambiguity howing a grammar is ambiguous is easy Find a string x in the L(G) that has two derivations howing a particular grammar is not ambiguous is usually difficult. howing that any grammar is not ambiguous is not possible. 7

Derivations Leftmost derivations A leftmost derivation is one where the leftmost variable in the current string is always the first to get replaced vi production rule. A rightmost derivation is one where the rightmost variable in the current string is always the first to get replaced vi production rule. Derivations a + a + a + a + rightmost derivation + + a leftmost derivation Ambiguity Removing ambiguities As it turns out (we won t prove this) In unambiguous grammars,leftmost derivations will always be unique. In unambiguous grammars,rightmost derivations will always be unique. ome languages are inherently ambiguous This cannot always be done In fact, We can/will show there is no algorithm for determining if a CFG is ambiguous However, On a case by case basis, ambiguities can be eliminated Abbreviated grammar for algebraic expressions Production rules + (1) * (2) () (3) a (4) This grammar has two problems 1. Precedence of operators is not respected a * a + a should be interpreted as (a*a) + a 2. equence of identical operators can be grouped either from the left or the right a + a + a can be interpreted as either (a+a)+a or a + (a + a) 8

olution Introduce some new variables Factor expression that cannot be broken up by either * or + a () Term expression that cannot be broken up by + All Factors T * F Expression all possible expression All Terms + T Our new grammar + T T T T * F F F () a Note that all recursion is leftmost * has higher precedent than + a + a + a + a * a is interpreted as ((a+a) + a) + (a*a) a + a + a + a * a + T + T T * F + T F F a a F a It can be shown That every string x, that is generated by this new grammar, has only one leftmost derivation As such this new grammar is unambiguous Done using induction on the x. Reality Check Context Free Grammars (CFG) Context Free Languages are generated from CFGs Parse Trees and Derviation Ambiguity About Ambiguity Ambiguity A grammar is ambiguous if there is a string generated by the grammar that has two distinct derivations. ome languages are inherently ambiguous All grammars that generate the language are ambiguous There is no algorithm to determine if any given grammar is ambiguous Proving a grammar to be ambiguous is easy Proving that a grammar is not is hard. 9

Questions? Break. 10