CSE 12 Abstract Syntax Trees
CSE 12 Abstract Syntax Trees
CSE 12 Abstract Syntax Trees
16
Compilers
Interpreters
Towards an interpreter
The result of all this is: doing what the statement says! (For
example, computing a value and returning it or printing it out)
5
Parts of an interpreter
In designing an interpreter, follow the decomposition into two
tasks, and design it to have two parts:
1. A syntactic analysis engine, which takes as input a string, and
outputs an appropriate tree structure
2. A semantic evaluation engine, which takes as input that tree,
and does what the original input string said to do
program
text
Syntax
analyzer
Semantic
evaluator
appropriate
action
1) Write down the start symbol. This is the first step in the derivation.
2) If the current step in the derivation consists of only terminal
symbols, and is equal to the target string, you have parsed the
string; done.
Example derivations
A derivation starts with the start symbol, and at each step, replaces
one nonterminal symbol by one of the definitions of that nonterminal.
Here are derivations of the strings 2 and 2 + w + 3 based on
that grammar:
<A> => <C> => <D> => <M> => <const> => 2
<A> => <C> => <C> + <D> => <C> + <M> =>
<C> + <const> => <C> + 3 =>
<C> + <D> + 3 => <D> + <D> + 3 =>
<M> + <D> + 3 => <M> + <M> + 3 =>
<const> + <M> + 3 => <const> + <ident> + 3 =>
2 + <ident> + 3 => 2 + w + 3
11
Parsing strings
Using that grammar, parse these strings of terminal
symbols that is, show a derivation of each of them from
the start symbol <A>:
w
2 + w
2 + w * 3
(2 + w) * 3
12
14
in a tree, each node except the root has exactly one parent
in a BNF grammar, each rule has exactly one nonterminal
symbol on the left hand side of the rule (this makes BNF
grammars what are called "context free" grammars)
<A> => <C> => <D> => <M> => <const> => 2
16
<C>
- <D>
<D>
<M>
<D>
<M>
<const>
<M>
<ident>
<const>
2
17
2+w*3
(2 + w) * 3
w=2
18
1 ( 2 + 3 )
<A>
<A>
<C>
<C>
<D>
<M>
<D>
(
<M>
<const>
<C>
<A>
<C>
<C>
<D>
<D>
<M>
<M>
<const>
<const>
<C>
-
<D>
<D>
<M>
<M>
<A>
<const>
1
<C>
+
<C>
<D>
<D>
<M>
<M>
<const>
3
<const>
2
20
21
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
22
Make all these classes derived from one base class, or make
them all implement one interface
permits polymorphism
23
Semantic rules
Evaluating an AST
Evaluate these ASTs ( for 1 ( 2 + 3 ) and
<C>
<A>
<A>
<C>
-
<C>
+
<D>
<D>
<M>
<C>
<M>
<A>
<const>
1
<C>
+
<C>
<D>
<D>
<M>
<M>
<const>
3
<const>
2
<C>
-
1 2 + 3
<D>
<D>
<M>
<D>
<M>
<M>
<const>
2
<const>
3
<const>
1
28
Evaluating AST's
Construct ASTs for the following expressions, and then evaluate
them:
2 + 3 * 4
w = 2
2 + w * 3
29
Evaluating AST's
left-recursive rule gives left-to-right associativity, rightrecursive rule gives right-to-left associativity
symbols farther from the root (the start symbol) are
evaluated first, giving higher precedence
Map
31
Map ADT
32
Map ADT
Domain:
Operations (typical):
In each case:
Using Java generics, you can specify the type of the keys, and
the type of the values associated with keys
34
35
Next time
Reading: Gray, Ch 12
38