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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
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.
More on Recursive Recursion vs. Iteration Why Recursion?
Recursion Gordon College CPS212
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.
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.
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.
Grammars and Parsing. Sentence  Noun Verb Noun Noun  boys Noun  girls Noun  dogs Verb  like Verb  see Grammars Grammar: set of rules for generating.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
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.
LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.
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.
PART I: overview material
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Recursive Descent Parsers Read and recognize the input (in order to translate it or evaluate it) Implicitly construct the derivation tree Design is driven.
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.
Languages and Grammars. A language is a set of strings. Example: The set of all valid C++ programs is a language. Each program consists of a string of.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
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.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS 153: Concepts of Compiler Design September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
Recursion Powerful Tool
Grammars and Parsing.
Parsing and Parser Parsing methods: top-down & bottom-up
Unit-3 Bottom-Up-Parsing.
PROGRAMMING LANGUAGES
Cse 373 April 14th – TreEs pt 2.
Syntax versus Semantics
Recognizers 13-Sep-18.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 20: Binary Trees.
Recursive descent parsing
Programming Language Syntax 6
Compiler Design 4. Language Grammars
Top-Down Parsing CS 671 January 29, 2008.
Programming Language Syntax 2
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Recursive descent parsing
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Compiler Construction
Recognizers 1-Jan-19.
Recognizers 16-Jan-19.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks.
Recognizers 22-Feb-19.
CSC 4181 Compiler Construction Context-Free Grammars
Programming Languages
BNF 9-Apr-19.
Recursive descent parsing
Recursive descent parsing
Recursive descent parsing
Recursive descent parsing
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

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 return boolean values, but in addition, if they succeed they should leave a Tree on the stack The stack should be an instance variable of the Parser Here’s one easy way to do it: Create a new Parser(String s) for each String you want to use, and call some parsing method ( term, command,...) for that particular String

Recognizer helper methods private boolean number() { return nextTokenMatches(Token.NUMBER); } private boolean name() { return nextTokenMatches(Token.NAME); } private boolean keyword(String expectedKeyword) { return nextTokenMatches(Token.KEYWORD, expectedKeyword); } private boolean symbol(String expectedSymbol) { return nextTokenMatches(Token.SYMBOL, expectedSymbol); }

nextTokenMatches private boolean nextTokenMatches(int type) { Token t = tokenizer.next(); if (t.getType() == type) { return true; } else tokenizer.pushBack(t); return false; } private boolean nextTokenMatches(int type, String value) { Token t = tokenizer.next(); if (type == t.getType() && value.equals(t.getValue())) { return true; } else tokenizer.pushBack(t); return false; }

Revised nextTokenMatches private boolean nextTokenMatches(int type) { Token t = tokenizer.next(); if (t.getType() == type) { stack.push(new Tree(t)); return true; } else tokenizer.pushBack(t); return false; } private boolean nextTokenMatches(int type, String value) { Token t = tokenizer.next(); if (type == t.getType() && value.equals(t.getValue())) { stack.push(new Tree(t)); return true; } else tokenizer.pushBack(t); return false; }

comparator ::= " “ public boolean comparator() { if (symbol(" ")) return true; return false; } Whatever a comparator is found, one Tree node (with the Token representing that comparator as its value) is left on the stack Since we recognized one thing, and we leave one thing on the stack, this method does not need to be changed in any way

while command public boolean whileCommand() { if (keyword("while")) { if (condition()) { if (block()) { return true; } } error("Error in \"while\" statement"); } return false; } while condition block We want to change this: command Into this: makeTree(3, 2, 1); The makeTree method will take three things off the stack and replace them with one thing

Counting I like to count this way: while condition block This way is easier to implement: while condition block I prefer the first way because, although it means trickier arithmetic in makeTree, it simplifies counting everywhere else while condition block

Accessing the Stack A stack, as a stack, has no methods for accessing the n th thing from the top, but... class Stack extends Vector...and Vector has an elementAt(int index) operator This represents a stack with five things in it The “ e ” is at the top of the stack This means that, with some annoying arithmetic, you can access any element of the stack you like, counting elementAt(vector.size() - 1) as the top abcde

Recursion in BNF ::= | “*” is bad, because of the left recursion Implemented in the obvious way, it would cause our program to go into an infinite recursion ::= | “*” has another problem Implemented in the obvious way, it will build a tree of the wrong shape ::= { “*” } works well Implemented in the obvious way—iteratively, not recursively—it builds a tree of the correct shape

term() public boolean term() { if (!factor()) return false; while (multiplyOperator()) { if (!factor()) error("No term after '*' or '/'"); makeTree(2, 3, 1); } return true; }

The End