04._Abstract_Syntax

04._Abstract_Syntax - Abstract Syntax Tree (AST) Teodor...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Abstract Syntax Tree (AST) Teodor Przymusinski Automated Parser Generator (CUP) Grammar Parse DFA Parse Table Parser 05/13/09 Teodor Przymusinski CS 152 2 Syntactic Analysis (Lexer & Parser) Program Lexer Stream of Tokens Parser (using parse table) Concrete Syntax Tree Parser (using semantic actions) Abstract Syntax Tree 05/13/09 Teodor Przymusinski CS 152 3 Sample Grammar 0. P S$ 1. S S ; S 2. S id := E 3. E id 4. E num 5. E E E right assoc ; left assoc 05/13/09 Program (PR) CompoundStm (CS) AssignStm (AS) IdExp (IE) NumExp (NE) MinusExp (ME) Teodor Przymusinski CS 152 4 Parse Table $ 0 1 2 3 4 5 6 7 8 9 05/13/09 10 id 4 num - := ; 2 S 1 3 E A 4 r1 5 7 r2 r3 r4 7 r5 8 Teodor Przymusinski r5 / 9 r1 / 2 8 9 r3 r4 r2 r3 r4 10 CS 152r5 5 6 Parsing Using the Parse Table x := 2 ; y := 2 x 1 $ id(x) := num(2) ; id(y) := num(2) id(x) num(1) $ id4 :=5 num8 S1 ;2 id4 :=5 E6 id4 :=5 E6 S1 ;2 id4 :=5 num8 S1 ;2 id4 :=5 E6 -9 id7 S1 ;2 id4 :=5 E6 -9 E10 S1 ;2 id4 :=5 E6 -9 num8 S1 ;2 id4 :=5 E6 -9 E10 S1 ;2 S3 S1 Accept! Concrete Syntax (Parse) Tree id(x) := num(2) ; id(y) := num(2) id(x) num(1) $ P(PR) S(CS) S(AS) E(ME) S(AS) E(NE) E(NE) E(ME) E(iE) E(NE) id(x) := num(2) ; id(y) := num(2) id(x) num(1) $ Abstract Syntax (Parse) Tree - AST id(x) := num(2) ; id(y) := num(2) id(x) num(1) $ P(PR) S(CS) S(AS) E(ME) S(AS) E(NE) id(x) num(2) id(y) E(NE) num(2) E(ME) E(iE) id(x) E(NE) num(1) Building Abstract Syntax Trees in Java abstract class Stm { int pos; Abstract int accept (Visitor v); } class CompoundStm extends Stm { Stm left, right; int accept (Visitor v) { return v.visit (this); } CompoundStm (Stm l, Stm r ) { left=l; right=r; pos= (l.pos + r.pos) / 2 } } class AssignStm extends Stm { String id; Exp exp; int accept (Visitor v) { return v.visit (this); } AssignStm (String i, Exp e ) { id = I; exp = e; pos= e.pos } } 05/13/09 Teodor Przymusinski CS 152 9 Building Abstract Syntax Trees Cont. abstract class Exp { int pos; Abstract int accept (Visitor v); } class MinusExp extends Exp { Exp left, right; int accept (Visitor v) { return v.visit (this); } MinusExp (Exp l, Exp r ) { left=l; right=r; pos= (l.pos + r.pos) / 2 } } class NumExp extends Exp { int value; int accept (Visitor v) { return v.visit (this); } NumExp ( int n, int p ) { value=n; pos=p } } class IdExp extends Exp { String value; int accept (Visitor v) { return v.visit (this); } IdExp ( String n, int p ) { value=n; pos=p; } } Teodor Przymusinski CS152 10 Visitor Interface interface Visitor { int visit (MinusExp e); int visit (NumExp e); int visit (IdExp e); int visit (CompoundStm s); int visit (AssignStm s); } class Interpreter implements Visitor { int visit (MinusExp e) { return e.left.accept (this) - e.right.accept (this) ; } int visit (CompoundStm s) { s.left.accept (this); s.right.accept (this); } ... } class Optimizer implements Visitor { int visit (MinusExp e) { ... } int visit (CompoundStm s) { ... } ... } Teodor Przymusinski CS152 11 Semantic Actions 1. 2. 3. 4. 5. S S:l ; S:r S id(i) := E:e E id(i):k E num(n):k E E:l E:r { return new CompoundStm(l, r); } { return new AssignStm(i, e); } { return new IdExp(i, k.p); } { return new NumExp(n, k.p); } { return new MinusExp(l, r); } 05/13/09 Teodor Przymusinski CS 152 12 Abstract Syntax for Mini-Java Skeleton classes are given on page 99. Make sure to study them carefully to understand their relationship to the syntx of Mini-Java Visitor interface is given on page 100 Details of the implementation will be discussed in the lab Make sure to become familiar with the syntax of CUP 05/13/09 Teodor Przymusinski CS 152 13 ...
View Full Document

This note was uploaded on 06/03/2008 for the course CS 152 taught by Professor Przymusinski during the Spring '08 term at UC Riverside.

Ask a homework question - tutors are online