Download presentation
Presentation is loading. Please wait.
Published byMarjory Porter Modified over 9 years ago
1
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 http://peerinstruction4cs.org. Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org.Cynthia LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org
2
Today’s Topics 1. ASTs 2
3
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
4
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
5
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
6
Compilers and interpreters
7
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
8
Compilers and Interpreters Syntax analyzer: Recursively builds up an AST Semantic evaluator: Takes an AST and does the calculation by traversing the tree
9
Syntax Analyzer Building Abstract Syntax Trees
10
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)
11
Our sample grammar := | := = := + | - | := * | / | := | | ( ) := w | x | y | z := 0 | 1 | 2 | 3 | 4
12
Sample derivation “2” => => => => => 2 := | := = := + | - | := * | / | := | | ( ) := w | x | y | z := 0 | 1 | 2 | 3 | 4
13
You turn! “2 + w + 3” Which of the following steps can NOT follow immediately after the other in a derivation? A. => + B. + + 3 => + + 3 C. + + 3 => + + 3 D. 2 + + 3 => 2 + w + 3 E. Other/none/more := | := = := + | - | := * | / | := | | ( ) := w | x | y | z := 0 | 1 | 2 | 3 | 4
14
Solution “2 + w + 3” => => + => + => + => + 3 => + + 3 => + + 3 => + + 3 => + + 3 => 2 + + 3 => 2 + w + 3 := | := = := + | - | := * | / | := | | ( ) := w | x | y | z := 0 | 1 | 2 | 3 | 4
15
Your turn! “2 + w * 3” := | := = := + | - | := * | / | := | | ( ) := w | x | y | z := 0 | 1 | 2 | 3 | 4
16
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)
17
Semantic Evaluator Traversing Abstract Syntax Trees
18
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
19
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
20
Ok so we can do eval() on paper… …how do we do it in code??
21
Eval of variables
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.