ASTs, Grammars, Parsing, Tree traversals

Slides:



Advertisements
Similar presentations
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Advertisements

GRAMMARS & PARSING Lecture 7 CS2110 – Fall
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix,
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
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.
Stacks.
Reverse Polish Notation (RPN) & Stacks CSC 1401: Introduction to Programming with Java Week 14 – Lecture 2 Wanda M. Kunkle.
Context-Free Grammars Lecture 7
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
1 Stacks Stack Applications Evaluating Postfix Expressions Introduction to Project 2 Reading: L&C Section 3.2,
Class 4: Queues. cis 335 Fall 2001 Barry Cohen What is a queue? n A stack is an ordered sequence of items. n As in lists and stacks, each node contains.
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.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
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.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
Information and Computer Sciences University of Hawaii, Manoa
CS 331, Principles of Programming Languages Chapter 2.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 12 CS2110 – Spring
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
GRAMMARS & PARSING Lecture 8 CS2110 – Spring If you are going to form a group for A2, please do it before tomorrow (Friday) noon.
CS 331, Principles of Programming Languages Chapter 2.
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.
Reverse Polish Notation Written by J.J. Shepherd.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
ADTS, GRAMMARS, PARSING, TREE TRAVERSALS Lecture 13 CS2110 – Spring
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
ASTs, Grammars, Parsing, Tree traversals
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Grammars and Parsing.
Revised based on textbook author’s notes.
A Simple Syntax-Directed Translator
Introduction to Parsing
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
Copyright ©2012 by Pearson Education, Inc. All rights reserved
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Syntax-Directed Translation
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
ADTs, Grammars, Parsing, Tree traversals
Syntax-Directed Translation
ASTs, Grammars, Parsing, Tree traversals
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
Context Free Grammars-II
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 13 CS2110 – Fall 2017

Expression Trees From last time: 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 * 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. 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. * 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. 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. + * 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. * 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

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)))))

Expression trees: in code public interface Expr { String infix(); // returns an infix representation int eval(); // returns the value of the expression } public class Int implements Expr { private int v; public int eval() { return v; } public String infix() { return " " + v + " "; } public class Sum implements Expr { private Expr left, right; public int eval() { return left.eval() + right.eval(); } public String infix() { 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. 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