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.

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

Semantics Static semantics Dynamic semantics attribute grammars
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program,
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.
Parsing III (Eliminating left recursion, recursive descent parsing)
Environments and Evaluation
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
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.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
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.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Lecture 14 Syntax-Directed Translation Harry Potter has arrived in China, riding the biggest initial print run for a work of fiction here since the Communist.
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.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 12 CS2110 – Spring
Binary Trees. Node structure Data A data field and two pointers, left and right.
Parser construction tools: YACC
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
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.
Information and Computer Sciences University of Hawaii, Manoa
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
A grammar for arithmetic expressions involving the four basic operators and parenthesized expressions. Parenthesized expressions have the highest precedence.
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CPS 506 Comparative Programming Languages Syntax Specification.
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.
Syntax (2).
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.
Parser Generation Using SLK and Flex++ Copyright © 2015 Curt Hill.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Parsing III (Top-down parsing: recursive descent & LL(1) )
MiniJava Compiler A multi-back-end JIT compiler of Java.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
Parsing 2 of 4: Scanner and Parsing
ASTs, Grammars, Parsing, Tree traversals
COMP261 Lecture 18 Parsing 3 of 4.
Building AST's for RPAL Programs
A Simple Syntax-Directed Translator
Introduction to Parsing (adapted from CS 164 at Berkeley)
Trees.
Recursive descent parsing
Syntax-Directed Translation
Top-Down Parsing CS 671 January 29, 2008.
ADTs, Grammars, Parsing, Tree traversals
Recursive descent parsing
Recognizers 1-Jan-19.
Recognizers 16-Jan-19.
Stacks.
Recognizers 22-Feb-19.
Building AST's for RPAL Programs
Trees.
The Recursive Descent Algorithm
Recursive descent parsing
Recursive descent parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CMPE 152: Compiler Design December 4 Class Meeting
Stacks.
Presentation transcript:

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 away unnecessary information (comments, whitespace, punctuation used only for disambiguation, etc.) It represents syntax, not semantics However, well-written syntax can sometimes help with the semantics For example, ::= | + | * is technically correct, but gives us no help in recognizing that multiplication has precedence over addition The program is represented as a tree 2

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

From Recognizer to Parser First, make a Recognizer The Recognizer code will form the “skeleton” of your Parser Create a Stack > as a globally available instance variable You will use this to hold the trees as you build them The methods from the Recognizer all return a boolean ; do not change this Your new results will go onto the Stack Each time you recognize something, also build a Tree to represent it, and put this Tree onto the Stack Most of the time, you will assemble the new Tree from the two Tree s you most recently put onto the Stack

Example: Unary minus A simple example of an is -5 As the program steps through the code for, it puts – (minus) on the stack, then it puts 5 on the stack Each of these is in the form of a Tree consisting of a single node with a Token as its value Note that the most recently parsed item, 5, is on the top of the stack These can be combined into a new Tree, with the minus as the root and the 5 as its child

makeTree /** * Removes two Trees from the stack, makes a new Tree and * puts it on the stack. The element on the top of the stack * (the most recently seen element) is made the child of the * element beneath it (which was seen earlier). */ private void makeTree() { Tree child = stack.pop(); Tree parent = stack.pop(); parent.addChild(child); stack.push(parent); }

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); return true; } } error("Error in \"while\" statement"); } return false; } while conditionblock

makeTree private void makeTree(int rootIndex, int... childIndices) { // Get root from stack Tree root = getStackItem(rootIndex); // Get other trees from stack and add them as children of root for (int i = 0; i getStackItem(int n) { return stack.get(stack.size() - n); }

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; }

Tricky problem: Defining ::= “*” | (For simplicity, I’m ignoring the “/” and “%” operators) This is logically correct, but it defines the wrong tree for expressions such as x * y * z -- treats it as x * (y * z) ::= “*” | This is equally correct, and correctly defines x * y * z as meaning (x * y) * z However, the left recursion can’t be programmed: “To recognize a term, first recognize a term” Solution: ::= { “*” } This turns the recursion into an iteration The result is easy to program correctly

Code for term() public boolean term() { if (!factor()) return false; while (multiplyOperator()) { if (!factor()) { error("No term after '*' or '/'"); } makeTree(2, 3, 1); // *, first factor, second factor } return true; } Here’s a snippet of code from my JUnit test methods: use("x * y * z"); assertTrue(parser.term()); assertTree("*(*(x, y), z)"); use(String) and assertTree(String) are helper methods that I’ve written; you should be able to figure out what they do

An isCommand() method public boolean command() { if (action()) return true; if (thought()) 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 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"));

17 The End