1 / 21

MiniJava Compiler

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.

bly
Download Presentation

MiniJava Compiler

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MiniJava Compiler A multi-back-end JIT compiler of Java

  2. Team • Instructor 华保健 • Participants 徐波 孙浩 裴达凯 范琳 张腾宇

  3. What we have achieved • What is unfinished yet • What we will go on with

  4. Our original plan • Lexical analysis • Parsing • Type check • Translation

  5. Our accomplishment • Lexical analysis • Parsing • AST disposal • Translation

  6. 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"

  7. 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); } …

  8. 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

  9. 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); :}

  10. 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

  11. 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 )

  12. AST of MiniJava Program

  13. AST of MiniJava Program

  14. AST of MiniJava Program

  15. 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

  16. 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

  17. 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()); } }

  18. 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

  19. Utility • E-business Web Site • Industrial control software • Data-base arrangement • Java developing on embedded system

  20. 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

  21. Thank you!

More Related