CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
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.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Top-Down Parsing.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Parsing III (Eliminating left recursion, recursive descent parsing)
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,
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.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Professor Yihjia Tsai Tamkang University
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
Chapter 6: Top-Down Parser1 Compiler Designs and Constructions Chapter 6: Top-Down Parser Objectives: Types of Parsers Backtracking Vs. Non-backtracking.
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.
410/510 1 of 21 Week 2 – Lecture 1 Bottom Up (Shift reduce, LR parsing) SLR, LR(0) parsing SLR parsing table Compiler Construction.
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?
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.
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
Parsing III (Top-down parsing: recursive descent & LL(1) )
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.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Syntax and Semantics Structure of programming languages.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
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.
Simple One-Pass Compiler
COP4020 Programming Languages Parsing Prof. Xin Yuan.
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.
Parsing Top-Down.
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.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
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 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 due on Monday February 8 th Name and date your submission Submit electronically in Homework Server.
Chapter 2: A Simple One Pass Compiler
Lecture 7 Syntax Analysis (5) Operator-Precedence Parsing
Parsing #1 Leonidas Fegaras.
Programming Languages Translator
Context free grammars Terminals Nonterminals Start symbol productions
Lecture #12 Parsing Types.
Parsing with Context Free Grammars
4 (c) parsing.
Parsing Techniques.
Compiler Design 4. Language Grammars
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Definition
COP4020 Programming Languages
Compiler Design 7. Top-Down Table-Driven Parsing
Lecture 7: Introduction to Parsing (Syntax Analysis)
Designing a Predictive Parser
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho, Sethi, Ullman aka: The Dragon Book Section 2.4 page 40 October 25, 2006

CS 310 – Fall 2006 Pacific University Parsing Can a string, s, be generated by a grammar? –does source code conform to the C grammar? For any CFG, we can parse in O(n 3 ), n = |s| –Linear algorithms, O(n), exist for languages that arise in practice –Single left to right scan with one look ahead character Top-down vs. Bottom-up –two types of parsing –describes how you construct the parse tree

CS 310 – Fall 2006 Pacific University Parsing Top-down –efficient parsers that are more easily constructed by hand –We will be concerned with these for now Bottom-up –handles a larger class of grammars –often used in software tools that produce a parser from a grammar Example A -> 0A1 A -> B B -> #

CS 310 – Fall 2006 Pacific University Top Down Parsing Create the parse tree from the top down At node n, labeled with nonterminal A, select one of the productions for A and construct children at n using the RHS of the production. Find the next node at which a subtree is to be constructed Repeat What if a we could choose from 2 productions in 1? *from Aho, Sethi, Ullman

CS 310 – Fall 2006 Pacific University Top Down Parsing For some grammars, this can be done with a single left to right scan of the input –looking at a single character at a time –the lookahead character TYPE -> SIMPLE | id | array [ SIMPLE ] of TYPE SIMPLE -> integer | char | num dotdot num * *from Aho, Sethi, Ullman

CS 310 – Fall 2006 Pacific University Let’s build the parse tree array [ num dotdot num ] of integer

CS 310 – Fall 2006 Pacific University Recursive-descent Parsing Top down parsing –execute a set of recursive procedures to parse –one procedure per nonterminal Predictive parsing –special case of Recursive-descent parsing –the lookahead character unambiguously determines how to choose the next step not all grammars will work

CS 310 – Fall 2006 Pacific University Example procedure type begin if lookahead is in { integer, char, num } then simple() else if lookahead = id then match(id); else if lookahead = array then match(array); match([); simple; match(]); match(of); type; else error endif end type TYPE -> SIMPLE | id | array [ SIMPLE ] of TYPE

CS 310 – Fall 2006 Pacific University Left Recursion T -> T a x | x what does this produce? Left Recursive Grammar This could cause us to loop forever! –we stay in the same function, T Rewrite as right recursive T -> x R R -> ax R | ε

CS 310 – Fall 2006 Pacific University First The lookahead character unambiguously determines how to choose the next step To determine this, we calculate FIRST(A) –where A is a non-terminal FIRST(A) is the set of characters that appear as the first symbols of one or more strings generated from A  For predictive parsing to work without backtracking when A -> X and A -> Y exist, FIRST(X) and FIRST(Y) must be disjoint  from Aho, Sethi, Ullman

CS 310 – Fall 2006 Pacific University First What is FIRST() for each of the nonterminals? TYPE -> SIMPLE | id | array [ SIMPLE ] of TYPE SIMPLE -> integer | char | num dotdot num *

CS 310 – Fall 2006 Pacific University Simple Parse Table Instead of a function, we can build a table to tell us how to parse. (1) S -> Q (2) S -> ( S ) (3) Q -> 1 (4) Q -> 0 ()01 S2-11 Q--43 Parse Error!