SIGCSE 2005, St. Louis, MO Design Patterns for Recursive Descent Parsing Dung Nguyen, Mathias Ricken & Stephen Wong Rice University.

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

1 of 3 Teaching OOD from the Beginning Karel is Kool Visual Feedback is Powerful – students see their mistakes/triumphs Stay within one common metaphor.
Grammar and Algorithm }
Grammar vs Recursive Descent Parser
 CS /11/12 Matthew Rodgers.  What are LL and LR parsers?  What grammars do they parse?  What is the difference between LL and LR?  Why do.
Parsing & Scanning Lecture 2 COMP /25/2004 Derek Ruths Office: DH Rm #3010.
Design Patterns for Marine Biology Simulation Dung “Zung” Nguyen Mathias Ricken Stephen Wong Rice University.
LR Parsing – The Items Lecture 10 Mon, Feb 14, 2005.
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
CS 280 Data Structures Professor John Peterson. Lexer Project Questions? Must be in by Friday – solutions will be posted after class The next project.
Chapter 2 A Simple Compiler
CS 280 Data Structures Professor John Peterson. Lexer Project Questions? Must be in by Friday – solutions will be posted after class The next project.
Lexical and syntax analysis
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Introduction.
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Top-Down Parsing - recursive descent - predictive parsing
LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.
OOP in Introductory CS Stephen Wong and “Zung” Nguyen Rice University Better students though abstraction.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.
Introduction to Compiling
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Bc. Jozef Lang (xlangj01) Bc. Zoltán Zemko (xzemko01) Increasing power of LL(k) parsers.
The Interpreter Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Patterns for Decoupling Data Structures and Algorithms or How visitors can help you grow! Stephen Wong, Oberlin College Dung “Zung” Nguyen, Pepperdine.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
Top-Down Predictive Parsing We will look at two different ways to implement a non- backtracking top-down parser called a predictive parser. A predictive.
©SoftMoore ConsultingSlide 1 Teaching Compiler Design.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CS 614: Theory and Construction of Compilers Lecture 4 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
CS 2130 Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing Warning: The precedence table given for the Wff grammar is in error.
Comp 411 Principles of Programming Languages Lecture 3 Parsing
Teaching Compiler Design
Parsing #1 Leonidas Fegaras.
A Simple Syntax-Directed Translator
Programming Languages Translator
CS510 Compiler Lecture 4.
LR Parsing – The Items Lecture 10 Fri, Feb 13, 2004.
Lecture #12 Parsing Types.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Compiler Lecture 1 CS510.
Top-Down Parsing.
Programming Language Syntax 7
Parsing & Scanning Lecture 2
CSE401 Introduction to Compiler Construction
Comp 401 Concluding Remarks
Lecture 7: Introduction to Parsing (Syntax Analysis)
Programming Language Syntax 5
Nifty Assignments: Marine Biology Simulation
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Design Patterns for Recursive Descent Parsing
LL and Recursive-Descent Parsing
Computing Follow(A) : All Non-Terminals
Kanat Bolazar February 16, 2010
Recursive descent parsing
Nonrecursive Predictive Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Lecture 18 Bottom-Up Parsing or Shift-Reduce Parsing
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Recursive descent parsing
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
Presentation transcript:

SIGCSE 2005, St. Louis, MO Design Patterns for Recursive Descent Parsing Dung Nguyen, Mathias Ricken & Stephen Wong Rice University

RDP in CS2? Context: objects-first intro curriculum which already covers Polymorphism Recursion Design patterns (visitors, factories, etc) OOD principles Want good OOP/D example Want a relevant CS topic Recursive Descent Parsing: Smooth transitions from simple to complex examples, developing abstract model ∆ change in grammar  ∆ change in code

The Problem of Teaching RDP “ A c o m p l e x, i s o l a t e d, a d v a n c e d t o p i c f o r u p p e r d i v i s i o n o n l y ” Parser generator ?

Object-Oriented Approach Grammar must drive any processing related to it, e.g. parsing.  Model the grammar first: Terminal symbols (tokens) Non-Terminal symbols (incl. start symbol) Rules Driving forces Decouple intelligent tokens from rules  visitors to tokens Extensible system: open ended number of tokens  extended visitors Then Parsing will come!

Representing Tokens Intelligent Tokens  No type checking! Decoupled from processing  Visitor pattern For LL(1) grammars, in any given situation, the token determines the parsing action taken  Parsing is done by visitors to tokens

Processing Tokens with Visitors Token A Token B Visitor caseA caseB visits Standard Visitor Pattern: calls

caseB VisitorB Processing Tokens with Visitors Token A Token B Visitor caseA visits Visitor Pattern modified with Chain-of-Responsibility: visits chain VisitorB defaultCase caseB VisitorA defaultCase caseB caseA visits calls delegates to

Modeling an LL(1) Grammar Left-Factoring Make grammar predictively parsable EE E1  + EFF | FF empty | ¤ E1 num | id

FF Modeling an LL(1) Grammar In multiple rules (branches), replace sequences and tokens with unique non- terminal symbols Branches only contain non-terminals EE E1  F FF empty | E1 + E numid |E1a  F1  F2  E1a F1 F2 numid |

Modeling an LL(1) Grammar Branches modeled by inheritance (“is-a”) Sequences modeled by composition (“has-a”) A  B | C S  X Y

Object Model of Grammar E  F E1 E1  empty | E1a E1a  + E F  F1 | F2 F1  num F2  id Grammar Structure = Class Structure

Modeling an LL(1) Grammar

Detailed and Global Analysis FF EE E1  F empty | E1 + E num id E1a  F1  F2  E1a F1 | E1a F1 F2 To process E, we must first know about F and E1… But to process F, we must first know about F1 and F2… but to process F1, we must first know about num! The processing of one rule requires deep knowledge of the whole grammar! Or does it??... Abstract and Local Analysis! To process E, we must have the ability to process F and E1, independent of how either F or E1 are processed! Since parsing is done with visitors to tokens, all we need to parse E are the visitors to parse F and E1. But E doesn’t know what it takes to make the F and E1 parsing visitors… We need abstract construction of the visitors…

Factory Model of Parser E  F E1 E1  empty | E1a E1a  + E F  F1 | F2 F1  num F2  id Parser Structure = Factory Structure Grammar represented purely with composition

Extending the Grammar Adding new tokens and rules Highly localized impact on code No re-computing of prediction tables

E  S E1 E1  empty | E1a E1a  + E S  P | T P  (E) T  F T1 T1  empty | T1a T1a  * S F  F1 | F2 F1  num F2  id E  F E1 E1  empty | E1a E1a  + E F  F1 | F2 F1  num F2  id

Parser Demo (If time permits) We change your grammar in two minutes while you wait! gram

Automatic Parser Generator No additional theory needed for generalization No fixed-points, FIRST and FOLLOWS sets Kooprey Parser generator: BNF  Java kou·prey (noun): “a rare short-haired ox (Bos sauveli) of forests of Indochina […]” (Merriam-Webster Online) Extensions Skip generation of source, create parser at runtime

Conclusion Simple enough to introduce in CS2 course – near end of CS2) Teaches an abstraction of grammars and parsing Reinforces foundational OO principles Abstract representations Abstract construction Decoupled systems Recursion