CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Semantics Static semantics Dynamic semantics attribute grammars
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program,
Environments and Evaluation
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
CS 280 Data Structures Professor John Peterson. Lexer Project Questions? Must be in by Friday – solutions will be posted after class The next project.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Language Translators - Lee McCluskey LANGUAGE TRANSLATORS: WEEK 21 LECTURE: Using JavaCup to create simple interpreters
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
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.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Interpreters and Higher-Order Functions CSE 413 Autumn 2008 Credit: CSE341 notes by Dan Grossman.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Chapter 3 – Describing Syntax
Planning & System installation
Planning & System installation
A Simple Syntax-Directed Translator
Parsing & Context-Free Grammars
Basic Program Analysis: AST
Parsing & Context-Free Grammars Hal Perkins Autumn 2011
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
Chapter 2: A Simple One Pass Compiler
ASTs, Grammars, Parsing, Tree traversals
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSE 12 – Basic Data Structures
Operator precedence and AST’s
Parsing & Context-Free Grammars Hal Perkins Summer 2004
Parsing & Context-Free Grammars Hal Perkins Autumn 2005
COMPILER CONSTRUCTION
Presentation transcript:

CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia Lee is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Based on a work at Permissions beyond the scope of this license may be available at LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics 1. ASTs 2

READING QUIZ!  A node in the parse tree corresponds to what? A. one symbol in the BNF grammer B. a transition from one FFT to the next C. the index of the symbol in a string

READING QUIZ!  A parse tree where non-essential nodes are not included is called what? A. An Abstract Syntax Tree B. A Generic Parse Tree C. A Universal Interpreter D. All of the above

READING QUIZ!  A map ADT is useful when you want store, retrieve and manipulate data based on keys associated with pieces of data. In relation to parsing, one application is... A. a distance table B. a difference table C. a symbol table D. a sequence table

Compilers and interpreters

Compilers and Interpreters  One of the most important kinds of programs are programs that read other programs and translate them into action  Compilers and interpreters  They both have two main steps in their processing  Syntax analyzer  Semantic evaluator

Compilers and Interpreters  Syntax analyzer:  Recursively builds up an AST  Semantic evaluator:  Takes an AST and does the calculation by traversing the tree

Syntax Analyzer Building Abstract Syntax Trees

Syntax Analyzer: parse() method  static Assmt parse(java.lang.String s)  So we take a string and need to produce a tree node  (the example above produces Assmt, but could be other kinds of nodes)

Our sample grammar := | := = := + | - | := * | / | := | | ( ) := w | x | y | z := 0 | 1 | 2 | 3 | 4

Sample derivation “2” => => => => => 2 := | := = := + | - | := * | / | := | | ( ) := w | x | y | z := 0 | 1 | 2 | 3 | 4

You turn! “2 + w + 3” Which of the following steps can NOT follow immediately after the other in a derivation? A. => + B => C => D => 2 + w + 3 E. Other/none/more := | := = := + | - | := * | / | := | | ( ) := w | x | y | z := 0 | 1 | 2 | 3 | 4

Solution “2 + w + 3” => => + => + => + => + 3 => => => => => => 2 + w + 3 := | := = := + | - | := * | / | := | | ( ) := w | x | y | z := 0 | 1 | 2 | 3 | 4

Your turn! “2 + w * 3” := | := = := + | - | := * | / | := | | ( ) := w | x | y | z := 0 | 1 | 2 | 3 | 4

So we can do parse() on paper…  …how do we do it in code??  static Assmt parse(java.lang.String s)  So we take a string and need to produce a tree node  (the example above produces Assmt, but could be other kinds of nodes)

Semantic Evaluator Traversing Abstract Syntax Trees

Semantic Evaluator  This is really just a traversal of the tree  We have been talking about the “visit” operation as being something like “print the this node’s data variable”  But here it will be to evaluate the expression

Which kind of traversal? (and WHY?) A. Pre-order: 1. “visit” self 2. Traverse left 3. Traverse right B. In-order: 1. Traverse left 2. “visit” self 3. Traverse right C. Post-order: 1. Traverse left 2. Traverse right 3. “visit” self

Ok so we can do eval() on paper…  …how do we do it in code??

Eval of variables