CS 404 Introduction to Compiler Design

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

Compiler Construction
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.
Top-Down Parsing.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Professor Yihjia Tsai Tamkang University
Top-Down Parsing.
CPSC 388 – Compiler Design and Construction
Syntax and Semantics Structure of programming languages.
Top-Down Parsing - recursive descent - predictive parsing
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
Chapter 5 Top-Down Parsing.
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
1 Compiler Construction Syntax Analysis Top-down parsing.
Syntax and Semantics Structure of programming languages.
1 Compiler Construction Syntax Analysis Top-down parsing.
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 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
1 Chapter 6 Bottom-Up Parsing. 2 Bottom-up Parsing A bottom-up parsing corresponds to the construction of a parse tree for an input tokens beginning at.
Syntax and Semantics Structure of programming languages.
Compiler Construction
Chapter 4 - Parsing CSCE 343.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Compiler design Bottom-up parsing Concepts
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Syntactic Analysis and Parsing
Parsing.
Compiler Construction
Top-down parsing cannot be performed on left recursive grammars.
Compiler Construction
Fall Compiler Principles Lecture 4: Parsing part 3
UNIT 2 - SYNTAX ANALYSIS Role of the parser Writing grammars
Bottom-Up Syntax Analysis
4 (c) parsing.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Regular Grammar - Finite Automaton
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
CSC 4181 Compiler Construction Parsing
Syntax Analysis source program lexical analyzer tokens syntax analyzer
4d Bottom Up Parsing.
BOTTOM UP PARSING Lecture 16.
Lecture 8 Bottom Up Parsing
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
Parsing IV Bottom-up Parsing
Compiler SLR Parser.
LR Parsing. Parser Generators.
Syntax Analysis - Parsing
4d Bottom Up Parsing.
Kanat Bolazar February 16, 2010
Announcements HW2 due on Tuesday Fall 18 CSCI 4430, A Milanova.
4d Bottom Up Parsing.
Nonrecursive Predictive Parsing
Context Free Grammar – Quick Review
4d Bottom Up Parsing.
Chap. 3 BOTTOM-UP PARSING
4d Bottom Up Parsing.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

CS 404 Introduction to Compiler Design Lecture 4 Ahmed Ezzat Top-Down Parsing LL(1), Bottom-Up Parsing LR CS 404 Ahmed Ezzat

1. Top-down Parsing Predictive: try to guess which production rule to apply next, given The current non-terminal symbol One or more ‘look-ahead’ terminal symbols Two ways to do predictive parsing Use recursive procedures Use a predictive parsing table CS 404 Ahmed Ezzat

LL(1) Grammar A restrict set of grammars with no need to backtrack Uses an explicit stack rather than recursive calls to perform parsing LL(k) parsing means that k tokens of lookahead are used LL(1): L: scan input string from left to right L: left-most derivation is applied at each step 1: one input symbol for lookahead CS 404 Ahmed Ezzat

Two Separate Steps Construct LL(1) parsing table Compute FIRST and FOLLOW Construct the parsing table Parsing Stack: that holds grammar symbol: non-terminals and tokens. Parsing strings using the parsing table CS 404 Ahmed Ezzat

FIRST and FOLLOW sets FIRST(α) contains any symbol that might begin a sentence derived from α FOLLOW(A) includes all symbols that could appear immediately after A in a valid sentence CS 404 Ahmed Ezzat

Compute FIRST If x is a terminal, then FIRST(x) = {x} If xε, then add ε to FIRST(x) If x is non-terminal and XY1Y2…Yk, then add z to FIRST(x) if for some i, z is in FIRST(Yi) and ε is in FIRST(Yj) for all j<i CS 404 Ahmed Ezzat

Compute FIRST for a String α (FI4) For α = X1X2…Xn Add all non-ε symbols of FIRST(X1) to FIRST(α) Add all non- ε symbols of FIRST(Xj) to FIRST(α) if ε is in all FIRST(Xi) for i<j Add ε to FIRST(α) if ε is in all FIRST(Xi) for all i CS 404 Ahmed Ezzat

Compute FOLLOW (FO1) Put $ in FOLLOW(S) ($ is called endmarker) (FO2) If AαBβ, then put FIRST(β) (except ε) into FOLLOW(B) (FO3) If AαB, then put FOLLOW(A) into FOLLOW(B) (FO4) If AαBβ and βε, then put FOLLOW(A) into FOLLOW(B) CS 404 Ahmed Ezzat

Predictive Parsing and Left factoring Example Assume the following Grammar: E  T + E | T T  int | int * T | (E) Hard to predict because: For T, 2 productions start with int For E, it is not clear how to predict The Grammar must be left-factored before being used for predictive parsing CS 404 Ahmed Ezzat

Predictive Parsing and Left factoring Example Assume the following Grammar: E  T + E | T T  int | int * T | (E) Factor out common prefixes of productions, possibly introducing ε-productions E  TX X  + E | ε T  (E) | int Y Y  * T | ε int * + ( ) $ E TX T X X + E ε T Int Y ( E ) Y * T CS 404 Ahmed Ezzat

Construct the Parsing Table For each production rule Aα [M1] For each terminal a in FIRST(α), add Aα to M[A,a] [M2] If ε is in FIRST(α), add Aα to M[A,b] for each terminal b in FOLLOW(A). (b can be $) Unidentified entry of M are ‘error entries’ CS 404 Ahmed Ezzat

Use Parsing Table to Parse Push $S into the stack; attach $ to the end of the string. x is the stack top, a is the input If x=a=$, success If x=a<>$, pop x, advance input If x is non-terminal If M[x,a] = {xUVW}, replace x by WVU (U on top) If M[x,a] has no rule, error CS 404 Ahmed Ezzat

Use Parsing Table Example to Parse E  TX X  + E | ε T  (E) | int Y Y  * T | ε CS 404 Ahmed Ezzat

2. Bottom-up Parsing Start from the leaf nodes of a tree and works in upward direction till reaching the root node CS 404 Ahmed Ezzat

Bottom-up Parsing Start with string of terminals Builds up from leaves of parse tree Apply production rules backwards (reduction) When reach start symbol and exhausted input, done Shift-reduce is one common type of bottom-up parsing CS 404 Ahmed Ezzat

Bottom-up Parsing Shift-Reduce Parsing: Shift: advance input pointer to next input symbol; symbol is pushed into the stack Reduce: when parser finds complete grammar rule (RHS) and replace it to (LHS) LR Parser: it is non-recursive, shift-reduce, bottom-up parser SLR(1): Simple LR parser; works on smallest class of grammar LR(1): Works on complete set of LR(1) grammar LALR(1): Look-Ahead LR parser. Works on intermediate size of grammar. # of states is the same as in SLR(1). CS 404 Ahmed Ezzat

Bottom-up Parsing - Example Bottom-up parser traces rightmost derivation in reverse E T + int * int * int + int int * T + int T + int T + T T + E E CS 404 Ahmed Ezzat

Shift-reduce Parsing Use context-free grammar (may not be LL1) Use stack to keep track of tokens seen so far Hard to do manually, but best with Yacc Basic idea: Shift next symbol onto stack When stack top contains a good right-hand-side of a production, reduce by a rule CS 404 Ahmed Ezzat

When to Shift or Reduce? Reduce if top of stack represents the right hand side of a production rule (a handle) Need to recognize handles If cannot reduce and there are more inputs, shift If cannot shift or reduce, error Use Action and Goto tables to help decide CS 404 Ahmed Ezzat

Shift or Reduce Example Shift: Move | one place to the right Shifts a terminal to the left string ABC|XYZ  ABCX|YZ Reduce: apply an inverse production rule at the right end of the left string If A  XY is a production rule, then Cbxy|ijk  CbA|ijk CS 404 Ahmed Ezzat

LR Parsing Left to right input (Left scan) Right-most derivation in reverse order Efficient, table based parsing by shift-reduce Can handles more grammar than LL(1) Can handle most programming languages CS 404 Ahmed Ezzat

LR Parsing Data Structure Stack of states {S0, …, Sm} Action Table: Action[S’,a], a is terminal. Tells the parser whether to: Shift (S’) Reduce (R) Accept (A) the source code, or Signal a syntactic error (E) Goto Table: Goto[S’,X], X is non-terminal. Defines the next state after a shift CS 404 Ahmed Ezzat

LR Parsing Data Structure Sm Sm-1 $ … a1 ai an ….. LR Parsing Program ACTION GOTO Output LR Parser Model Stack Input CS 404 Ahmed Ezzat

LR Parsing Algorithm Initially push S0 Given state S’ on top of stack, with input symbol a If (Action[S’,a] = shift S’) Push a, then S’ onto stack Move to next input symbol CS 404 Ahmed Ezzat

LR Parsing Algorithm (continue) If (Action[S’,a] = reduce AX1X2…Xn) Pop off n states (and n terminals) to find Su on top of stack Push A Push new state Goto[Su,A] Output production AX1X2…Xn If action[S’,a] = accept, done! If action[S’,a] = error, error! CS 404 Ahmed Ezzat

LR Parsing With Only States on Stack If (Action[S,a] = shift S) Push S onto stack If (Action[S,a] = reduce AX1X2…Xn) Pop off n states to find Su on top of stack CS 404 Ahmed Ezzat

END CS 404 Ahmed Ezzat