26-Jun-15 Recursive descent parsing. The Stack One easy way to do recursive descent parsing is to have each parse method take the tokens it needs, build.

Slides:



Advertisements
Similar presentations
AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
16-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
17-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
CS 330 Programming Languages 09 / 23 / 2008 Instructor: Michael Eckmann.
27-Jun-15 Recursive descent parsing. The Stack One easy way to do recursive descent parsing is to have each parse method take the tokens it needs, build.
28-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
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.
14-Jul-15 Parser Hints. The Stack To turn a “Recognizer” into a “Parser,” we need the use of a Stack All boolean Recognizer methods should continue to.
14-Jul-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Grammars and Parsing. Sentence  Noun Verb Noun Noun  boys Noun  girls Noun  dogs Verb  like Verb  see Grammars Grammar: set of rules for generating.
Parsing. Goals of Parsing Check the input for syntactic accuracy Return appropriate error messages Recover if possible Produce, or at least traverse,
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.
AN IMPLEMENTATION OF A REGULAR EXPRESSION PARSER
CS 280 Data Structures Professor John Peterson. How Does Parsing Work? You need to know where to start (“statement”) This grammar is constructed so that.
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
COMP Parsing 2 of 4 Lecture 22. How do we write programs to do this? The process of getting from the input string to the parse tree consists of.
C Chuen-Liang Chen, NTUCS&IE / 77 TOP-DOWN PARSING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Recursive descent parsing 12-Nov-15. Abstract Syntax Trees (ASTs) An AST is a way of representing a computer program It is abstract because it throws.
22-Nov-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
Parsing Top-Down.
GRAMMARS & PARSING Lecture 8 CS2110 – Spring If you are going to form a group for A2, please do it before tomorrow (Friday) noon.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
More Parsing CPSC 388 Ellen Walker Hiram College.
5-Jan-16 Recursive descent parsing. Some notes on recursive descent The starter code that I gave you did not exactly fit the grammar that I gave you Both.
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.
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.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Error recovery in predictive parsing An error is detected during the predictive parsing when the terminal on top of the stack does not match the next input.
Grammars and Parsing.
Building AST's for RPAL Programs
Chapter 4 - Parsing CSCE 343.
Bottom-up parsing Goal of parser : build a derivation
Lecture #12 Parsing Types.
Recursive Descent Parsing
Recognizers 13-Sep-18.
Syntax One - Hybrid CMSC 331.
Recursive descent parsing
Lexical and Syntax Analysis
Recursive Descent Parsing
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
CSE 3302 Programming Languages
ADTs, Grammars, Parsing, Tree traversals
Recursive descent parsing
CSC 4181Compiler Construction Context-Free Grammars
Recognizers 1-Jan-19.
Recognizers 16-Jan-19.
Recognizers 22-Feb-19.
Building AST's for RPAL Programs
Syntax Analysis - Parsing
CSC 4181 Compiler Construction Context-Free Grammars
The Recursive Descent Algorithm
Recursive descent parsing
Recursive descent parsing
Chapter 10: Compilers and Language Translation
CO4301 – Advanced Games Development Week 3 Parsing Continued
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
CMPE 152: Compiler Design December 4 Class Meeting
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
Presentation transcript:

26-Jun-15 Recursive descent parsing

The Stack One easy way to do recursive descent parsing is to have each parse method take the tokens it needs, build a parse tree, and put the parse tree on a global stack Write a parse method for each nonterminal in the grammar Each parse method should get the tokens it needs, and only those tokens Those tokens (usually) go on the stack Each parse method may call other parse methods, and expect those methods to leave their results on the stack Each (successful) parse method should leave one result on the stack

Example: while statement ::= “while” The parse method for a does this: Calls the Tokenizer, which returns a “while” token Makes the “while” into a Tree, which it puts on the stack Calls the parser for, which parses a condition and puts a Tree representation of that condition on the stack Stack now contains: “while” (stack “top” is on the right), where stands for some created Tree Calls the parser for, which parses a block and puts a Tree representation of that block on the stack Stack now contains: “while” Pops the top three things from the stack, assembles them into a Tree representing a while statement, and pushes this Tree onto the stack

Sample Java code public boolean whileCommand() { if (keyword("while")) { if (condition()) { if (block()) { makeTree(3, 2, 1); // or some such return true; } } error("Error in \"while\" statement"); } return false; }

Fancier error messages public boolean whileCommand() { if (keyword("while")) { if (condition()) { if (block()) { makeTree(3, 2, 1); // or some such return true; } error("Error in \"while\" block"); } error("Error in \"while\" condition"); } return false; }

Alternative code public boolean whileCommand() { if (keyword("while") && condition() && block()) { makeTree(3, 2, 1); // or some such return true; } return false; } No room for an error condition in this code

Alternative code with one message public boolean whileCommand() { if (keyword("while")) { if (condition()) && (block()) { makeTree(3, 2, 1); // or some such return true; } error("Error in \"while\" statement"); } return false; }

Parser methods In the BNF, I have one long definition for ::= "move" | "penup" | "pendown" | "color"... In my code, I broke that into multiple methods ::= | | |...

My command() method public boolean command() { if (move()) return true; if (penup()) return true; if (pendown()) return true;...

My helper methods I wrote a number of helper methods for the Parser and for the ParserTest classes One very useful method is tree, in the ParserTest class tree just takes three Objects and builds a tree from them This method lets me build parse trees for use in assertEquals tests Another is assertStackTop, which is just private void assertStackTop(Tree bt) { assertEquals(bt, parser.stack.peek()); } Examples: Tree condition = tree("=", "2", "2"); assertStackTop(tree("if", condition, "list"));

The End