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

Similar documents
syntax tree - * * * * * *

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

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

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

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

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

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

Lecture 12 TREES II CS2110 Spring 2018

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

GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

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

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.

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

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

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

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

Stacks, Queues and Hierarchical Collections

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

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

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

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

EE 368. Week 6 (Notes)

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

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

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

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

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

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

Recursive Data Structures and Grammars

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Syntax and Grammars 1 / 21

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

12 Abstract Data Types

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

CSE 373 APRIL 17 TH TREE BALANCE AND AVL

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Expressions and Assignment

COMP 250 Fall binary trees Oct. 27, 2017

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

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

CS2383 Programming Assignment 3

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

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

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

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

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Syntax-Directed Translation. Lecture 14

A Simple Syntax-Directed Translator

Announcements TREES II. Comparing Data Structures. Binary Search Trees. Red-Black Trees. Red-Black Trees 3/13/18

Building Compilers with Phoenix

It parses an input string of tokens by tracing out the steps in a leftmost derivation.

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Question Points Score

Algorithms and Data Structures Exercise I

March 20/2003 Jayakanth Srinivasan,

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

1 Lexical Considerations

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

CS61BL. Lecture 3: Asymptotic Analysis Trees & Tree Traversals Stacks and Queues Binary Search Trees (and other trees)

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

CSE P 501 Exam 8/5/04

An Introduction to Trees

Computer Science E-119 Fall Problem Set 4. Due prior to lecture on Wednesday, November 28

Introduction to Parsing. Lecture 8

RECURSION (CONTINUED)

Upcoming ACM Events Linux Crash Course Date: Time: Location: Weekly Crack the Coding Interview Date:

Lecture 26. Introduction to Trees. Trees

Recitation 9. Prelim Review

CS W3134: Data Structures in Java

The Stack and Queue Types

Lecture Chapter 6 Recursion as a Problem Solving Technique

Table of Contents. Chapter 1: Introduction to Data Structures... 1

How do LL(1) Parsers Build Syntax Trees?

Optimizing Finite Automata

CS153: Compilers Lecture 4: Recursive Parsing

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

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

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

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

Principles of Programming Languages COMP251: Syntax and Grammars

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

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

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Stating the obvious, people and computers do not speak the same language.

COMP3131/9102: Programming Languages and Compilers

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

3. According to universal addressing, what is the address of vertex d? 4. According to universal addressing, what is the address of vertex f?

Lecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

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

INF2220: algorithms and data structures Series 1

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Compiler Code Generation COMP360

Transcription:

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

Announcements 2 Today: The last day to request prelim regrades Assignment A4 due next Thursday night. Please work on it early and steadily. Watch the two videos on recursion on trees before working on A4! Next week s recitation. Learn about interfaces Iterator and Iterable. There will be 15 minutes of videos to watch. Then, in recitation, you will fix your A3 so that a foreach loop can be used on it. DLL<Integer> d= new DLL<Integer>(); for (Integer i : d) { }

Expression Trees 3 we can draw a syntax tree for the Java expression 2 1 (1 + 0). 2 1 - + 1 0

Pre-order, Post-order, and In-order 4 2 1 - + 1 0 Pre-order traversal: 1. Visit the root 2. Visit the left subtree (in pre-order) 3. Visit the right subtree - 2 1 + 1 0

Pre-order, Post-order, and In-order 5 2 1 - + 1 0 Pre-order traversal - 2 1 + 1 0 Post-order traversal 1. Visit the left subtree (in post-order) 2. Visit the right subtree 3. Visit the root 2 1 1 0 + -

Pre-order, Post-order, and In-order 6 2 1 - + 1 0 Pre-order traversal Post-order traversal - 2 1 + 1 0 2 1 1 0 + - In-order traversal 1. Visit the left subtree (in-order) 2. Visit the root 3. Visit the right subtree 2 1-1 + 0

Pre-order, Post-order, and In-order 7 2 1 - + 1 0 Pre-order traversal Post-order traversal - 2 1 + 1 0 2 1 1 0 + - In-order traversal (2 1) - (1 + 0) To avoid ambiguity, add parentheses around subtrees that contain operators.

In Defense of Postfix Notation 8 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. + 2 1 1 0 + 2 1 1 0

In Defense of Postfix Notation 9 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. + 1 1 0 + 2 1 1 0 2

In Defense of Postfix Notation 10 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. + 1 0 + 2 1 1 0 1 2

In Defense of Postfix Notation 11 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. + 1 0 + 2 1 1 0 2

In Defense of Postfix Notation 12 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. 2 1 + 1 0 0 1 2 +

In Defense of Postfix Notation 13 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. + 2 1 1 0 1 2

In Defense of Postfix Notation 14 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. 2 1 + 1 0 2

In Defense of Postfix Notation 15 Execute expressions in postfix notation by reading from left to right. Numbers: push onto the stack. Operators: pop the operands off the stack, do the operation, and push the result onto the stack. In about 1974, Gries paid $300 for an HP calculator, which had some memory and used postfix notation! Still works. 2 a.k.a. reverse Polish notation

In Defense of Prefix Notation 16 Function calls in most programming languages use prefix notation: like add(37, 5). Some languages (Lisp, Scheme, Racket) use prefix notation for everything to make the syntax simpler. (define (fib n) (if (<= n 2) 1 (+ (fib (- n 1) (fib (- n 2)))))

18 Determine tree from preorder and postorder Suppose inorder is B C A E D preorder is A B C D E Can we determine the tree uniquely?

19 Determine tree from preorder and postorder Suppose inorder is B C A E D preorder is A B C D E Can we determine the tree uniquely? What is the root? preorder tells us: A What comes before/after root A? Inorder tells us: Before : B C After: E D

20 Determine tree from preorder and postorder Suppose inorder is B C A E D preorder is A B C D E The root is A. Left subtree contains B C Right subtree contains E D Now figure out left, right subtrees using the same method. From the above: For left subtree For right subtree: inorder is: B C inorder is: E D preorder is: B C preorder is: D E root is: B root is: D Right subtree: C left subtree: E

Expression trees: in code 21 public interface Expr { String inorder(); // returns an inorder representation int eval(); // returns the value of the expression } public class int implements Expr { private int v; public int eval() { return v; } public String inorder() { return " " + v + " "; } } public class Sum implements Expr { private Expr left, right; public int eval() { return left.eval() + right.eval(); } public String infinorder() { return "(" + left.infix() + "+" + right.infix() + ")"; } }

Grammars 22 The cat ate the rat. The cat ate the rat slowly. The small cat ate the big rat slowly. The small cat ate the big rat on the mat slowly. The small cat that sat in the hat ate the big rat on the mat slowly, then got sick. Not all sequences of words are sentences: The ate cat rat the How many legal sentences are there? How many legal Java programs are there? How can we check whether a string is a Java program?

Grammars 23 A grammar is a set of rules for generating the valid strings of a language. Sentence Verb Verb Verb goats astrophysics bunnies like see Read as may be composed of

Grammars 24 A grammar is a set of rules for generating the valid strings of a language. Sentence Verb Verb Verb goats astrophysics bunnies like see Sentence

Grammars 25 A grammar is a set of rules for generating the valid strings of a language. Sentence Verb Verb Verb goats astrophysics bunnies like see Verb

Grammars 26 A grammar is a set of rules for generating the valid strings of a language. Sentence Verb Verb Verb goats astrophysics bunnies like see bunnies Verb

Grammars 27 A grammar is a set of rules for generating the valid strings of a language. Sentence Verb Verb Verb goats astrophysics bunnies like see bunnies like

Grammars 28 A grammar is a set of rules for generating the valid strings of a language. Sentence Verb Verb Verb goats astrophysics bunnies like see bunnies like astrophysics

Grammars 30 A grammar is a set of rules for generating the valid strings of a language. Sentence Verb Verb Verb goats astrophysics bunnies like see bunnies like astrophysics goats see bunnies (18 sentences total) The words goats, astrophysics, bunnies, like, see are called tokens or terminals The words Sentence,, Verb are called nonterminals

A recursive grammar 31 Sentence Sentence Sentence Sentence and Sentence Sentence or Sentence Verb goats astrophysics bunnies Verb like see bunnies like astrophysics goats see bunnies bunnies like goats and goats see bunnies (infinite possibilities!) The recursive definition of Sentence makes this grammar infinite.

Grammars for programming languages 34 A grammar describes every possible legal program. You could use the grammar for Java to list every possible Java program. (It would take forever.) A grammar also describes how to parse legal programs. The Java compiler uses a grammar to translate your text file into a syntax tree and to decide whether a program is legal. docs.oracle.com/javase/specs/jls/se8/html/jls-2.html#jls-2.3 docs.oracle.com/javase/specs/jls/se8/html/jls-19.html

Grammar for simple expressions (not the best) 35 E integer E ( E + E ) Simple expressions: An E can be an integer. An E can be ( followed by an E followed by + followed by an E 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) ((4+23) + 89) Some illegal expressions: (3 3 + 4 Tokens of this grammar: ( + ) and any integer