CS453 Intro and PA1 1CS453 Lecture MiniJava Intro 1Plan for Today Structure of the MiniJava Compiler– Lexer– Interface between lexer and parser: Symbol and TokenValue– Parser– Interface between parser and semantic analysis: ast.node.*– Interface between semantic analysis and code generation: symbol tableCS453 Lecture MiniJava Intro 2Structure of the MiniJava Compiler“sentences”SynthesisoptimizationAssem (MIPS)IR code generationAssem (MIPS)Analysischaracter streamlexical analysis“words”tokenssemantic analysissyntactic analysisASTAST and symbol tablecode genMIPSPA3PA4PA5PA6553CS453 Lecture MiniJava Intro 3Specifying Tokens with JFlex JFlex example input file: package mjparser; import java_cup.runtime.Symbol; %% %line %char %cup %public %eofval{ return new Symbol(sym.EOF, newTokenValue("EOF", yyline, yychar)); %eofval} LETTER=[A-Za-z] DIGIT=[0-9] UNDERSCORE="_" LETT_DIG_UND={LETTER}|{DIGIT}|{UNDERSCORE} ID={LETTER}({LETT_DIG_UND})* %% "&&" { return new Symbol(sym.AND, newTokenValue(yytext(), yyline, yychar)); } "boolean" {return newSymbol(sym.BOOLEAN,... {ID} { return new Symbol(sym.ID, new ...CS453 Lecture MiniJava Intro 4Specifying Grammar with JavaCUP JavaCUP example input file: package mjparser; import java_cup.runtime.*; import ast.node.*; ... terminal AND, ASSIGN, INT; terminal mjparser.TokenValue NUMBER; ... non terminal Program program; non terminal List<IClassDecl> class_decl_list; non terminal MainClass main_class; start with program; program ::= main_class:m class_decl_list:l {: RESULT = new Program(m,l); :} ; exp ::= | NUMBER:n {: Token token = new Token(n.text,n.line, n.pos); RESULT = new IntegerExp( token ); :} ...CS453 Intro and PA1 2CS453 Lecture MiniJava Intro 5Abstract Syntax Tree for Memory Layout ExampleCS453 Lecture MiniJava Intro 6Example Symbol Tableclass And { public static void main(String[] a){System.out.println(new Foo().testing(42)); }}class Foo { public int testing(int p) { int x; if (p < 10 && 2 < p) { x = 7; } else { x = 22; } return x;}}CS453 Lecture MiniJava Intro 7Assem(MIPS).textmain:main_framesize=24main_paramsNregsaves=8sw $ra, 0($sp)subu $sp, $sp, 4sw $fp, 0($sp)subu $sp, $sp, 4addu $fp, $sp, main_paramsNregsavessubu $sp, $fp, main_framesize# ExpCALL# ExpCONSTli $t2, 0sw $t2, -8($fp)lw $t2, -8($fp)# push parameter onto stacksw $t2, 0($sp)subu $sp, $sp, 4jal _halloc#StmMOVE(ExpTEMP(t1), e)move $t0, $v0sw $t0, -12($fp)# ExpCALLlw $t2, -12($fp)# push parameter onto stacksw $t2, 0($sp)subu $sp, $sp, 4# ExpCONSTli $t2, 42sw $t2, -16($fp)lw $t2, -16($fp)# push parameter onto stacksw $t2, 0($sp)subu $sp, $sp, 4jal Foo_testing#StmMOVE(ExpTEMP(t1), e)move $t0, $v0sw $t0, -20($fp)# ExpCALLlw $t2, -20($fp)# push parameter onto stacksw $t2, 0($sp)subu $sp, $sp, 4jal _printint# epiloguedone1:lw $ra, 0($fp)move $t0, $fplw $fp, -4($fp)move $sp, $t0jr
View Full Document