TOP-DOWN PARSING Recursive-Descent, Predictive Parsing.

Slides:



Advertisements
Similar presentations
Parsing II : Top-down Parsing
Advertisements

Compiler Construction
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Lecture # 11 Grammar Problems.
Top-Down Parsing.
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
Parsing III (Eliminating left recursion, recursive descent parsing)
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
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.
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.
Professor Yihjia Tsai Tamkang University
Top-Down Parsing.
Parsing II : Top-down Parsing Lecture 7 CS 4318/5331 Apan Qasem Texas State University Spring 2015 *some slides adopted from Cooper and Torczon.
CPSC 388 – Compiler Design and Construction
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Compiler Principles Winter Compiler Principles Exercises on scanning & top-down parsing Roman Manevich Ben-Gurion University.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Syntax and Semantics Structure of programming languages.
Parsing Chapter 4 Parsing2 Outline Top-down v.s. Bottom-up Top-down parsing Recursive-descent parsing LL(1) parsing LL(1) parsing algorithm First.
Top-Down Parsing - recursive descent - predictive parsing
Chapter 5 Top-Down 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.
# 1 CMPS 450 Parsing CMPS 450 J. Moloney. # 2 CMPS 450 Check that input is well-formed Build a parse tree or similar representation of input Recursive.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
-Mandakinee Singh (11CS10026).  What is parsing? ◦ Discovering the derivation of a string: If one exists. ◦ Harder than generating strings.  Two major.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Syntax and Semantics Structure of programming languages.
Top Down Parsing - Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University.
Parsing Top-Down.
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.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
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)
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Top-Down Predictive Parsing We will look at two different ways to implement a non- backtracking top-down parser called a predictive parser. A predictive.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
8 January 2004 Department of Software & Media Technology 1 Top Down Parsing Recursive Descent Parsing Top-down parsing: –Build tree from root symbol –Each.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Chapter 2 (part) + Chapter 4: Syntax Analysis S. M. Farhad 1.
Compiler Syntactic Analysis r Two general classes of parsing techniques m Bottom-up (Operator-Precedence parsing) Begin with the terminal nodes.
Top-down parsing 1. Last Time CYK – Step 1: get a grammar in Chomsky Normal Form – Step 2: Build all possible parse trees bottom-up Start with runs of.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
Syntax and Semantics Structure of programming languages.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Parsing — Part II (Top-down parsing, left-recursion removal)
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Context free grammars Terminals Nonterminals Start symbol productions
Parsing IV Bottom-up Parsing
Compilers Welcome to a journey to CS419 Lecture15: Syntax Analysis:
Parsing — Part II (Top-down parsing, left-recursion removal)
Top-down parsing cannot be performed on left recursive grammars.
Parsing Techniques.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Parsing — Part II (Top-down parsing, left-recursion removal)
LL and Recursive-Descent Parsing
LEFT RECURSION.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

TOP-DOWN PARSING Recursive-Descent, Predictive Parsing

Prior to top-down parsing Checklist : 1.Remove ambiguity if possible by rewriting the grammar 2.Remove left- recursion, otherwise it may lead to an infinite loop. 3. Do left- factoring.

Left- factoring In predictive parsing, the prediction is made about which rule to follow to parse the non-terminal by reading the following input symbols In case of predictive parsing, left-factoring helps remove removable ambiguity. “Left factoring is a grammar transformation that is useful for producing a grammar suitable for predictive parsing. The basic idea is that when it is not clear which of two alternative productions to use to expand a non-terminal A, we may be able to rewrite the A-productions to defer the decision until we have seen enough of the input to make the right choice.” - Aho,Ullman,Sethi

Left-factoring Here is a grammar rule that is ambiguous: A -> xP1 | xP2 | xP3 | xP4 ….| xPn Where x & Pi’s are strings of terminals and non-terminals and x !=  If we rewrite it as A-> xP’ P’ -> P1|P2|P3 …|Pn We call that the grammar has been “left-factored”, and the apparent ambiguity has been removed. Repeating this for every rule left-factors a grammar completely

Example stmt -> if exp then stmt endif | if exp then stmt endif else stmt endif We can left factor it as follows : stmt -> if exp then stmt endif ELSEFUNC ELSEFUNC -> else stmt endif |  (epsilon) Thereby removing the ambiguity

Parsers: Recursive-Descent Recursive, Uses backtracking Tries to find a leftmost derivation Unless the grammar is ambiguous or left-recursive, it finds a suitable parse tree But is rarely used as programming constructs can be parsed without backtracking Consider the grammar: S cAd | bd A ab | a and the string “cad”

ScAdScAd abab ScAdScAd a ScAdScAd A -> ab Next rule to parse A is taken A->a, turns out CORRECT, Parser stops The next non=term in line A is parsed using first rule, A -> ab, but turns out INCORRECT, parser backtracks A -> a S->cAd Recursive parsing with backtracking : example Following the first rule, S->cAd to parse S

Predictive parser It is a recursive-descent parser that needs no backtracking Suppose A -> A1 | A2 | ….| An If the non-terminal to be expanded next is ‘A’, then the choice of rule is made on the basis of the current input symbol ‘a’.

Procedure Make a transition diagram( like dfa/nfa) for every rule of the grammar. Optimize the dfa by reducing the number of states, yielding the final transition diagram To parse a string, simulate the string on the transition diagram If after consuming the input the transition diagram reaches an accept state, it is parsed.

Example The grammar is as follows E -> E + T | T T - > T * F | F F -> (E) | id After removing left-recursion, left-factoring The rules are :

Rules and their transition diagrams E->T T’ T’ -> +T T’ |  T -> F T’’ T -> *F T’’ |  T -> (E) |id E T T’ T F T’’  T’ + T T  T’ + T T T (E ) id START

Optimization After optimization it yields the following DFA like structures:  T *  F + T (E ) id START FINAL

SIMULATION METHOD Start from the start state If a terminal comes consume it, move to next state If a non – terminal comes go to the state of the “dfa” of the non-term and return on reaching the final state Return to the original “dfa” and continue parsing If on completion( reading input string completely), you reach a final state, string is successfully parsed.

Disadvantage : It is inherently a recursive parser, so it consumes a lot of memory as the stack grows. To remove this recursion, we use LL-parser, which uses a table for lookup.

ABHIGYAN 04CS1012