Anatomy of a Small Compiler Mx Mx COMS W4115 A Programming Langauge for Scientific Computation Resembles Matlab Octave Mathematica etc Project from Spring 2003 Authors Tiantian Zhou Hanhua Feng Prof Stephen A Edwards Fall 2003 Columbia University Department of Computer Science Yong Man Ra Chang Woo Lee Example Mx source part 1 Mx source part 2 Plotting the Lorenz equations Lorenz equation parameters Parameters for the procedure N 20000 p zeros N 1 3 t 0 0 h 0 001 x 10 0 10 p 0 x matrix transpose dy0 dt dy1 dt dy2 dt a 10 b 8 3 0 r 28 y1 y0 y0 r y2 y1 y0 y1 by2 Two argument function returning a vector func Lorenz y t a y 1 y 0 y 0 y 2 r y 0 y 1 y 0 y 1 b y 2 Runge Kutta numerical integration procedure func RungeKutta f y t h k1 h f y t k2 h f y 0 5 k1 t 0 5 h k3 h f y 0 5 k2 t 0 5 h k4 h f y k3 t h return y k1 k4 6 0 k2 k3 3 0 file Result grammar g lines role Scanner and Parser Builds the tree 314 Lexer Parser ANTLR source Interpreter Walks the tree invokes objects methods walker g 170 Tree Walker ANTLR source MxInterpreter java 359 Function invocation etc MxSymbolTable java 109 Name to object mapping Top level Invokes the interpreter MxMain java 153 Command line interface MxException java 13 Error reporting Runtime system Represents data performs operations MxDataType java 169 Base class MxBool java 63 Booleans MxInt java 152 Integers MxDouble java 142 Floating point MxString java 47 String MxVariable java 26 Undefined variable MxFunction java 81 User defined functions MxInternalFunction m4 410 sin cos etc macro processed jamaica Matrix java 1387 Matrices MxMatrix java 354 Wrapper jamaica Range java 163 e g 1 10 MxRange java 67 Wrapper jamaica BitArray java 226 Matrix masks MxBitArray java 47 Wrapper jamaica Painter java 339 Bitmaps jamaica Plotter java 580 2 D plotting total 5371 for i 1 N x RungeKutta Lorenz x t h p i x t h colormap 3 plot p return 0 The Scanner class MxAntlrLexer extends Lexer options k 2 charVocabulary 3 377 testLiterals false exportVocab MxAntlr protected ALPHA a z A Z protected DIGIT 0 9 WS t setType Token SKIP NL n r n r n r setType Token SKIP newline The Scanner The Scanner The Parser Top level COMMENT options greedy false NL n r n r NL setType Token SKIP LPAREN RPAREN TRSP COLON DCOLON class MxAntlrParser extends Parser LDV LDVEQ ID options testLiterals true ALPHA ALPHA DIGIT setType LDVEQ setType LDV NUMBER DIGIT DIGIT E e DIGIT STRING n options k 2 buildAST true exportVocab MxAntlr tokens STATEMENT FOR CON program statement func def EOF program STATEMENT PROG program The Parser Statements The Parser Statements 1 The Parser Statements 2 statement for stmt if stmt loop stmt break stmt continue stmt return stmt load stmt assignment func call stmt LBRACE statement RBRACE statement STATEMENT STATEMENT statement for stmt for LPAREN for con RPAREN statement break stmt break ID SEMI continue stmt continue ID SEMI return stmt return expression SEMI load stmt include STRING SEMI for con ID ASGN range COMMA ID ASGN range for con FOR CON FOR CON for con if stmt if LPAREN expression RPAREN statement options greedy true else statement loop stmt loop LPAREN id ID RPAREN stmt statement if null id loop stmt LOOP loop stmt else loop stmt LOOP loop stmt id assignment l value ASGN PLUSEQ MINUSEQ MULTEQ LDVEQ MODEQ RDVEQ expression SEMI func call stmt func call SEMI func call ID LPAREN expr list RPAREN func call FUNC CALL FUNC CALL func call The Parser Function Definitions The Parser Expressions The Walker Top level func def func ID LPAREN var list RPAREN func body expression logic term logic factor relat expr var list ID COMMA ID var list VAR LIST VAR LIST var list var list VAR LIST VAR LIST var list func body ASGN a expression SEMI func body a LBRACE statement RBRACE func body STATEMENT FUNC BODY func body logic term or logic term logic factor and logic factor not relat expr arith expr GE LE GT LT EQ NEQ arith expr arith expr arith term PLUS MINUS arith term arith term arith factor MULT LDV MOD RDV arith factor arith factor PLUS r value arith factor UPLUS UPLUS arith factor MINUS r value arith factor UMINUS UMINUS arith factor r value TRSP r value l value func call NUMBER STRING true false array LPAREN expression RPAREN l value ID LBRK index RBRK import java io import java util class MxAntlrWalker extends TreeParser options importVocab MxAntlr static MxDataType null data new MxDataType NULL MxInterpreter ipt new MxInterpreter The Walker Literals Variables and Functions The Walker Expressions The Walker Simple operators expr returns MxDataType r MxDataType a b Vector v MxDataType x String s null String sx r null data or a expr right or if a instanceof MxBool r MxBool a var a expr right or else r a or expr right or and a expr right and if a instanceof MxBool r MxBool a var expr right and a else r a and expr right and The Walker For and If statements The Walker Multiple expressions The Walker Variable list for x mexpr forbody MxInt values ipt forInit x while ipt forCanProceed x values r expr forbody ipt forNext x values ipt forEnd x if a expr thenp elsep if a instanceof MxBool return a error if expression should be bool if MxBool a var r expr thenp else if null elsep r expr elsep mexpr returns MxDataType rv MxDataType a rv null Vector v EXPR LIST v new Vector a expr v add a rv ipt convertExprList v a expr rv new MxDataType 1 rv 0 a FOR CON v new Vector s ID a expr a setName s getText v add a rv ipt convertExprList v vlist returns String sv Vector v sv null VAR LIST v new Vector s ID v add s getText sv ipt convertVarList v CEC Esterel Syntax CEC is the Columbia Esterel Compiler that my group is currently developing Standard free form style CEC not a expr r a not GE a expr b expr r a ge b LE a expr b expr r a le b GT a expr b expr r a gt b LT a expr b expr r a lt b EQ a expr b expr r a eq b NEQ a expr b expr r a ne b PLUS a expr b expr r a plus b MINUS a expr b expr r a minus b MULT a expr b expr r a times b LDV a expr b expr r a lfracts b RDV a expr b expr r a rfracts b MOD a expr b expr r a modulus b COLON c1 c2 r MxRange create null c1 null expr c1 null c2 null expr c2 ASGN a expr b expr r ipt assign a b FUNC CALL a expr …
View Full Document
Unlocking...