CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.

Slides:



Advertisements
Similar presentations
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
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.
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.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
CS 3240 – Chuck Allison.  A model of computation  A very simple, manual computer (we draw pictures!)  Our machines: automata  1) Finite automata (“finite-state.
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.
COP4020 Programming Languages
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
CS 3240 – Chapter 7. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
Parsing II : Top-down Parsing Lecture 7 CS 4318/5331 Apan Qasem Texas State University Spring 2015 *some slides adopted from Cooper and Torczon.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
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.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
CMSC 330: Organization of Programming Languages
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
-Mandakinee Singh (11CS10026).  What is parsing? ◦ Discovering the derivation of a string: If one exists. ◦ Harder than generating strings.  Two major.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
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.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Context-Free Grammars
Chapter 5 Context-Free Grammars
Grammars CPSC 5135.
PART I: overview material
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.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
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 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.
Context Free Grammars.
CS 3240 – Chapter 11.  They may not halt on every possible input!  And not just because the creator of a specific TM was a doofus  This is related.
CPS 506 Comparative Programming Languages Syntax Specification.
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”
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
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.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
Chapter 5 Context-free Languages
Top-Down Parsing.
Syntax Analyzer (Parser)
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
CSE 311 Foundations of Computing I Lecture 20 Context-Free Grammars and Languages Autumn 2012 CSE
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
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.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
CONTEXT-FREE LANGUAGES
Lecture 7: Introduction to Parsing (Syntax Analysis)
CFGs: Formal Definition
Presentation transcript:

CS 3240 – Chapter 5

LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar Recursively Enumerable Turing MachineUnrestricted Phrase- Structure Grammar 2CS Introduction

 5.1: Context-Free Grammars  Derivations  Derivation Trees  5.2: Parsing and Ambiguity  5.3: CFGs and Programming Languages  Precedence  Associativity  Expression Trees CS Context-Free Languages3

 S ➞ aaSa | λ  It is not right-linear or left-linear  so it is not a “regular grammar”  But it is linear  only one variable  What is it’s language? CS Context-Free Languages4

5 S ➝ aSb | λ Deriving aaabbb: S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb

 Variables  aka “non-terminals”  Letters from some alphabet, Σ  aka “terminals”  Rules (“substitution rules”)  of the form V → s ▪ where s is any string of letters and variables, or λ  Rules are often called productions CS Context-Free Languages6

 a n cb n  a n b 2n  a n b m, where 0 ≤ n ≤ m ≤ 2n  a n b m, n ≠ m  Palindrome (start with a recursive definition)  Non-Palindrome  Equal  a n b n a m CS Context-Free Languages7

CS Pushdown Automata8 S → aSbSbS | bSaSbS | bSbSaS | λ Trace ababbb When building CFGs, remember that the start variable (S) represents a string in the language. So, for example, if S has twice as many b’s as a’s, then so does aSbSbS, etc.

 A derivation is a sequence of applications of grammatical rules, eventually yielding a string in the language  A CFG can have multiple variables on the right-hand side of a rule  Giving a choice of which variable to expand first  By convention, we usually use a leftmost derivation CS Context-Free Languages9

10 → → the → → sings | eats → cat | song | canary ⇒ ⇒ the ⇒ the canary ⇒ the canary sings ⇒ the canary sings the ⇒ the canary sings the song “sentential forms” (aka “productions”)

 A graphical representation of a derivation  The start symbol is the root  Each symbol in the right-hand side of the rule is a child node at the same level  Continue until the leaves are all terminals CS Context-Free Languages11

CS Context-Free Languages12

 Note how there was only one parse tree or the string “the canary sings the song”  And only one leftmost derivation  This is not true of all grammars!  Some grammars allow choices of distinct rules to generate the same string  Or equivalently, where there is more than one parse tree for the same string  Such a grammar is ambiguous  Not easy to process programmatically CS Context-Free Languages13

CS Context-Free Languages14 → + | * | ( ) | a | b | c ⇒ + ⇒ a + ⇒ a + * ⇒ a + b * ⇒ a + b * c ⇒ * ⇒ + * ⇒ a + * <exp ⇒ a + b * ⇒ a + b * c

CS Context-Free Languages15 Which one is “correct”?

 The process of determining if a string is generated by a grammar  And often we want the parse tree  So that we know the order of operations  Top-down Parsing  Easiest conceptually  Bottom-up Parsing  Most efficient (used by commercial compilers)  We will use a simple one in Chapter 6 CS Context-Free Languages16

 Try to match a string, w, to a grammar  If there is a rule S → w, we’re done!  Fat chance :-)  Try to find rules that match the first character  A “look-ahead” strategy  This is what we do “in our heads” anyway  Repeat on the rest of the string…  Very “brute force” CS Context-Free Languages17

CS Context-Free Languages18 S → SS | aSb | bSa | λ Parse “aabb”:

CS Context-Free Languages19 S → SS | aSb | bSa | λ Parse “aabb”: Candidate rules: 1) S → SS, 2) S → aSb: 1)SS ⇒ SSS, SS ⇒ aSbS 2)aSb ⇒ aSSb, aSb ⇒ aaSbb Answer: S ⇒ aSb ⇒ aaSbb ⇒ aabb (2) Not a well-defined algorithm (yet)!

 A top-down parsing technique  Grammar Requirements:  no ambiguity  no lambdas  no left-recursion (e.g., A -> Ab)  … and some other stuff  Create a function for each variable  Check first character to choose a rule  Start by calling S( ) CS Context-Free Languages20

 Grammar: S -> aSb | ab  Function S:  if length == 2, check to see if it is “ab”  otherwise, consume outer‘a’ and ‘b’, then call S on what’s left  See parseanbn.py, parseanbn2.py CS Context-Free Languages21

 Grammar: A -> BA | a B -> bB | b  See parsebstara.cpp CS Context-Free Languages22

 Lambda rules can cause productions to shrink  Then they can grow, and shrink again  And grow, and shrink, and grow, and shrink…  How then can we know if the string isn’t in the language?  That is, how do we know when we’re done so we can stop and reject the string? CS Context-Free Languages23

 A rule of the form A → B doesn’t increase the size of the sentential form  Once again, we could spend a long time cycling through unit rules before parsing |w|  We prefer a method that always strictly grows to |w|, so we can stop and answer “yes” or “no” efficiently  So, we will remove lambda and unit rules  In Chapter 6 CS Context-Free Languages24

 Precedence  Associativity CS Context-Free Languages25

 It was ambiguous because it treated all operators equally  But multiplication should have higher precedence than addition  So we introduce a new variable for multiplicative expressions  And place it further down in the rules  Because we want it to appear further down in the parse tree CS Context-Free Languages26

CS Context-Free Languages27 → + | → * | → ( ) | a | b | c Now only one leftmost derivation for a + b * c: ⇒ + ⇒ + ⇒ + ⇒ a + ⇒ a + * ⇒ a + b * ⇒ a + b * c

CS Context-Free Languages28

 Derive the parse tree for a + b + c …  Note how you get (a + b) + c, in effect  Left-recursion gives left associativity  Analogously for right associativity  Exercise:  Add a right-associative power (exponentiation) operator (^, with variable ) to the grammar with the proper precedence CS Context-Free Languages29