ASTs, Grammars, Parsing, Tree traversals

Slides:



Advertisements
Similar presentations
GRAMMARS & PARSING Lecture 7 CS2110 – Fall
Advertisements

Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
GRAMMARS & PARSING Lecture 7 CS2110 – Fall Java Tips 2  Declare fields and methods public if they are to be visible outside the class; helper methods.
Trees Chapter Chapter Contents Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals.
Stacks.
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Context-Free Grammars Lecture 7
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Preorder Traversal with a Stack Push the root onto the stack. While the stack is not empty n pop the stack and visit it.
Class 4: Stacks. cis 335 Fall 2001 Barry Cohen What is a stack? n A stack is an ordered sequence of items, of which only the last (‘top’) item can be.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 12 CS2110 – Spring
Binary Trees. Node structure Data A data field and two pointers, left and right.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Computer Science 112 Fundamentals of Programming II Expression Trees.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
10.3 Tree Transversal. Pre/post fix notation and order See handout. a.bc.d e f g h i j k.
CS2852 Week 8, Class 2 Today Tree terminology Non-Binary and Non-Search Trees Tree Traversals (Remaining slides not yet shown) Tomorrow: Quiz Implementing.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Trees.ppt1 Introduction Many data structures are linear –unique first component –unique last component –other components have unique predecessor and successor.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Tree Data Structures.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 12 CS2110 – Spring
1 CS 163 Data Structures Chapter 9 Building, Printing Binary Trees Herbert G. Mayer, PSU Status 5/21/2015.
Introduction to Parsing
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
GRAMMARS & PARSING Lecture 8 CS2110 – Spring If you are going to form a group for A2, please do it before tomorrow (Friday) noon.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
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.
Wednesday, March 2 Homework #2 is posted Homework #2 is posted Due Friday, March 4 (BOC) Due Friday, March 4 (BOC) Program #5 is posted Program #5 is posted.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
Reverse Polish Notation Written by J.J. Shepherd.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary Trees.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
Chapter 3: Describing Syntax and Semantics
ASTs, Grammars, Parsing, Tree traversals
Grammars and Parsing.
Revised based on textbook author’s notes.
A Simple Syntax-Directed Translator
Parsing & Context-Free Grammars
CS510 Compiler Lecture 4.
MEMORY REPRESENTATION OF STACKS
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
CO4301 – Advanced Games Development Week 2 Introduction to Parsing
Context-free Languages
ASTs, Grammars, Parsing, Tree traversals
Podcast Ch17a Title: Expression Trees
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Syntax-Directed Translation
CS212: Data Structures and Algorithms
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
ADTs, Grammars, Parsing, Tree traversals
Syntax-Directed Translation
Section 9.3 by Andrew Watkins
Queue Applications Lecture 31 Mon, Apr 9, 2007.
CSC 143 Java Applications of Trees.
Trees, part 2 Lecture 13 CS2110 – Spring 2019
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Discrete Maths 13. Grammars Objectives
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
Presentation transcript:

ASTs, Grammars, Parsing, Tree traversals Lecture 14 CS2110 – Fall 2018

Announcements 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 we can draw a syntax tree for the Java expression 2 * 1 – (1 + 0). - * + 2 1 1 This could be extended to other Java syntax—like classes and methods!

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

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

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

Pre-order, Post-order, and In-order * 2 1 + Pre-order traversal - * 2 1 + 1 0 Post-order traversal 2 1 * 1 0 + - While infix notation is the most “human,” it’s also the most problematic! It leads to ambiguity that has to be solved with parentheses. The other two styles don’t have this problem. In-order traversal (2 * 1) - (1 + 0) To avoid ambiguity, add parentheses around subtrees that contain operators.

In Defense of Postfix Notation 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 + 2 1 * 1 0 + *

In Defense of Postfix Notation 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 * 1 0 + * 2

In Defense of Postfix Notation 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 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 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 2

In Defense of Postfix Notation 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 2

In Defense of Postfix Notation 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 + 2

In Defense of Postfix Notation 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 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)))))

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?

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

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 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 Not all sequences of words are sentences: The ate cat rat the 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 A grammar is a set of rules for generating the valid strings of a language. Read  as “may be composed of” Sentence  Noun Verb Noun Noun  goats Noun  astrophysics Noun  bunnies Verb  like Verb  see

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

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

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

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

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

Grammars A grammar is a set of rules for generating the valid strings of a language. Sentence  Noun Verb Noun Noun  goats Noun  astrophysics Noun  bunnies Verb  like Verb  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, Noun, Verb are called nonterminals

A recursive grammar Sentence  Sentence and Sentence Sentence  Sentence or Sentence Sentence  Noun Verb Noun Noun  goats Noun  astrophysics Noun  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 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) 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