Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

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.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Bottom-up Parsing A general style of bottom-up syntax analysis, known as shift-reduce parsing. Two types of bottom-up parsing: Operator-Precedence parsing.
LR-Grammars LR(0), LR(1), and LR(K).
Mooly Sagiv and Roman Manevich School of Computer Science
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
1 Chapter 5: Bottom-Up Parsing (Shift-Reduce). 2 - attempts to construct a parse tree for an input string beginning at the leaves (the bottom) and working.
Pertemuan 12, 13, 14 Bottom-Up Parsing
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
LR(1) Languages An Introduction Professor Yihjia Tsai Tamkang University.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 CIS 461 Compiler Design & Construction Fall 2012 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #12 Parsing 4.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
– 1 – CSCE 531 Spring 2006 Lecture 8 Bottom Up Parsing Topics Overview Bottom-Up Parsing Handles Shift-reduce parsing Operator precedence parsing Readings:
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.
BOTTOM-UP PARSING Sathu Hareesh Babu 11CS10039 G-8.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
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 Compiler Construction Syntax Analysis Top-down parsing.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CH4.1 CSE244 Sections 4.5,4.6 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Box U-155 Storrs,
CMSC 331, Some material © 1998 by Addison Wesley Longman, Inc. 1 Chapter 4 Chapter 4 Bottom Up Parsing.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Syntax and Semantics Structure of programming languages.
1 Bottom-Up Parsing  “Shift-Reduce” Parsing  Reduce a string to the start symbol of the grammar.  At every step a particular substring is matched (in.
Chapter 5: Bottom-Up Parsing (Shift-Reduce)
1 Compiler Construction Syntax Analysis Top-down parsing.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
10/10/2002© 2002 Hal Perkins & UW CSED-1 CSE 582 – Compilers LR Parsing Hal Perkins Autumn 2002.
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Bernd Fischer RW713: Compiler and Software Language Engineering.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Syntax Analysis Part II Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Syntax and Semantics Structure of programming languages.
lec02-parserCFG May 8, 2018 Syntax Analyzer
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Compiler design Bottom-up parsing Concepts
Bottom-Up Parsing.
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Revision ? E  TE  E   + TE  |  T  FT  T   * FT  | 
Parsing IV Bottom-up Parsing
Fall Compiler Principles Lecture 4: Parsing part 3
Bottom-Up Syntax Analysis
4 (c) parsing.
Subject Name:COMPILER DESIGN Subject Code:10CS63
4d Bottom Up Parsing.
BOTTOM UP PARSING Lecture 16.
Lecture 8 Bottom Up Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
Parsing IV Bottom-up Parsing
Bottom-Up Parsing “Shift-Reduce” Parsing
4d Bottom Up Parsing.
Kanat Bolazar February 16, 2010
4d Bottom Up Parsing.
lec02-parserCFG May 27, 2019 Syntax Analyzer
Chap. 3 BOTTOM-UP PARSING
4d Bottom Up Parsing.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol table –Describe the program objects l Type l Location l Scope and access l Munge the AST –Optimize –Modify l Subscript checking l Communication interactions l Generate executable code

Terminology l Derivation Sequence starting with the start symbol S and proceeding through a sequence of derivation steps l Derivation step A non-terminal on the left side of a derivation is replaced by the right hand side of a production rule in the next step of the derivation  A  if A  is a production, and ,  are arbitrary strings of grammar symbols l Sentential Form Any derivation step l Sentence Sentential form with no non-terminals

Derivation example l Grammar E  E + E | E * E | ( E ) | - E | id l Simple derivation E  - E Derivation of -(id+id) E  -E  -(E)  -(E+E)  -(id+E)  -(id+id) E -(id+id) derives it  1   2  …   n or simply   Select a nonterminal to replace and an alternative at each step of a derivation    

Leftmost Derivation The derivation E  -E  -(E)  -(E+E)  -(id+E)  -(id+id) is leftmost which we designate as E  lm -E  lm -(E)  lm -(E+E)  lm -(id+E)  lm -(id+id) l A sentential form is a derivation step. l A leftmost derivation step is a left sentential form, for example: l ( Denoted  * lm for typographical convenience)

Leftmost Derivation l A derivation in which only the leftmost non- terminal in any sentential form is replaced at each step. l Unique derivation for a string most of the time

Rightmost Derivation l The rightmost non-terminal is replaced in the derivation process in each step. l Also referred to as Canonical Derivation l Right sentential form: a sentential form produced via a rightmost derivation –If S  * , then  is a sentential form of the CFG –If S  * rm , then  is a right sentential form

Right Sentential Form Grammar: S  aABe A  Abc | e B  d l Reduce abbcde to S by four steps abbcde aAbcde aAde aABe S l In reverse, it is S  rm aABe  rm aAde  rm aAbcde  rm abbcde –abbcde is a right-sentential form (replace b in position 2 by A) –aAbcde is a right-sentential form (replace Abc in position 2 by A)

Top Down Parsing

Top Down l For a given string, builds a parse tree from the start symbol of the grammar, and grows towards the roots l Suitable grammar is LL(k) –Leftmost processing, leftmost derivation, look at most k symbols ahead –Should be left-factored, and without immediate left recursion l Recursive descent parsing –May backtrack, for certain grammars –Good error-detection and handling capabilities l Predictive parsing –No backtracking –Given the partial derivation and the leading terminal, exactly one production rule is applicable –Parsers include l Hands written recursive descent l Table-driven LL(k) parser (for later)

Two: Operator Precedence

l Operator grammar no right side of a production contains adjacent non- terminals l S  EoE | id, o  + | - | * | / (X) l If a grammar is an operator grammar and it has no productions with null of the RHS, then there is a operator-precedence parser for that grammar l Special case of a shift-reduce parser

Precedence Grammars l Parse with shift/reduce l No production right side is , and no right side has two adjacent nonterminals l bad multi-precedence operator (-) difficult can’t be sure parser matches the grammar! only works for some grammars l good simple, simple, simple Build on non-reflexive precedence relations that we denote as. >, , as in text)

Computing Precedence l Precedence is disjoint. Can have a <. b a b c b is read “yields precedence” or “equal precedence” l Obtain precedence by manual assignment using traditional associative and precedence rules, or mechanically from nonambiguous grammar l How to process –“Ignore” nonterminals, and then delimit handle from right side. > and then back up to the left side <.

Operator Precedence Parser l Remove (hide) nonterminals and place precedence relationship between pairs of terminals (1) Represent id + id * id as $ + * $ (2) Apply scanning method a) scan from left toward right to find. > b) backwards scan to <. or $ c) handle is located between start and end of scan d) reduce the handle to the corresponding nonterminal l Relies on the grammar’s special form (1) In grammar rule, no adjacent nonterminals on the right hand-side (by definition), so no right sentential form will have two non-terminals (2) Form is  0 a 1  1... a n  n  i is nonterminal or  a i is a nonterminal

Bottom Up Parsing (shift-reduce)

Bottom Up l What –For a given string, builds a parse tree starting at the leaves and reaches the root. –Known as shift-reduce parsing –Rightmost derivation in reverse l How –Start with the given input string (I.e., program) –Find a substring that matches the right side of a production –Substitute left-side of grammar rule when right-side matches l Terminology –A handle is a substring that matches the right hand side of a production –Handles will be reduced to the left-hand side of the matching production

Handles l Handle –Substring that matches the right side of a production –When reduced (to the left side), this represents one step along the reverse of a rightmost derivation l Handle Pruning –Replacing a handle by the left hand side

Implementation l Stack is a good data type to implement a shift-reduce parser –The stack contains the grammar symbols l Nonterminals recognized from the input l Terminals not yet recognized as belonging to any production –An input tape contains the input string l The parser –Shift next input symbol from the input tape and into the stack –Keep shifting until a handle appears at top of stack –Reduce when a handle appears on top of stack –Terminate when the stack contains S and the input tape is null

LR(k) Grammar l LR grammar if it can be recognized by a bottom-up parser on a left to right scan can recognize handles when they appear on the top of the stack l LR(k) – left to right scanning l LR(k) -- rightmost derivation in reverse l Supports non-backtracking shift-reduce parsing method. Deterministic parsing! l Too hard to construct parse-table by hand (so use a parser-generator)

LR Parsing Algorithm l Parsing program l Parsing table l Input tape l Stack l Output tape

Parsing Table Action[] What to do when a symbol appears on the tape Action[State, input symbol]  Shift, reduce, accept, reject Goto[] A finite automaton that can recognize a handle on the top of the stack by scanning the stack top to bottom Goto[State, grammar symbol]  State

Whats so great about LR Grammars? l “The LR requirement is that we be able to recognize the occurrence of the right side of a production, having seen what is derived from that side. This is far less stringent than the requirement for predictive parsing, namely that we be able to recognize the apparent use of the production seeing only the first symbol that it derives” [AU77:202] l The state symbol on top of the stack contains all the information the parser needs

The Underlying Basis of LR (a Lasting Relationship ) l Whereas the nondeterministic nPDA is equivalent to the CFG, the deterministic dPDA is only equivalent to a subclass of deterministic CFL l Every LR(k) grammar generates a deterministic CFL l Every deterministic CFL has an LR(1) grammar l No ambiguous grammar can be LR(k) for any k l However, it remains undecidable whether a CFG G is ambiguous

LR Parsing Algorithm l A Parsing Program l A Parsing Table l An input Tape l A stack l An output Tape

Parsing Table l Action –(State, input symbol) –Shift, reduce, accept, reject l Goto –A finite automaton that can recognize a handle on the top of the stack by scanning the stack top to bottom –(State, grammar symbol) –State

LR Algorithm action[s m,a i ]  { shift s, reduce a , accept, error } goto[s, a] takes state and grammar symbol and produces a state It is the transition function of a DFA on viable prefixes of G Viable prefix is a prefix that can appear on the stack during a rightmost derivation l Progresses through configurations Configuration – right sentential forms with states intermixed Written as pairs – (stack contents, unexpended input) l To obtain the next move –read a (current input) –Look at s m (state at top-of-stack) –consult action[]

LR Actions action[s m,a] = shift s execute a shift move: (s 0 X 1 s 1 X 2 s 2.. X m s m, a i a i+1.. a n $) shifted current input a i and next state s off of input and onto stack (s 0 X 1 s 1 X 2 s 2.. X m s m a i s, a i+1.. a n $) action[sm,a] = reduce a  execute a reduce move: (s 0 X 1 s 1 X 2 s 2.. X m s m, a i a i+1.. a n $) popped 2r symbols off (r for state, r for grammar) pushed A and s onto stack no change in input (s 0 X 1 s 1 X 2 s 2.. X m-r s m-r A s, a i a i+1.. a n $) where s=goto[s m-r,A], and r is length of  (rule right-hand side) See example 4.33, pages for detail

Construction of LR parser table l SLR (Simple LR) l LALR (Look Ahead LR) l Canonical LR

Yet Another Compiler Compiler l Parser Generator l Converts a Context Free Grammar into a set of tables for an automaton. l Generates LALR parser l This automaton executes the LALR(1) parser algo.