Parsing Techniques.

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

Parsing II : Top-down Parsing
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Parsing III (Eliminating left recursion, recursive descent parsing)
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
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.
Parsing II : Top-down Parsing Lecture 7 CS 4318/5331 Apan Qasem Texas State University Spring 2015 *some slides adopted from Cooper and Torczon.
Compiler Construction Parsing Part I
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.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Parsing III (Top-down parsing: recursive descent & LL(1) )
-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.
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.
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.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
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 CIS 461 Compiler Design and Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #8 Parsing Techniques.
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)
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
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.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #6 Parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Introduction to Parsing
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Comp 411 Principles of Programming Languages Lecture 3 Parsing
Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
Compiler Construction Parsing Part I
Parsing #1 Leonidas Fegaras.
lec02-parserCFG May 8, 2018 Syntax Analyzer
Parsing — Part II (Top-down parsing, left-recursion removal)
Parsing III (Top-down parsing: recursive descent & LL(1) )
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
CS510 Compiler Lecture 4.
Lexical and Syntax Analysis
Context free grammars Terminals Nonterminals Start symbol productions
Unit-3 Bottom-Up-Parsing.
Parsing IV Bottom-up Parsing
Compilers Welcome to a journey to CS419 Lecture15: Syntax Analysis:
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
Parsing with Context Free Grammars
4 (c) parsing.
Top-Down Parsing.
Top-Down Parsing CS 671 January 29, 2008.
Lecture 8 Bottom Up Parsing
Compiler Design 7. Top-Down Table-Driven Parsing
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
Lecture 8: Top-Down Parsing
Bottom Up Parsing.
Compiler Construction
Parsing IV Bottom-up Parsing
Introduction to Parsing
Introduction to Parsing
Parsing — Part II (Top-down parsing, left-recursion removal)
LL and Recursive-Descent Parsing
Syntax Analysis - Parsing
Nonrecursive Predictive Parsing
Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
lec02-parserCFG May 27, 2019 Syntax Analyzer
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Parsing Techniques

Parsing Techniques Top-down parsers Start at the root of the parse tree and grow towards leaves. Pick a production and try to match the input

Parsing Techniques Top-down parsers Start at the root of the parse tree and grow towards leaves.ck a production and try to match the input

Parsing Techniques Top-down parsers Start at the root of the parse tree and grow towards leaves. Pick a production and try to match the input

Parsing Techniques Top-down parsers Bad “pick”  may need to backtrack Some grammars are backtrack-free.

Parsing Techniques Top-down parsers Bad “pick”  may need to backtrack Some grammars are backtrack-free.

Parsing Techniques Bottom-up parsers Start at the leaves and grow toward root As input is consumed, encode possibilities in an internal state.

Parsing Techniques Bottom-up parsers Start at the leaves and grow toward root As input is consumed, encode possibilities in an internal state.

Parsing Techniques Bottom-up parsers Start at the leaves and grow toward root As input is consumed, encode possibilities in an internal state. end of lec 12 compiler: intro

Compiler Construction Sohail Aslam Lecture 13 compiler: intro

Parsing Techniques Bottom-up parsers Start in a state valid for legal first tokens Bottom-up parsers handle a large class of grammars

Parsing Techniques Bottom-up parsers Start in a state valid for legal first tokens Bottom-up parsers handle a large class of grammars

Top-Down Parser A top-down parser starts with the root of the parse tree. The root node is labeled with the goal symbol of the grammar

Top-Down Parser A top-down parser starts with the root of the parse tree. The root node is labeled with the goal symbol of the grammar

Top-Down Parsing Algorithm Construct the root node of the parse tree Repeat until the fringe of the parse tree matches input string

Top-Down Parsing Algorithm Construct the root node of the parse tree Repeat until the fringe of the parse tree matches input string

Top-Down Parsing At a node labeled A, select a production with A on its lhs for each symbol on its rhs, construct the appropriate child

Top-Down Parsing At a node labeled A, select a production with A on its lhs for each symbol on its rhs, construct the appropriate child

Top-Down Parsing When a terminal symbol is added to the fringe and it does not match the fringe, backtrack Find the next node to be expanded

Top-Down Parsing When a terminal symbol is added to the fringe and it does not match the fringe, backtrack Find the next node to be expanded

Top-Down Parsing The key is picking right production in step 1. That choice should be guided by the input string

Top-Down Parsing The key is picking right production in step 1. That choice should be guided by the input string

Expression Grammar 1 Goal → expr 2 expr + term 3 | expr - term 4 term 5 term * factor 6 term ∕ factor 7 factor 8 number 9 id 10 ( expr )

Top-Down Parsing Let’s try parsing x – 2 * y

P Sentential Form input - Goal x – 2 * y 1 expr 2 expr + term 4 term + term 7 factor + term 9 <id,x> + term x – 2 * y

This worked well except that “–” does not match “+” Sentential Form input - Goal x – 2 * y 1 expr 2 expr + term 4 term + term 7 factor + term 9 <id,x> + term x – 2 * y This worked well except that “–” does not match “+”

The parser must backtrack to here Sentential Form input - Goal x – 2 * y 1 expr 2 expr + term 4 term + term 7 factor + term 9 <id,x> + term x – 2 * y The parser must backtrack to here

This time the “–” and “–” matched P Sentential Form input - Goal x – 2 * y 1 expr 2 expr – term 4 term – term 7 factor – term 9 <id,x> – term x – 2 * y This time the “–” and “–” matched

We can advance past “–” to look at “2” Sentential Form input - Goal x – 2 * y 1 expr 2 expr – term 4 term – term 7 factor – term 9 <id,x> – term x – 2 * y x – 2 * y We can advance past “–” to look at “2”

Now, we need to expand “term” Sentential Form input - Goal x – 2 * y 1 expr 2 expr – term 4 term – term 7 factor – term 9 <id,x> – term x – 2 * y x – 2 * y Now, we need to expand “term”

We have more input but no non-terminals left to expand Sentential Form input - <id,x> – term x – 2 * y 7 <id,x> – factor 9 <id,x> – <num,2> x – 2 * y “2” matches “2” We have more input but no non-terminals left to expand

The expansion terminated too soon  Need to backtrack Sentential Form input - <id,x> – term x – 2 * y 7 <id,x> – factor 9 <id,x> – <num,2> x – 2 * y The expansion terminated too soon  Need to backtrack

Success! We matched and consumed all the input Sentential Form input - <id,x> – term x – 2 * y 5 <id,x> – term * factor 7 <id,x> – factor * factor 8 <id,x> – <num,2> * factor x – 2  * y x – 2 *  y 9 <id,x> – <num,2> * <id,y> x – 2 * y  Success! We matched and consumed all the input

Another Possible Parse Sentential Form input - Goal x – 2 * y 1 expr 2 expr +term expr +term +term expr +term +term +term expr +term +term +term +.... Wrong choice of expansion leads to non-termination consuming no input!! Parser must make the right choice

Top-down parsers cannot handle left-recursive grammars Left Recursion Top-down parsers cannot handle left-recursive grammars

Left Recursion Formally, A grammar is left recursive if  A  NT such that  a derivation A * A a, for some string a  (NT  T)*

Left Recursion Our expression grammar is left recursive. This can lead to non-termination in a top-down parser

Left Recursion Our expression grammar is left recursive. This can lead to non-termination in a top-down parser

Left Recursion Non-termination is bad in any part of a compiler!

Left Recursion For a top-down parser, any recursion must be a right recursion We would like to convert left recursion to right recursion

Left Recursion For a top-down parser, any recursion must be a right recursion We would like to convert left recursion to right recursion

Eliminating Left Recursion To remove left recursion, we transform the grammar

Eliminating Left Recursion Consider a grammar fragment: A → A a | b where neither a nor b starts with A.

Eliminating Left Recursion We can rewrite this as: A → b A' A' → a A' | e where A' is a new non-terminal

Eliminating Left Recursion We can rewrite this as: A → b A' A' → a A' | e where A' is a new non-terminal

Eliminating Left Recursion A → b A ' A' → a A' | e This accepts the same language but uses only right recursion

Eliminating Left Recursion The expression grammar we have been using contains two cases of left- recursion

Eliminating Left Recursion expr → expr + term | expr – term term term * factor term ∕ factor factor

Eliminating Left Recursion Applying the transformation yields expr → term expr' expr' + term expr' | – term expr' e

Eliminating Left Recursion Applying the transformation yields term → factor term' term' * factor term' | ∕ factor term' e

Eliminating Left Recursion These fragments use only right recursion They retain the original left associativity A top-down parser will terminate using them.

Eliminating Left Recursion These fragments use only right recursion They retain the original left associativity A top-down parser will terminate using them.

Eliminating Left Recursion These fragments use only right recursion They retain the original left associativity A top-down parser will terminate using them.

1 Goal → expr 2 term expr' 3 expr' + term expr' 4 | – term expr' 5 e 6 term factor term' 7 term' * factor term' 8 ∕ factor term' 9 10 factor number 11 id 12 ( expr )

Predictive Parsing If a top down parser picks the wrong production, it may need to backtrack Alternative is to look ahead in input and use context to pick correctly

Predictive Parsing If a top down parser picks the wrong production, it may need to backtrack Alternative is to look ahead in input and use context to pick correctly

Predictive Parsing How much lookahead is needed? In general, an arbitrarily large amount

Predictive Parsing How much lookahead is needed? In general, an arbitrarily large amount

Predictive Parsing Fortunately, large classes of CFGs can be parsed with limited lookahead Most programming languages constructs fall in those subclasses

Predictive Parsing Fortunately, large classes of CFGs can be parsed with limited lookahead Most programming languages constructs fall in those subclasses

Predictive Parsing Basic Idea: Given A → a | b, the parser should be able to choose between a and b.

Predictive Parsing FIRST Sets: For some rhs a  G, define FIRST(a) as the set of tokens that appear as the first symbol in some string that derives from a.

Predictive Parsing That is, x  FIRST(a) iff a * x g, for some g.