Context-Free Languages & Grammars (CFLs & CFGs)

Slides:



Advertisements
Similar presentations
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Advertisements

CS5371 Theory of Computation
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Specifying Languages CS 480/680 – Comparative Languages.
1 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
EECS 6083 Intro to Parsing Context Free Grammars
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.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Lecture # 5 Pumping Lemma & Grammar
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
CONTEXT FREE GRAMMAR presented by Mahender reddy.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
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 CS 130: Theory of Computation HMU textbook, Chap 5.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Spring
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Autumn 2012 CSE
CSE 311 Foundations of Computing I Lecture 20 Context-Free Grammars and Languages Autumn 2012 CSE
Context Free Grammars & Parsing CPSC 388 Fall 2001 Ellen Walker Hiram College.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
5. Context-Free Grammars and Languages
Chapter 3 – Describing Syntax
FORMAL LANGUAGES & AUTOMATA THEORY
Closed book, closed notes
Context-Free Languages & Grammars (CFLs & CFGs)
CS 404 Introduction to Compiler Design
G. Pullaiah College of Engineering and Technology
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
CSE 105 theory of computation
Lecture 22 Pumping Lemma for Context Free Languages
Context free grammar.
Pushdown Automata (PDA). Part 2
PARSE TREES.
Course 2 Introduction to Formal Languages and Automata Theory (part 2)
Context-Free Grammars
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Context-free Languages
5. Context-Free Grammars and Languages
Lecture 7: Introduction to Parsing (Syntax Analysis)
CHAPTER 2 Context-Free Languages
CSE 311: Foundations of Computing
فصل دوم Context-Free Languages
Lecture 4: Lexical Analysis & Chomsky Hierarchy
The Pumping Lemma for CFL’s
CSE 105 theory of computation
CS21 Decidability and Tractability
Theory of Computation Lecture #
Key Answers for Homework #7
CSE 105 theory of computation
High-Level Programming Language
The Pumping Lemma for CFL’s
CSE 105 theory of computation
The Pumping Lemma for CFL’s
Intro to Theory of Computation
CSE 105 theory of computation
Presentation transcript:

Context-Free Languages & Grammars (CFLs & CFGs) Cpt S 317: Spring 2009 Context-Free Languages & Grammars (CFLs & CFGs) The structure and the content of the lecture is based on http://www.eecs.wsu.edu/~ananth/CptS317/Lectures/index.htm School of EECS, WSU

Not all languages are regular So what happens to the languages which are not regular? Can we still come up with a language recognizer? i.e., something that will accept (or reject) strings that belong (or do not belong) to the language?

Context-Free Languages Cpt S 317: Spring 2009 Context-Free Languages A language class larger than the class of regular languages Supports natural, recursive notation called “context-free grammar” Applications: Parse trees, compilers XML Context- free (PDA/CFG) Regular (FA/RE) School of EECS, WSU

An Example A palindrome is a word that reads identical from both ends Cpt S 317: Spring 2009 An Example A palindrome is a word that reads identical from both ends E.g., madam, redivider, malayalam, 010010010 Let L = { w | w is a binary palindrome} Is L regular? No. Proof: Let w=0N10N (assuming N to be the p/l constant) By Pumping lemma, w can be rewritten as xyz, such that xykz is also L (for any k≥0) But |xy|≤N and y≠ ==> y=0+ ==> xykz will NOT be in L for k=0 ==> Contradiction School of EECS, WSU

But the language of palindromes… Cpt S 317: Spring 2009 But the language of palindromes… is a CFL, because it supports recursive substitution (in the form of a CFG) This is because we can construct a “grammar” like this: A ==>  A ==> 0 A ==> 1 A ==> 0A0 A ==> 1A1 Same as: A => 0A0 | 1A1 | 0 | 1 |  Productions Terminal Variable or non-terminal How does this grammar work? School of EECS, WSU

How does the CFG for palindromes work? An input string belongs to the language (i.e., accepted) iff it can be generated by the CFG Example: w=01110 G can generate w as follows: A => 0A0 => 01A10 => 01110 G: A => 0A0 | 1A1 | 0 | 1 |  Generating a string from a grammar: Pick and choose a sequence of productions that would allow us to generate the string. At every step, substitute one variable with one of its productions.

Context-Free Grammar: Definition Cpt S 317: Spring 2009 Context-Free Grammar: Definition A context-free grammar G=(V,T,P,S), where: V: set of non-terminals T: set of terminals (= alphabet U {}) P: set of productions, each of which is of the form V ==> 1 | 2 | … Where each i is an arbitrary string of non-terminals and terminals S ==> start variable CFG for the language of binary palindromes: G=({A},{0,1},P,A) P: A -> 0 A 0 | 1 A 1 | 0 | 1 |  School of EECS, WSU

More examples Parenthesis matching in code (see HW1) Syntax checking Cpt S 317: Spring 2009 More examples Parenthesis matching in code (see HW1) Syntax checking In scenarios where there is a general need for: Matching a symbol with another symbol, or Matching a count of one symbol with that of another symbol, or Recursively substituting one symbol with a string of other symbols School of EECS, WSU

Example #2 Language of balanced paranthesis e.g., ()(((())))((()))…. Cpt S 317: Spring 2009 Example #2 Why is this problem important? Example! Language of balanced paranthesis e.g., ()(((())))((()))…. CFG? G: S => (S) | SS |  How would you generate the string “(((()))()())” using this grammar? Write a small piece of code for this! http://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/ School of EECS, WSU

Example #3 A grammar for L = {0m1n | m≥n} CFG? G: S => 0S1 | A Cpt S 317: Spring 2009 Example #3 A grammar for L = {0m1n | m≥n} CFG? G: S => 0S1 | A A => 0A |  How would you interpret the string “00000111” using this grammar? School of EECS, WSU

Example #4 A program containing if-then(-else) statements Cpt S 317: Spring 2009 Example #4 A program containing if-then(-else) statements if Condition then Statement else Statement (Or) if Condition then Statement CFG? G: S -> if C then S1 else S2 | if C then S | OTHER S (start symbol), C, S1, S2, S, OTHER – nonterminals if, then, else –terminals + other terminals School of EECS, WSU

More examples (@seminar) L1 = {0n | n≥0 } L2 = {0n | n≥1 } L3={0i1j2k | i=j or j=k, where i,j,k≥0} L4={0i1j2k | i=j or i=k, where i,j,k≥1}

Applications of CFLs & CFGs Cpt S 317: Spring 2009 Applications of CFLs & CFGs Compilers use parsers for syntactic checking Parsers can be expressed as CFGs Balancing paranthesis: B ==> BB | (B) | Statement Statement ==> … If-then-else: S ==> SS | if Condition then Statement else Statement | if Condition then Statement | Statement Condition ==> … C paranthesis matching { … } Pascal begin-end matching YACC (Yet Another Compiler-Compiler) School of EECS, WSU

More applications Markup languages Nested Tag Matching HTML XML Cpt S 317: Spring 2009 More applications Markup languages Nested Tag Matching HTML <html> …<p> … <a href=…> … </a> </p> … </html> XML <PC> … <MODEL> … </MODEL> .. <RAM> … </RAM> … </PC> School of EECS, WSU