Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages 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.
Prof. Busch - LSU1 Simplifications of Context-Free Grammars.
CFGs and PDAs Sipser 2 (pages ). Last time…
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
1 … NPDAs continued. 2 Pushing Strings Input symbol Pop symbol Push string.
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
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.
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.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Specifying Languages CS 480/680 – Comparative Languages.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Prof. Busch - LSU1 Pushdown Automata PDAs. Prof. Busch - LSU2 Pushdown Automaton -- PDA Input String Stack States.
1 Pushdown Automata PDAs. 2 Pushdown Automaton -- PDA Input String Stack States.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2005Costas Busch - RPI1 Context-Free Languages.
Context-Free Languages Hinrich Schütze CIS, LMU, Slides based on RPI CSCI 2400 Thanks to Costas Busch.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
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.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Grammars CPSC 5135.
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”
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Overview of Previous Lesson(s) Over View  In our compiler model, the parser obtains a string of tokens from the lexical analyzer & verifies that the.
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.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
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.
1 Context Free Grammars Xiaoyin Wang CS 5363 Spring 2016.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Costas Busch - LSU1 PDAs Accept Context-Free Languages.
Theory of Languages and Automata By: Mojtaba Khezrian.
5. Context-Free Grammars and Languages
Context-Free Languages
G. Pullaiah College of Engineering and Technology
CS510 Compiler Lecture 4.
Syntax versus Semantics
CSE322 LEFT & RIGHT LINEAR REGULAR GRAMMAR
CS21 Decidability and Tractability
Parsing Costas Busch - LSU.
Context-Free Languages
Chapter 2 Context-Free Language - 01
Presentation transcript:

Prof. Busch - LSU1 Context-Free Languages

Prof. Busch - LSU2 Regular Languages Context-Free Languages

Prof. Busch - LSU3 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton

Prof. Busch - LSU4 Context-Free Grammars

Prof. Busch - LSU5 Grammars Grammars express languages Example: the English language grammar

Prof. Busch - LSU6

7 Derivation of string “the dog sleeps”:

Prof. Busch - LSU8 Derivation of string “a cat runs”:

Prof. Busch - LSU9 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” }

Prof. Busch - LSU10 Variables Sequence of Terminals (symbols) Productions Sequence of Variables

Prof. Busch - LSU11 Another Example Grammar: Variable Sequence of terminals and variables The right side may be

Prof. Busch - LSU12 Grammar: Derivation of string :

Prof. Busch - LSU13 Grammar: Derivation of string :

Prof. Busch - LSU14 Other derivations: Grammar:

Prof. Busch - LSU15 Grammar: Language of the grammar:

Prof. Busch - LSU16 We write: Instead of: for zero or more derivation steps A Convenient Notation

Prof. Busch - LSU17 in zero or more derivation steps In general we write: If: Trivially:

Prof. Busch - LSU18 Example Grammar Possible Derivations

Prof. Busch - LSU19 Another convenient notation:

Prof. Busch - LSU20 Formal Definitions Set of variables Set of terminal symbols Start variable Set of productions Grammar:

Prof. Busch - LSU21 Context-Free Grammar: All productions in are of the form String of variables and terminals Variable

Prof. Busch - LSU22 variables terminals productions start variable Example of Context-Free Grammar

Prof. Busch - LSU23 For a grammar with start variable String of terminals or Language of a Grammar:

Prof. Busch - LSU24 context-free grammar : Example: Since, there is derivation for any

Prof. Busch - LSU25 A language is context-free if there is a context-free grammar with Context-Free Language definition:

Prof. Busch - LSU26 since context-free grammar : Example: is a context-free language generates

Prof. Busch - LSU27 Context-free grammar : Example derivations: Palindromes of even length Another Example

Prof. Busch - LSU28 Context-free grammar : Example derivations: () ((( ))) (( )) Describes matched parentheses: Another Example

Prof. Busch - LSU29 Derivation Order and Derivation Trees

Prof. Busch - LSU30 Derivation Order Consider the following example grammar with 5 productions:

Prof. Busch - LSU31 Leftmost derivation order of string : At each step, we substitute the leftmost variable

Prof. Busch - LSU32 Rightmost derivation order of string : At each step, we substitute the rightmost variable

Prof. Busch - LSU33 Rightmost derivation of : Leftmost derivation of :

Prof. Busch - LSU34 Derivation Trees Consider the same example grammar: And a derivation of :

Prof. Busch - LSU35 yield

Prof. Busch - LSU36 yield

Prof. Busch - LSU37 yield

Prof. Busch - LSU38 yield

Prof. Busch - LSU39 yield Derivation Tree (parse tree)

Prof. Busch - LSU40 Give same derivation tree Sometimes, derivation order doesn’t matter Leftmost derivation: Rightmost derivation:

Prof. Busch - LSU41 Ambiguity

Prof. Busch - LSU42 Grammar for mathematical expressions Example strings: Denotes any number

Prof. Busch - LSU43 A leftmost derivation for

Prof. Busch - LSU44 Another leftmost derivation for

Prof. Busch - LSU45 Two derivation trees for

Prof. Busch - LSU46 take

Prof. Busch - LSU47 Good TreeBad Tree Compute expression result using the tree

Prof. Busch - LSU48 Two different derivation trees may cause problems in applications which use the derivation trees: Evaluating expressions In general, in compilers for programming languages

Prof. Busch - LSU49 Ambiguous Grammar: A context-free grammar is ambiguous if there is a string which has: two different derivation trees or two leftmost derivations (Two different derivation trees give two different leftmost derivations and vice-versa)

Prof. Busch - LSU50 stringhas two derivation trees this grammar is ambiguous since Example:

Prof. Busch - LSU51 stringhas two leftmost derivations this grammar is ambiguous also because

Prof. Busch - LSU52 IF_STMTif EXPR then STMT if EXPR then STMT else STMT Another ambiguous grammar: VariablesTerminals Very common piece of grammar in programming languages

Prof. Busch - LSU53 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2 Two derivation trees

Prof. Busch - LSU54 In general, ambiguity is bad and we want to remove it Sometimes it is possible to find a non-ambiguous grammar for a language But, in general ιt is difficult to achieve this

Prof. Busch - LSU55 Ambiguous Grammar Non-Ambiguous Grammar Equivalent generates the same language A successful example:

Prof. Busch - LSU56 Unique derivation tree for

Prof. Busch - LSU57 An un-successful example: every grammar that generates this language is ambiguous is inherently ambiguous:

Prof. Busch - LSU58 Example (ambiguous) grammar for :

Prof. Busch - LSU59 The string has always two different derivation trees (for any grammar) For example