Fall 2005Costas Busch - RPI1 Context-Free Languages.

Slides:



Advertisements
Similar presentations
Context-Free Languages
Advertisements

CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
CFGs and PDAs Sipser 2 (pages ). Last time…
CS5371 Theory of Computation
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Costas Busch - RPI1 Grammars. Costas Busch - RPI2 Grammars Grammars express languages Example: the English language.
Courtesy Costas Busch - RPI
1 Grammars. 2 Grammars express languages Example: the English language.
Fall 2006Costas Busch - RPI1 Pushdown Automata PDAs.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
Courtesy Costas Busch - RPI1 Context-Free Languages.
Costas Busch - RPI1 The Pumping Lemma for Context-Free Languages.
1 Reverse of a Regular Language. 2 Theorem: The reverse of a regular language is a regular language Proof idea: Construct NFA that accepts : invert the.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2006Costas Busch - RPI1 The Chomsky Hierarchy.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
Fall 2006Costas Busch - RPI1 PDAs Accept Context-Free Languages.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
Fall 2005Costas Busch - RPI1 Pushdown Automata PDAs.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Context-Free Languages Hinrich Schütze CIS, LMU, Slides based on RPI CSCI 2400 Thanks to Costas Busch.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
Grammars CPSC 5135.
Context-free Languages
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CS 3240: Languages and Computation Context-Free Languages.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Context Free Grammars 1. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Context-Free Languages. Regular Languages Context-Free Languages.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Syntax Analyzer (Parser)
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
1 Context Free Grammars Xiaoyin Wang CS 5363 Spring 2016.
Lecture 15 Ambiguous Grammars Topics: Context Free Grammars Language generated by a grammar Proofs with L(G) Ambiguous grammars October 20, 2008 CSCE 355.
Theory of Languages and Automata By: Mojtaba Khezrian.
5. Context-Free Grammars and Languages
CONTEXT-FREE LANGUAGES
Context-Free Languages
Formal Language & Automata Theory
G. Pullaiah College of Engineering and Technology
Fall Compiler Principles Context-free Grammars Refresher
NPDAs Accept Context-Free Languages
Syntax versus Semantics
NPDAs Accept Context-Free Languages
CSE322 LEFT & RIGHT LINEAR REGULAR GRAMMAR
5. Context-Free Grammars and Languages
The Post Correspondence Problem
Context-Free Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Fall Compiler Principles Context-free Grammars Refresher
Presentation transcript:

Fall 2005Costas Busch - RPI1 Context-Free Languages

Fall 2005Costas Busch - RPI2 Regular Languages

Fall 2005Costas Busch - RPI3 Regular Languages Context-Free Languages

Fall 2005Costas Busch - RPI4 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton

Fall 2005Costas Busch - RPI5 Context-Free Grammars

Fall 2005Costas Busch - RPI6 Grammars Grammars express languages Example: the English language

Fall 2005Costas Busch - RPI7

Fall 2005Costas Busch - RPI8 A derivation of “the dog walks”:

Fall 2005Costas Busch - RPI9 A derivation of “a cat runs”:

Fall 2005Costas Busch - RPI10 Language of the grammar: L = { “a cat runs”, “a cat sleeps”, “the cat runs”, “the cat sleeps”, “a dog runs”, “a dog sleeps”, “the dog runs”, “the dog sleeps” }

Fall 2005Costas Busch - RPI11 Notation VariableTerminal Production Rules

Fall 2005Costas Busch - RPI12 Another Example Grammar: Derivation of sentence :

Fall 2005Costas Busch - RPI13 Grammar: Derivation of sentence :

Fall 2005Costas Busch - RPI14 Other derivations: Grammar:

Fall 2005Costas Busch - RPI15 Language of the grammar

Fall 2005Costas Busch - RPI16 More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules

Fall 2005Costas Busch - RPI17 Example Grammar :

Fall 2005Costas Busch - RPI18 More Notation Sentential Form: A sentence that contains variables and terminals Example: Sentential Formssentence

Fall 2005Costas Busch - RPI19 We write: Instead of:

Fall 2005Costas Busch - RPI20 In zero or more steps In general we write: If: Trivially:

Fall 2005Costas Busch - RPI21 Example Grammar Possible Derivations

Fall 2005Costas Busch - RPI22 Another Grammar Example Grammar : Derivations:

Fall 2005Costas Busch - RPI23 More Derivations Grammar

Fall 2005Costas Busch - RPI24 Language of a Grammar For a grammar with start variable : String of terminals

Fall 2005Costas Busch - RPI25 Example For grammar : Since:

Fall 2005Costas Busch - RPI26 A Convenient Notation

Fall 2005Costas Busch - RPI27 Example Context-free grammar : Possible Derivations: (((( )))) Describes parentheses:

Fall 2005Costas Busch - RPI28 Context-free grammar : Derivations: Example Palindromes of even length

Fall 2005Costas Busch - RPI29 A context-free grammar : Derivations: Example () ((( ))) (( )) Describes matched parentheses:

Fall 2005Costas Busch - RPI30 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable

Fall 2005Costas Busch - RPI31 The language of the grammar is:

Fall 2005Costas Busch - RPI32 Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with

Fall 2005Costas Busch - RPI33 Derivation Order and Derivation Trees

Fall 2005Costas Busch - RPI34 Derivation Order Leftmost derivation: Rightmost derivation:

Fall 2005Costas Busch - RPI35 Leftmost derivation: Rightmost derivation:

Fall 2005Costas Busch - RPI36 Derivation Trees

Fall 2005Costas Busch - RPI37

Fall 2005Costas Busch - RPI38

Fall 2005Costas Busch - RPI39

Fall 2005Costas Busch - RPI40 Derivation Tree

Fall 2005Costas Busch - RPI41 yield Derivation Tree

Fall 2005Costas Busch - RPI42 Partial Derivation Trees Partial derivation tree

Fall 2005Costas Busch - RPI43 Partial derivation tree

Fall 2005Costas Busch - RPI44 Partial derivation tree sentential form yield

Fall 2005Costas Busch - RPI45 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:

Fall 2005Costas Busch - RPI46 Ambiguity

Fall 2005Costas Busch - RPI47 leftmost derivation

Fall 2005Costas Busch - RPI48 leftmost derivation

Fall 2005Costas Busch - RPI49 Two derivation trees

Fall 2005Costas Busch - RPI50 The grammar is ambiguous: stringhas two derivation trees

Fall 2005Costas Busch - RPI51 stringhas two leftmost derivations The grammar is ambiguous:

Fall 2005Costas Busch - RPI52 Definition: A context-free grammar is ambiguous if some string has: two different derivation trees OR two leftmost derivations

Fall 2005Costas Busch - RPI53 Why do we care about ambiguity? take

Fall 2005Costas Busch - RPI54

Fall 2005Costas Busch - RPI55 Good derivation Tree Bad derivation Tree

Fall 2005Costas Busch - RPI56 We want to remove ambiguity Ambiguity is bad for programming languages

Fall 2005Costas Busch - RPI57 We fix the ambiguous grammar: New unambiguous grammar:

Fall 2005Costas Busch - RPI58 Unique derivation tree

Fall 2005Costas Busch - RPI59 Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT

Fall 2005Costas Busch - RPI60 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2

Fall 2005Costas Busch - RPI61 Inherent Ambiguity Some context free languages have only ambiguous grammars Example:

Fall 2005Costas Busch - RPI62 The string has always two derivation trees (for any grammar) For example