240 likes | 472 Views
MiniJava Compiler. A multi-back-end JIT compiler of Java. Team. Instructor 华保健 Participants 徐波 孙浩 裴达凯 范琳 张腾宇. What we have achieved What is unfinished yet What we will go on with. Our original plan. Lexical analysis Parsing Type check Translation. Our accomplishment.
E N D
MiniJava Compiler A multi-back-end JIT compiler of Java
Team • Instructor 华保健 • Participants 徐波 孙浩 裴达凯 范琳 张腾宇
What we have achieved • What is unfinished yet • What we will go on with
Our original plan • Lexical analysis • Parsing • Type check • Translation
Our accomplishment • Lexical analysis • Parsing • AST disposal • Translation
Lexer • Tool: Jflex • Announced to be “The Fast Scanner Generator for Java” • From source language into target language, a compiler must first pull it apart and understand its structure and meaning, then put it together in a different way • Here we break the input into individual words or "tokens"
Part of “miniJava.flex” //Macro Declarations INTEGER=0|[1-9][0-9]* ALPHA=[A-Za-z] … //keywords "void“ { return symbol(sym.VOID); } "main“ { return symbol(sym.MAIN); } … //operator "&&“ { return symbol(sym.AND); } "<“ { return symbol(sym.LESS); } "!“ { return symbol(sym.DENY); } …
Parser • Tool: CUP • Parser Generator in Java • grammars is used to describe the structure of lexical tokens • parsing using a simple algorithm known as context-free • grammar production turns into one clause of recursive functions
Part of “parser.cup” //terminal terminal CLASS, MAIN, …, ADD, MINUS… terminal Integer INTEGER; terminal String ID; //non terminal non terminal syntaxTree.exp.exp exp; non terminal syntaxTree.program.program Program; //precedence && associativity precedence left AND; precedence left LESS; precedence left ADD,MINUS; //recursive function Program ::= MainClass:m ClassDecl:c {: RESULT = new programMain(m,c); :} | MainClass:m {: RESULT = new programMain(m); :}
Abstract syntax tree • A compiler must do more than recognize whether a sentence belongs to the language of a grammar - it must do something useful with that sentence • To produce a parse tree - a data structure that later phases of the compiler can traverse • Technically, a parse tree has exactly one leaf for each token of the input and one internal node for each grammar rule reduced during the parse • Many of the punctuation tokens are redundant and convey no information
Structure of AST Program → MainClass ClassDecl* MainClass → classid { public static void main ( String [] id ) { Statement }} ClassDecl → classid { VarDecl* MethodDecl* } → classidextendsid { VarDecl* MethodDecl* } … Exp → Exp op Exp → Exp [ Exp ] → Exp . length → Exp . id ( ExpList ) → INTEGER LITERAL → true → false → id → this → new int [ Exp ] → newid () → ! Exp → ( Exp )
Deal with AST • Here comes troubles for us • Semantic analysis is exhausted • Bytecode grammar is a rough task • Time is limited • No knowledge of intermediate language
Transformation • Java 2 C • Two choices • Re-construct a AST for C • Use method in AST to convert the representation of Java AST • We choose the second one • Every class of AST contains the method to output follow the C grammer
Transformation //the root point of AST of MiniJava public class programMain extends program{ … public String toC(){ return (clas.toC()+mainn.toC()); } } … //every class contains the method “toC()” public class formalListApp extends formalList{ … public String toC() { return (clas.toC()+" *this, "+t.toC()+' '+id.toC()+' '+fr.toC()); } }
Final product • Lexical analyze of the whole java file • Scan the source code to verify that it belongs to the language of miniJava grammar • Store the structure of the file into the abstract syntax tree • Convert it into a syntax tree of C • The target code can be compiled and run on multi-back-end
Utility • E-business Web Site • Industrial control software • Data-base arrangement • Java developing on embedded system
Not the end • It’s really a funny thing to construct a Java compiler with Java language • Semantic analysis & Intermediate language unfinished • We will go on with this topic