ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

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

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

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

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

GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

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

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

syntax tree - * * * * * *

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

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

Stacks, Queues and Hierarchical Collections

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

CSE 3302 Programming Languages Lecture 2: Syntax

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.

COMP 250 Fall binary trees Oct. 27, 2017

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

Principles of Programming Languages COMP251: Syntax and Grammars

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

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

TREES. Trees - Introduction

Lexical Scanning COMP360

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

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

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

Lecture Chapter 6 Recursion as a Problem Solving Technique

([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.

Compiling Regular Expressions COMP360

Introduction to Parsing. Lecture 8

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

CS W3134: Data Structures in Java

CS2383 Programming Assignment 3

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

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

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

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

This book is licensed under a Creative Commons Attribution 3.0 License

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

Compiler Design Concepts. Syntax Analysis

Expressions and Assignment

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Lecture 12 TREES II CS2110 Spring 2018

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Lecture 7: Deterministic Bottom-Up Parsing

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

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

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

CPS 506 Comparative Programming Languages. Syntax Specification

Lecture 8: Deterministic Bottom-Up Parsing

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

Introduction to Parsing. Lecture 5

PA3 Design Specification

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

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

A simple syntax-directed

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

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

Building Compilers with Phoenix

TREES Lecture 10 CS2110 Spring2014

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

infix expressions (review)

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

CS61B Lecture #20: Trees. Last modified: Wed Oct 12 12:49: CS61B: Lecture #20 1

A Simple Syntax-Directed Translator

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

Context-Free Grammars

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

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

CSE 401 Midterm Exam Sample Solution 2/11/15

An Introduction to Trees

Principles of Programming Languages COMP251: Syntax and Grammars

CS 314 Principles of Programming Languages

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

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

announcements CSE 311: Foundations of Computing review: regular expressions review: languages---sets of strings

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

Lexical and Syntax Analysis. Top-Down Parsing

12 Abstract Data Types

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

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

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

EECS 6083 Intro to Parsing Context Free Grammars

Non-deterministic Finite Automata (NFA)

Theory and Compiling COMP360

Ambiguity. Lecture 8. CS 536 Spring

CS164: Midterm I. Fall 2003

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

Parsing III. (Top-down parsing: recursive descent & LL(1) )

Postfix (and prefix) notation

2.2 Syntax Definition

Syntactic Analysis. Top-Down Parsing. Parsing Techniques. Top-Down Parsing. Remember the Expression Grammar? Example. Example

Total Score /1 /20 /41 /15 /23 Grader

CSE 373 Spring Midterm. Friday April 21st

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Transcription:

3//15 1 AD: Abstract Data ype 2 Just like a type: Bunch of values together with operations on them. Used often in discussing data structures Important: he definition says ntthing about the implementation, just about the behaviour of the operations what they do. (Could describe necessary execution times of operations) ADS, GRAMMARS, PARSING, R RAVRSALS Lecture 12 CS2110 Spring 2015 xample: A stack is a list of values with these operations: 1. ismpty: return true iff the stack is empty 2. Push: Put a value onto the front (top) of the stack 3. Pop: Remove and return the front (top) value of the stack Is a stack implemented in an array? singly linked list? doubly linked list? ArrayList? We don t know. AD: Abstract Data ype Pointers to material 3 Just like a type: Bunch of values together with operations on them. / an implementation of stack implements a LIO list of values of type. / public interface<> stack { / = the list is empty. / boolean ismpty(); / Push v onto the top of the list. / void push( v); / Remove top value on the list and return it. Precondition: the list is not empty. / pop(); Java interface is a great way to define an AD! Parse trees: text, section 23.36 Definition of Java Language, sometimes useful: docs.oracle.com/javase/specs/jls/se7/html/index.html Grammar for most of Java, for those who are curious: docs.oracle.com/javase/specs/jls/se7/html/jls-18.html ree traversals preorder, inorder, postorder: text, sections 23.13.. 23.15. xpression trees xpression trees 5 Can draw a tree for (2 3) (1 (5 ) public abstract class xp { / return the value of this xp / public abstract int eval(); 5 6 public abstract class xp { / return the value of this xp / public abstract int eval(); 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(); 1

3//15 7 tree for (2 3) (1 ) 8 tree for (2 3) (1 ) Preorder traversal: 1. Visit the root 2. Visit left subtree, in preorder 3. Visit right subtree, in preorder 1 - prefix and postfix notation proposed by Jan Lukasiewicz in 1951 Postfix (we see it later) is often called RPN for Reverse Polish Notation In about 197, Gries paid $300 for an HP calculator, which had some memory and used postfix notation! Still works. Come up to see it. Postorder traversal: 1. Visit left subtree, in postorder 2. Visit right subtree, in postorder 3. Visit the root 1 - Postfix notation 9 tree for (2 3) (1 ) 10 tree for (2 3) (1 ) Cornell tuition Calculator cost Percent 1973 $5030 $300.0596 201 $7,050 $60.00127 hen: (HP 5) RPN. 9 memory locations, -level stack, 1-line display Now: (HP 35S) RPN and infix. 30K user memory, 2-line display 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 Postfix notation 1-11 tree for (2 3) (1 ) 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 (2 3) (1 (- )) 12 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(); / Return the preorder./ public String pre() {return left.pre() right.pre(); / Return the postorder./ public String post() {return left.post() right.post() ; 2

3//15 Motivation for grammars A Grammar 13 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/se7/html/index.html 1 Noun Verb Noun 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 A Grammar A recursive grammar 15 Noun Verb Noun Verb see Grammar: set of rules for generating sentences of a language. xamples of : boys see bunnies bunnies like girls he words boys, girls, bunnies, like, see are called tokens or terminals he words, Noun, Verb are called nonterminals 16 and or Noun Verb Noun see Grammar 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 17 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.. 18 Punctuated. and or Noun VerbNoun New rule adds a period only at end of sentence. okens are the 7 words plus the period (.) Grammar is ambiguous: boys like girls Verb see and girls like boys or girls like bunnies 3

3//15 Grammars for programming languages Grammar for simple expressions (not the best) 19 20 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/se7/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 3) ((23) 89) Some illegal expressions: (3 3 okens of this grammar: ( ) and any integer 21 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 ((23) 89) is a valid expression by building a parse tree ( ) ( ) 23 89 22 22 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 3 23 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 ( ) 2 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 after call: (call returns true) ( 2 ( 8 ) 9 ) already processed unprocessed ( 2 ( 8 ) 9 )

3//15 Specification: / Unprocessed input starts an. / 25 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; 26 Illustration of parsing to check syntax integer ( ) Same code used 3 times. Cries out for a method to do that ( 1 ( 2 ) ) 27 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 1663 build the token 16, the token, and the token 63. 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 ( 8 ) 9 ) Change parser to generate a tree integer 28 / 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 29 / 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 ( ) 30 Code for 2 (3 ) PUSH 2 PUSH 3 PUSH : remove two top values from stack, add them, and place result on stack It s postfix notation! 37 2 S t a c k 5

3//15 Code for a stack machine Use parser to generate code for a stack machine 31 32 Code for 2 (3 ) PUSH 2 PUSH 3 PUSH : remove two top values from stack, add them, and place result on stack 7 29 S t a c k Code for 2 (3 ) PUSH 2 PUSH 3 PUSH : 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 w Code for 1 w Code for 2 w \n It s postfix notation! It s postfix notation! Grammar that gives precedence to over Does recursive descent always work? 33 -> { -> { -> integer -> ( ) 2 3 says do first Notation: { xxx means 0 or more occurrences of xxx. : xpression : erm : actor 2 3 ry to do first, can t complete tree 3 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 Syntactic ambiguity Syntactic ambiguity 35 Sometimes a sentence has more than one parse tree S A aaxb A x aab aaxbb can be parsed B b bb in two ways his kind of ambiguity sometimes shows up in programming languages. In the following, which then does the else go with? if 1 then if 2 then S1 else S2 36 his kind of ambiguity sometimes shows up in programming languages. In the following, which then does the else go with? if 1 then if 2 then S1 else S2 his ambiguity actually affects the program s meaning Resolve it by either (1) Modify the grammar to eliminate the ambiguity (best) (2) Provide an extra non-grammar rule (e.g. else goes with closest if) Can also think of modifying the language (require end delimiters) 6

3//15 Summary: What you should know xercises 37 preorder, inorder, and postorder traversal. How they can be used to get prefix notation, infix notation, and postfix notation for an expression tree. Grammars: productions or rules, tokens or terminals, nonterminals. he parse tree for a sentence of a grammar. Ambiguous grammar, because a sentence is ambiguous (has two different parse trees). You should be able to tell whether string is a sentence of a simple grammar or not. You should be able to tell whether a grammar has an infinite number of sentences. You are not responsible for recursive descent parsing 38 Write a grammar and recursive descent parser for sentence palindromes that ignores white spaces & punctuation Was it liot's toilet I saw? No trace, not one carton Go deliver a dare, vile dog! Madam, I'm Adam Write a grammar and recursive program for strings A n B n AB AABB AAAAAAABBBBBBB Write a grammar and recursive program for Java identifiers <letter> [<letter> or <digit>] 0 N j27, but not 2j7 7