CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Autumn 2012 CSE 311 1.

Slides:



Advertisements
Similar presentations
Recursive Definitions and Structural Induction
Advertisements

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.
Grammars, Languages and Parse Trees. Language Let V be an alphabet or vocabulary V* is set of all strings over V A language L is a subset of V*, i.e.,
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
ICE1341 Programming Languages Spring 2005 Lecture #5 Lecture #5 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
CS5371 Theory of Computation
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Context-Free Grammars Lecture 7
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Chapter 3: Formal Translation Models
Specifying Languages CS 480/680 – Comparative Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Language Translation Principles Part 1: Language Specification.
EECS 6083 Intro to Parsing Context Free Grammars
Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
CSI 3120, Grammars, page 1 Language description methods Major topics in this part of the course: –Syntax and semantics –Grammars –Axiomatic semantics (next.
CSE 311 Foundations of Computing I Lecture 15 Recursive Definitions and Structural Induction Autumn 2011 CSE 3111.
CSE 311 Foundations of Computing I Lecture 16 Recursively Defined Sets and Structural Induction Spring
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
Languages, Grammars, and Regular Expressions Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Grammars CPSC 5135.
PART I: overview material
CSE 311 Foundations of Computing I Lecture 17 Structural Induction: Regular Expressions, Regular Languages Autumn 2011 CSE 3111.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
CSE 311 Foundations of Computing I Lecture 17 Structural Induction Spring
Chapter 3 Describing Syntax and Semantics
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
CSE 311 Foundations of Computing I Lecture 24 FSM Limits, Pattern Matching Autumn 2011 CSE 3111.
CSE 311: Foundations of Computing Fall 2013 Lecture 18: Structural induction, regular expressions.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3.
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Spring
CS 461 – Sept. 23 Context-free grammars Derivations Ambiguity Proving correctness.
CSE 311 Foundations of Computing I Lecture 18 Recursive Definitions: Context-Free Grammars and Languages Autumn 2011 CSE 3111.
CSE 311 Foundations of Computing I Lecture 20 Context-Free Grammars and Languages Autumn 2012 CSE
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CSE 311 Foundations of Computing I Lecture 15 Strong Induction and Recursive Definitions Spring
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Context Free Grammars & Parsing CPSC 388 Fall 2001 Ellen Walker Hiram College.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
Chapter 3 Context-Free Grammar and Parsing
CSE 311 Foundations of Computing I
CSE 311: Foundations of Computing
CSE 311 Foundations of Computing I
Lecture 7: Introduction to Parsing (Syntax Analysis)
CSE 311: Foundations of Computing
CS 3304 Comparative Languages
High-Level Programming Language
CSE 311 Foundations of Computing I
COMPILER CONSTRUCTION
Presentation transcript:

CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Autumn 2012 CSE 311 1

Announcements Reading assignments – 7 th Edition, pp and pp – 6 th Edition, pp and pp – 5 th Edition, pp. 766 and pp For Friday, November 9 – 7 th Edition, Section 9.1 and pp – 6 th Edition, Section 8.1 and pp – 5 th Edition, Section 7.1 and pp Autumn 2012CSE 311 2

Highlight from last lecture: Structural Induction How to prove  x ∈ S. P(x) is true: Base Case: Show that P is true for all specific elements of S mentioned in the Basis step Inductive Hypothesis: Assume that P is true for some arbitrary values of each of the existing named elements mentioned in the Recursive step Inductive Step: Prove that P holds for each of the new elements constructed in the Recursive step using the named elements mentioned in the Inductive Hypothesis Conclude that  x ∈ S. P(x) Autumn 2012CSE 3113

Rooted Binary trees Basis: is a rooted binary tree Recursive Step: If and are rooted binary trees then so is: Autumn 2012CSE 3114 T1T1 T2T2 T1T1 T2T2

Functions defined on rooted binary trees size()=1 size( ) = 1+size(T 1 )+size(T 2 ) height()=0 height( )=1+max{height(T 1 ),height(T 2 )} Autumn 2012CSE 3115 T1T1 T2T2 T1T1 T2T2

For every rooted binary tree T size(T)  2 height(T)+1 -1 Autumn 2012CSE 3116

Languages: Sets of Strings Sets of strings that satisfy special properties are called languages. Examples: – English sentences – Syntactically correct Java/C/C++ programs – All strings over alphabet  – Palindromes over  – Binary strings that don’t have a 0 after a 1 – Legal variable names. keywords in Java/C/C++ – Binary strings with an equal # of 0’s and 1’s (HW6) Autumn 2012CSE 3117

Regular expressions Regular expressions over  Basis: – , are regular expressions – a is a regular expression for any a   Recursive step: – If A and B are regular expressions then so are: (A  B) (AB) A* Autumn 2012CSE 3118

Each regular expression is a “pattern” matches the empty string a matches the one character string a (A  B) matches all strings that either A matches or B matches (or both) (AB) matches all strings that have a first part that A matches followed by a second part that B matches A* matches all strings that have any number of strings (even 0) that A matches, one after another Autumn 2012CSE 311 9

Examples 001* 0*1* (0  1)0(0  1)0 (0*1*)* (0  1)* 0110 (0  1)* (00  11)* (01010  10001)(0  1)* Autumn 2012CSE

Regular expressions in practice Used to define the “tokens”: e.g., legal variable names, keywords in programming languages and compilers Used in grep, a program that does pattern matching searches in UNIX/LINUX Pattern matching using regular expressions is an essential feature of hypertext scripting language PHP used for web programming – Also in text processing programming language Perl Autumn 2012CSE

Regular Expressions in PHP int preg_match ( string $pattern, string $subject,...) $pattern syntax: [01] a 0 or a 1 ^ start of string $ end of string [0-9] any single digit \. period \, comma \- minus. any single character ab a followed by b (AB) ( a | b ) a or b (A  B) a ? zero or one of a (A  ) a * zero or more of a A* a + one or more of a AA* e.g. ^[\-+]?[0-9]*(\.|\,)?[0-9]+$ General form of decimal number e.g or -9,8 (Europe) Autumn 2012CSE

More examples All binary strings that have an even # of 1’s All binary strings that don’t contain 101 Autumn 2012CSE

Regular expressions can’t specify everything we might want Even some easy things like palindromes More complicated structures in programming languages – Matched parentheses – Properly formed arithmetic expressions – Etc. Autumn 2012CSE

Context Free Grammars A Context-Free Grammar (CFG) is given by a finite set of substitution rules involving – A finite set V of variables that can be replaced – Alphabet  of terminal symbols that can’t be replaced – One variable, usually S, is called the start symbol The rules involving a variable A are written as A  w 1 | w 2 |... | w k where each w i is a string of variables and terminals – that is w i ∈ (V    Autumn 2012CSE

How Context-Free Grammars generate strings Begin with start symbol S If there is some variable A in the current string you can replace it by one of the w’s in the rules for A – Write this as xAy ⇒ xwy – Repeat until no variables left The set of strings the CFG generates are all strings produced in this way that have no variables Autumn 2012CSE

Sample Context-Free Grammars Example: S  0S0 | 1S1 | 0 | 1 | Example: S  0S | S1 | Autumn 2012CSE

Sample Context-Free Grammars Grammar for {0 n 1 n : n≥ 0} all strings with same # of 0’s and 1’s with all 0’s before 1’s. Example: S   S  | SS | Autumn 2012CSE

Simple Arithmetic Expressions Autumn 2012CSE

Context-Free Grammars and recursively-defined sets of strings A CFG with the start symbol S as its only variable recursively defines the set of strings of terminals that S can generate A CFG with more than one variable is a simultaneous recursive definition of the sets of strings generated by each of its variables – Sometimes necessary to use more than one Autumn 2012CSE

Building in Precedence in Simple Arithmetic Expressions Autumn 2012CSE

Another name for CFGs BNF (Backus-Naur Form) grammars – Originally used to define programming languages – Variables denoted by long names in angle brackets, e.g.,,, ::= used instead of  Autumn 2012CSE

BNF for C Autumn 2012CSE

Parse Trees Back to middle school: ::= ::= | ::= Parse: The yellow duck squeaked loudly The red truck hit a parked car Autumn 2012CSE