Lecture #12 Parsing Types.

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Chapter 2-2 A Simple One-Pass Compiler
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.
Chapter 3 Syntax Analysis
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Lecture # 11 Grammar Problems.
Mooly Sagiv and Roman Manevich School of Computer Science
LR Parsing – The Items Lecture 10 Mon, Feb 14, 2005.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem 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,
Top-Down Parsing.
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.
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
Chapter 3 Chang Chi-Chung Parse tree intermediate representation The Role of the Parser Lexical Analyzer Parser Source Program Token Symbol.
Syntax and Semantics Structure of programming languages.
1 Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
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?
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Syntax and Semantics Structure of programming languages.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Simple One-Pass Compiler
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
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)
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Chapter 2: A Simple One Pass Compiler
Syntax and Semantics Structure of programming languages.
CSE 3302 Programming Languages
Parsing #1 Leonidas Fegaras.
4.1 Introduction - Language implementation systems must analyze
Chapter 4 - Parsing CSCE 343.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
LR Parsing – The Items Lecture 10 Fri, Feb 13, 2004.
Compiler design Bottom-up parsing Concepts
Lexical and Syntax Analysis
Context free grammars Terminals Nonterminals Start symbol productions
Unit-3 Bottom-Up-Parsing.
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
Compiler Construction
Syntax Analysis Part I Chapter 4
Context-free Languages
Parsing with Context Free Grammars
CS 404 Introduction to Compiler Design
4 (c) parsing.
Parsing Techniques.
Lexical and Syntax Analysis
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Definition
Recursive decent parsing
Chapter 2: A Simple One Pass Compiler
COP4020 Programming Languages
Compiler Design 7. Top-Down Table-Driven Parsing
Parsing IV Bottom-up Parsing
Chapter 4: Lexical and Syntax Analysis Sangho Ha
Syntax Analysis - Parsing
Nonrecursive Predictive Parsing
Predictive Parsing Program
4.1 Introduction - Language implementation systems must analyze
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Lecture #12 Parsing Types

Parsing Parsing = process of determining if a string of tokens can be generated by a grammar For any CF grammar there is a parser that takes at most O(n3) time to parse a string of n tokens Linear algorithms suffice for parsing programming language source code Top-down parsing “constructs” a parse tree from root to leaves Bottom-up parsing “constructs” a parse tree from leaves to root

Parsing Top-down (C-F grammar with restrictions) Recursive descent (predictive parsing) LL (Left-to-right, Leftmost derivation) methods Bottom-up (C-F grammar with restrictions) Operator precedence parsing LR (Left-to-right, Rightmost derivation) methods SLR, canonical LR, LALR

Predictive Parsing Recursive descent parsing is a top-down parsing method Every nonterminal has one (recursive) procedure responsible for parsing the nonterminal’s syntactic category of input tokens When a nonterminal has multiple productions, each production is implemented in a branch of a selection statement based on input look-ahead information Predictive parsing is a special form of recursive descent parsing where we use one lookahead token to unambiguously determine the parse operations

Example Predictive Parser (Grammar) type  simple |^ id | array [ simple ] of type simple  integer | char | num dotdot num

Example Predictive Parser (Program Code) procedure match(t : token); begin if lookahead = t then lookahead := nexttoken() else error() end; procedure type(); begin if lookahead in { ‘integer’, ‘char’, ‘num’ } then simple() else if lookahead = ‘^’ then match(‘^’); match(id) else if lookahead = ‘array’ then match(‘array’); match(‘[‘); simple(); match(‘]’); match(‘of’); type() else error() end; procedure simple(); begin if lookahead = ‘integer’ then match(‘integer’) else if lookahead = ‘char’ then match(‘char’) else if lookahead = ‘num’ then match(‘num’); match(‘dotdot’); match(‘num’) else error() end;

Example Predictive Parser (Execution Step 1) type() Check lookahead and call match match(‘array’) Input: array [ num dotdot num ] of integer lookahead

Example Predictive Parser (Execution Step 2) type() match(‘array’) match(‘[’) Input: array [ num dotdot num ] of integer lookahead

Example Predictive Parser (Execution Step 3) type() match(‘array’) match(‘[’) simple() match(‘num’) Input: array [ num dotdot num ] of integer lookahead

Example Predictive Parser (Execution Step 4) type() match(‘array’) match(‘[’) simple() match(‘num’) match(‘dotdot’) Input: array [ num dotdot num ] of integer lookahead

Example Predictive Parser (Execution Step 5) type() match(‘array’) match(‘[’) simple() match(‘num’) match(‘dotdot’) match(‘num’) Input: array [ num dotdot num ] of integer lookahead

Example Predictive Parser (Execution Step 6) type() match(‘array’) match(‘[’) simple() match(‘]’) match(‘num’) match(‘dotdot’) match(‘num’) Input: array [ num dotdot num ] of integer lookahead

Example Predictive Parser (Execution Step 7) type() match(‘array’) match(‘[’) simple() match(‘]’) match(‘of’) match(‘num’) match(‘dotdot’) match(‘num’) Input: array [ num dotdot num ] of integer lookahead

Example Predictive Parser (Execution Step 8) type() match(‘array’) match(‘[’) simple() match(‘]’) match(‘of’) type() match(‘num’) match(‘dotdot’) match(‘num’) simple() match(‘integer’) Input: array [ num dotdot num ] of integer lookahead

FIRST FIRST() is the set of terminals that appear as the first symbols of one or more strings generated from  type  simple | ^ id | array [ simple ] of type simple  integer | char | num dotdot num FIRST(simple) = { integer, char, num } FIRST(^ id) = { ^ } FIRST(type) = { integer, char, num, ^, array }

How to use FIRST We use FIRST to write a predictive parser as follows procedure rest(); begin if lookahead in FIRST(+ term rest) then match(‘+’); term(); rest() else if lookahead in FIRST(- term rest) then match(‘-’); term(); rest() else return end; expr  term rest rest  + term rest | - term rest |  When a nonterminal A has two (or more) productions as in A   |  Then FIRST () and FIRST() must be disjoint for predictive parsing to work

Predictive Parsing Eliminate left recursion from grammar Left factor the grammar Compute FIRST and FOLLOW Two variants: Recursive (recursive-descent parsing) Non-recursive (table-driven parsing)

Predictive Parsing Program (Driver) push($) push(S) a := lookahead repeat X := pop() if X is a terminal or X = $ then match(X) // moves to next token and a := lookahead else if M[X,a] = X  Y1Y2…Yk then push(Yk, Yk-1, …, Y2, Y1) // such that Y1 is on top … invoke actions and/or produce IR output … else error() endif until X = $