Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "MiniJava Compiler A multi-back-end JIT compiler of Java."— 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 → class id { public static void main ( String [] id ) { Statement }} ClassDecl → class id { VarDecl* MethodDecl* } → class id extends id { VarDecl* MethodDecl* } … Exp → Exp op Exp → Exp [ Exp ] → Exp. length → Exp. id ( ExpList ) → INTEGER LITERAL → true → false → id → this → new int [ Exp ] → new id () → ! Exp → ( Exp )

12 AST of MiniJava Program

13

14

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!


Download ppt "MiniJava Compiler A multi-back-end JIT compiler of Java."

Similar presentations


Ads by Google