ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Similar documents
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS. Regrades 10/6/15. Prelim 1. Prelim 1. Expression trees. Pointers to material

syntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)

If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

syntax tree - * * * * * *

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

Finish Lec12 TREES, PART 2. Announcements. JavaHyperText topics. Trees, re-implemented. Iterate through data structure 3/7/19

Announcements. Application of Recursion. Motivation. A Grammar. A Recursive Grammar. Grammars and Parsing

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Application of recursion. Grammars and Parsing. Recursive grammar. Grammars

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

Stacks, Queues and Hierarchical Collections

Introduction to Parsing. Lecture 8

CSE 3302 Programming Languages Lecture 2: Syntax

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

COMP 250 Fall binary trees Oct. 27, 2017

TREES. Trees - Introduction

Context-Free Grammars

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Lecture 12 TREES II CS2110 Spring 2018

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Lecture 8: Deterministic Bottom-Up Parsing

Principles of Programming Languages COMP251: Syntax and Grammars

Introduction to Parsing. Lecture 5

Outline. Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Lecture 7: Deterministic Bottom-Up Parsing

Building Compilers with Phoenix

Lecture Chapter 6 Recursion as a Problem Solving Technique

Ambiguity. Lecture 8. CS 536 Spring

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Expressions and Assignment

A programming language requires two major definitions A simple one pass compiler

Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Introduction to Parsing. Lecture 5

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Lexical Scanning COMP360

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Peace cannot be kept by force; it can only be achieved by understanding. Albert Einstein

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

Lexical and Syntax Analysis. Top-Down Parsing

Compiling Regular Expressions COMP360

Introduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Lexical and Syntax Analysis

Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

CSE 373 Spring Midterm. Friday April 21st

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

CSE 401 Midterm Exam Sample Solution 2/11/15

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Compiler Design Concepts. Syntax Analysis

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

12 Abstract Data Types

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Principles of Programming Languages COMP251: Syntax and Grammars

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

Supplemental Materials: Grammars, Parsing, and Expressions. Topics. Grammars 10/11/2017. CS2: Data Structures and Algorithms Colorado State University

Syntax-Directed Translation. Lecture 14

CS W3134: Data Structures in Java

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

MIT Top-Down Parsing. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

More Assigned Reading and Exercises on Syntax (for Exam 2)

Parsing. Parsing. Bottom Up Parsing. Bottom Up Parsing. Bottom Up Parsing. Bottom Up Parsing

CSE 12 Abstract Syntax Trees

Question Points Score

Tree Applications. Processing sentences (computer programs or natural languages) Searchable data structures

Compiler Theory. (Semantic Analysis and Run-Time Environments)

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Question Bank. 10CS63:Compiler Design

Formal Languages and Automata Theory, SS Project (due Week 14)

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

A simple syntax-directed

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

Related Course Objec6ves

EECS 6083 Intro to Parsing Context Free Grammars

Parsing II Top-down parsing. Comp 412

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Syntax and Grammars 1 / 21

A Simple Syntax-Directed Translator

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

Name CPTR246 Spring '17 (100 total points) Exam 3

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Bottom-Up Parsing. Lecture 11-12

Transcription:

1 Prelim 1 2 Where: Kennedy Auditorium When: A-Lib: 5:30-7 Lie-Z: 7:30-9 (unless we explicitly notified you otherwise) ADS, GRAMMARS, PARSING, R RAVRSALS Lecture 13 CS2110 Spring 2016 Pointers to material xpression trees 3 4 Parse trees: text, section 23.36 Definition of Java Language, sometimes useful: docs.oracle.com/javase/specs/jls/se8/html/index.html Grammar for most of Java, for those who are curious: docs.oracle.com/javase/specs/jls/se8/html/jls-18.html ree traversals preorder, inorder, postorder: text, sections 23.13.. 23.15. Can draw a tree for 2 3 (1 2 4) - 2 3 1 2 4 public abstract class xp { / return the value of this xp / public abstract int eval(); xpression trees 6 tree for (2 3) (1 4) 5 public abstract class xp { / return the value of this xp / public abstract int eval(); 2 3 2 3 1 4 public class Int extends xp { int v; public int eval() { return v; public class Add extends xp { xp left; xp right; public int eval() { return left.eval() right.eval(); Preorder traversal: 1. Visit the root 2. Visit left subtree, in preorder 3. Visit right subtree, in preorder 2 3 1 4 prefix and postfix notation proposed by Jan Lukasiewicz in 1951 Postfix (we see it later) is often called RPN for Reverse Polish Notation 1

7 tree for (2 3) (1 4) 8 tree for (2 3) (1 4) In about 1974, Gries paid $300 for an HP calculator, which had some memory and used postfix notation! Still works. Come up to see it. 2 3 Postorder traversal: 1. Visit left subtree, in postorder 2. Visit right subtree, in postorder 3. Visit the root 2 3 1 4 Postfix notation 1 4 Postfix is easy to compute. Process elements left to right. Number? Push it on a stack Binary operator? Remove two top stack elements, apply operator to it, push result on stack Unary operator? Remove top stack element, apply operator to it, push result on stack 2 3 1 4 Postfix notation 2 3 1 4 9 tree for (2 3) (1 4) 2 3 1 4 10 xpression trees public class Add extends xp { xp left; xp right; / Return the value of this exp. / public int eval() {return left.eval() right.eval(); public abstract class xp { public abstract int eval(); public abstract String pre(); public abstract String post(); Inorder traversal: 1. Visit left subtree, in inorder 2. Visit the root 3. Visit right subtree, in inorder o help out, put parens around expressions with operators / Return the preorder./ public String pre() {return left.pre() right.pre(); / Return the postorder./ public String post() {return left.post() right.post() ; (2 3) (1 4) Motivation for grammars A Grammar 11 he cat ate the rat. he cat ate the rat slowly. he small cat ate the big rat slowly. he small cat ate the big rat on the mat slowly. he small cat that sat in the hat ate the big rat on the mat slowly, then got sick. Not all sequences of words are legal sentences he ate cat rat the How many legal sentences are there? How many legal Java programs? How do we know what programs are legal? http://docs.oracle.com/javase/specs/jls/se8/html/index.html 12 Noun Verb Noun Noun boys Noun girls Noun bunnies Verb like see White space between words does not matter A very boring grammar because the set of s is finite (exactly 18 sentences) Our sample grammar has these rules: A can be a Noun followed by a Verb followed by a Noun A Noun can be boys or girls or bunnies A Verb can be like or see 2

A Grammar A recursive grammar 13 Noun Verb Noun Noun boys Noun girls Noun bunnies Verb like Verb see Grammar: set of rules for generating sentences of a language. xamples of : girls see bunnies bunnies like boys he words boys, girls, bunnies, like, see are called tokens or terminals he words, Noun, Verb are called nonterminals 14 Noun Noun Noun Verb and or Noun Verb Noun boys girls bunnies like see his grammar is more interesting than previous one because the set of s is infinite What makes this set infinite? Answer: Recursive definition of Detour s with periods 15 What if we want to add a period at the end of every sentence? and. or. Noun Verb Noun. Noun Does this work? No! his produces sentences like: girls like boys. and boys like bunnies.. 16 Punctuated. and or Noun VerbNoun New rule adds a period only at end of sentence. Noun boys okens are the 7 words plus Noun girls the period (.) Noun bunnies Grammar is ambiguous: Verb like boys like girls Verb see and girls like boys or girls like bunnies Grammars for programming languages Grammar for simple expressions (not the best) 17 18 Grammar describes every possible legal expression You could use the grammar for Java to list every possible Java program. (It would take forever.) Grammar tells the Java compiler how to parse a Java program docs.oracle.com/javase/specs/jls/se8/html/jls-2.html#jls-2.3 integer ( ) Simple expressions: An can be an integer. An can be ( followed by an followed by followed by an followed by ) Set of expressions defined by this grammar is a recursively-defined set Is language finite or infinite? Do recursive grammars always yield infinite languages? Some legal expressions: 2 (3 34) ((423) 89) Some illegal expressions: (3 3 4 okens of this grammar: ( ) and any integer 3

19 Use a grammar in two ways: A grammar defines a language (i.e. the set of properly structured sentences) Parsing A grammar can be used to parse a sentence (thus, checking if a string is asentence is in the language) o parse a sentence is to build a parse tree: much like diagramming a sentence integer ( ) xample: Show that ((423) 89) is a valid expression by building a parse tree ( ) ( ) 4 23 89 20 20 Grammar is ambiguous if it allows two parse trees for a sentence. he grammar below, using no parentheses, is ambiguous. he two parse trees to right show this. We don t know which to evaluate first in the expression 1 2 3 integer Ambiguity 1 2 1 2 3 3 21 Recursive descent parsing Write a set of mutually recursive methods to check if a sentence is in the language (show how to generate parse tree later). One method for each nonterminal of the grammar. he method is completely determined by the rules for that nonterminal. On the next pages, we give a high-level version of the method for nonterminal : integer ( ) 22 Parsing an integer ( ) / Unprocessed input starts an. Recognize that, throwing away each piece from the input as it is recognized. Return false if error is detected and true if no errors. Upon return, processed tokens have been removed from input. / public boolean parse() before call: already processed unprocessed ( 2 ( 4 8 ) 9 ) after call: already processed unprocessed (call returns true) ( 2 ( 4 8 ) 9 ) Specification: / Unprocessed input starts an. / 23 public boolean parse() { integer ( ) if (first token is an integer) remove it from input and return true; if (first token is not ( ) return false else remove it from input; if (!parse()) return false; if (first token is not ) return false else remove it from input; if (!parse()) return false; if (first token is not ) ) return false else remove it from input; return true; 24 Illustration of parsing to check syntax integer ( ) ( 1 ( 2 4 ) ) 4

25 he scanner constructs tokens An object scanner of class Scanner is in charge of the input String. It constructs the tokens from the String as necessary. e.g. from the string 1464634 build the token 1464, the token, and the token 634. It is ready to work with the part of the input string that has not yet been processed and has thrown away the part that is already processed, in left-to-right fashion. already processed unprocessed ( 2 ( 4 8 ) 9 ) Change parser to generate a tree integer 26 / Return a ree for the if no error. ( ) Return null if there was an error/ public ree parse() { if (first token is an integer) remove it from input and return true; if (first token is an integer) { ree t= new ree(the integer); Remove token from input; return t; Change parser to generate a tree Code for a stack machine 27 / Return a ree for the if no error. Return null if there was an error/ public ree parse() { if (first token is an integer) ; if (first token is not ( ) return null else remove it from input; ree t1= parse(); if (t1 == null) return null; if (first token is not ) return null else remove it from input; ree t2= parse(); if (t2 == null) return null; if (first token is not ) ) return false else remove it from input; return new ree(t1,, t2); integer ( ) 28 Code for 2 (3 4) PUSH 2 PUSH 3 PUSH 4 : remove two top values from stack, add them, and place result on stack It s postfix notation! 2 3 4 4 37 2 S t a c k Code for a stack machine Use parser to generate code for a stack machine 29 30 Code for 2 (3 4) PUSH 2 PUSH 3 PUSH 4 : remove two top values from stack, add them, and place result on stack 7 29 S t a c k Code for 2 (3 4) PUSH 2 PUSH 3 PUSH 4 : remove two top values from stack, add them, and place result on stack parse can generate code as follows: or integer i, return string PUSH i \n or (1 2), return a string containing Code for 1 Code for 2 \n It s postfix notation! 2 3 4 It s postfix notation! 2 3 4 5

Grammar that gives precedence to over Does recursive descent always work? 31 -> { -> { -> integer -> ( ) 2 3 4 says do first Notation: { xxx means 0 or more occurrences of xxx. : xpression : erm : actor 2 3 4 ry to do first, can t complete tree 32 Some grammars cannot be used for recursive descent rivial example (causes infinite recursion): S b S Sa Can rewrite grammar S b S ba A a A aa or some constructs, recursive descent is hard to use Other parsing techniques exist take the compiler writing course 6