Transforming Grammars. CF Grammar Terms Parse trees. – Graphical representations of derivations. – The leaves of a parse tree for a fully filled out tree.

Slides:



Advertisements
Similar presentations
Parsing II : Top-down Parsing
Advertisements

lec02-parserCFG March 27, 2017 Syntax Analyzer
CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Cse321, Programming Languages and Compilers 1 6/18/2015 Lecture #7, Feb. 5, 2007 Grammars Top down parsing Transition Diagrams Ambiguity Left recursion.
Context-Free Grammars Lecture 7
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Parsing III (Eliminating left recursion, recursive descent parsing)
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
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 536 Spring Ambiguity Lecture 8. CS 536 Spring Announcement Reading Assignment –“Context-Free Grammars” (Sections 4.1, 4.2) Programming.
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Chapter 3: Formal Translation Models
Parsing II : Top-down Parsing Lecture 7 CS 4318/5331 Apan Qasem Texas State University Spring 2015 *some slides adopted from Cooper and Torczon.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Context-Free Grammars and Parsing 1.
8/19/2015© Hal Perkins & UW CSEC-1 CSE P 501 – Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Syntax and Semantics Structure of programming languages.
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
Top-Down Parsing - recursive descent - predictive parsing
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of CFL was to formalize.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 Syntax Specification (Sections ) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos at UNC.
Syntax and Semantics Structure of programming languages.
Left Recursion Lecture 7 Fri, Feb 4, 2005.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
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.
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.
TOP-DOWN PARSING Recursive-Descent, Predictive Parsing.
1 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
Top-Down Parsing.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
CFL Big Picture. Context Free Languages Conclusion We have studied the class of context free languages (CFL) We saw two different ways to express a CFL.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Chapter 2 (part) + Chapter 4: Syntax Analysis S. M. Farhad 1.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
Parsing #1 Leonidas Fegaras.
lec02-parserCFG May 8, 2018 Syntax Analyzer
Parsing & Context-Free Grammars
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
CS510 Compiler Lecture 4.
Context-free grammars, derivation trees, and ambiguity
Chapter 3 Context-Free Grammar and Parsing
Parsing — Part II (Top-down parsing, left-recursion removal)
Compiler Construction
Syntax Analysis Sections :.
Parsing & Context-Free Grammars Hal Perkins Autumn 2011
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Lecture 7: Introduction to Parsing (Syntax Analysis)
Programming Languages
CSC 4181Compiler Construction Context-Free Grammars
CSC 4181 Compiler Construction Context-Free Grammars
BNF 9-Apr-19.
Context Free Grammar – Quick Review
Parsing & Context-Free Grammars Hal Perkins Summer 2004
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of formalizing the structure.
lec02-parserCFG May 27, 2019 Syntax Analyzer
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
CFL Big Picture.
Presentation transcript:

Transforming Grammars

CF Grammar Terms Parse trees. – Graphical representations of derivations. – The leaves of a parse tree for a fully filled out tree is a sentence. Regular language v.s. Context Free Languages – how do CFL compare to regular expressions? – Nesting (matched ()’s) requires CFG,’s RE's are not powerful enough. Ambiguity – A string has two derivations – E -> E + E | E * E | id x + x * y Left-recursion – E -> E + E | E * E | id – Makes certain top-down parsers loop

Grammar Transformations Backtracking and Factoring Removing ambiguity. – Simple grammars are often easy to write, but might be ambiguous. Removing Left Recursion Removing  -productions

Removing Left Recursion Top down recursive descent parsers require non-left recursive grammars Technique: Left Factoring E -> E + E | E * E | id E -> id E’ E’ -> + E E’ | * E E’ | 

General Technique to remove direct left recursion Every Non terminal with productions T -> T n | T m (left recursive productions) | a | b (non-left recursive productions) Make a new non-terminal T’ Remove the old productions Add the following productions T -> a T’ | b T’ T’ -> n T’ | m T’ |  T Tn Tn T n a (a | b) (n | m) * “a” and “b” because they are the rhs of the non-left recurive productions.

Backtracking and Factoring Backtracking may be necessary: S -> ee | bAc | bAe A -> d | cA try on string “bcde” S -> bAc (by S -> bAc) -> bcAc (by A -> cA) -> bcdc (by A -> d) But now we are stuck, we need to backtrack to – S -> bAc – And then apply the production (S -> bAe) Factoring a grammar – Factor common prefixes and make the different postfixes into a new non- terminal S -> ee | bAQ Q -> c | e A -> d | cA

Removing ambiguity. Adding levels to a grammar E -> E + E | E * E | id | ( E ) Transform to an equivalent grammar E -> E + T | T T -> T * F | F F -> id | ( E ) Levels make formal the notion of precedence. Operators that bind “tightly” are on the lowest levels

The dangling else grammar. st -> if exp then st else st | if exp then st | id := exp Note that the following has two possible parses if x=2 then if x=3 then y:=2 else y := 4 if x=2 then (if x=3 then y:=2 ) else y := 4 if x=2 then (if x=3 then y:=2 else y := 4)

Adding levels (cont) Original grammar st ::= if exp then st else st | if exp then st | id := exp Assume that every st between then and else must be matched, i.e. it must have both a then and an else. New Grammar with addtional levels st -> match | unmatch match -> if exp then match else match | id := exp unmatch -> if exp then st | if exp then match else unmatch

Removing  -productions It is possible to write every CFL that does not contain  (the empty string) without any  in any RHS S -> aDaE D -> bD | E E -> cE | 

Rules 1.Find all non-terminal, N, such that N derives  2.For each production, A -> w, create new productions, A -> w’, where w’ is derived from w by removing non-terminals that derive  (found in rule 1 above) 3.Create a new grammar from the original productions, together with the productions formed in step 2, removing any productions of the form, A -> .

1.Find all non-terminal, N, such that N derives  2.For each production, A -> w, create new productions, A -> w’, where w’ is derived from w by removing non-terminals that derive  (found in rule 1 above) 3.Create a new grammar from the original productions, together with the productions formed in step 2, removing any productions of the form, A -> . S -> aDaE D -> bD | E E -> cE |  E ->  S -> aDa D ->  E -> c S -> aDaE D -> bD | E E -> cE S -> aDa E -> c

Top to bottom example Start with an easy (to understand) grammar Transform it to one that is easier to parse Apply some of the transformation rules RE -> RE bar RE RE -> RE RE RE -> RE * RE -> id RE -> ^ RE -> ( RE )

A datatype suitable for representing Regular Expresions Build an instance of the datatype: data RegExp a = Lambda -- the empty string "" | Empty -- the empty set | One a -- a singleton set {a} | Union (RegExp a) (RegExp a) -- union of two RegExp | Cat (RegExp a) (RegExp a) -- Concatenation | Star (RegExp a) -- Kleene closure

Ambiguous grammar RE -> RE bar RE RE -> RE RE RE -> RE * RE -> id RE -> ^ RE -> ( RE ) Alt -> Alt bar Concat Alt -> Concat Concat -> Concat Closure Concat -> Closure Closure -> simple star Closure -> simple simple -> id | ( Alt ) | ^ Transform grammar by layering Tightest binding operators (*) at the lowest layer Layers are Alt, then Concat, then Closure, then Simple.

Left Recursive Grammar Alt -> Alt bar Concat Alt -> Concat Concat -> Concat Closure Concat -> Closure Closure -> simple star Closure -> simple simple -> id | (Alt ) | ^ Alt -> Concat moreAlt moreAlt -> Bar Concat moreAlt |  Concat -> Closure moreConcat moreConcat -> Closure moreConcat |  Closure -> Simple Star | Simple Simple -> Id | ( Alt ) | ^ For every Non terminal with productions T ::= T n | T m (left recursive prods) | a | b (non-left recursive prods) Make a new non-terminal T’ Remove the old productions Add the following productions T ::= a T’ | b T’ T’ ::= n T’ | m T’ | 