lec24 - CSE 12 Evaluating Abstract Syntax Trees Creating...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
24 Creating and Evaluating AST's Semantic Rules and BNF Rules The Map ADT and Symbol Tables CSE 12 Evaluating Abstract Syntax Trees
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Abstract syntax trees A parse tree includes all the information in a derivation of a string from the start symbol In a parse tree, the children of a nonterminal symbol node include nodes for all of the symbols in the definition of that nonterminal But this may be more information than is required for some applications of the tree For example: do not need to include nodes for parentheses just used for grouping; information in nodes corresponding to expression operators can be moved into their parent nodes; etc. A parse tree with nonessential information left out is called an Abstract Syntax Tree (AST)
Background image of page 2
A BNF grammar We are using this BNF grammar as an example Nonterminal symbols are enclosed in corner brackets <> Grammar metasymbols are := and | Other symbols are terminal symbols, to be taken literally <A> := <B> | <C> <B> := <ident> = <A> <C> := <C> + <D> | <C> - <D> | <D> <D> := <D> * <M> | <D> / <M> | <M> <M> := <ident> | <const> | (<A>) <ident> := w | x | y | z <const> := 0 | 1 | 2 | 3 | 4
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Parse tree and AST Parse tree and an AST for the string 1 – ( 2 + 3 ) <A> <C> <D> <C> <D> <M> - + <M> <const> 1 <A> ( ) <C> <C> <D> <D> <M> <const> 2 <M> <const> 3 <A> <C> <D> <C> <D> <M> - + <M> <const> 1 <A> <C> <C> <D> <D> <M> <const> 2 <M> <const> 3
Background image of page 4
Syntactic analysis == building an AST The input to the syntactic analysis phase of an interpreter is a string; the output is an AST So the process of syntactic analysis is the process of constructing an AST There are two main issues to address before implementing syntactic analysis in this way: the design of the AST data structure: what type of objects to use for the nodes, etc. what algorithm to use to build the AST
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
AST data structure design issues You know how to implement binary trees. .. ... but AST’s raise some additional issues: In general, a node in an AST can have more than 2 children (depends on how many symbols appear on the right hand side of a rule in the grammar) Intuitively, the nodes of an AST are of somewhat different types. .. a <const> node has a different meaning from a <ident> node, for example At the same time, the nodes of an AST are of similar type: they all correspond to part of an input string at some level of analysis
Background image of page 6
Applying OO design to AST design In an object-oriented approach, you can easily deal with these design issues: Define a separate class corresponding to each different nonterminal symbol in the grammar: nodes in the AST will be instances of these classes lets you make different types of nodes have different
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

lec24 - CSE 12 Evaluating Abstract Syntax Trees Creating...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online