Compiler Term Project - PowerPoint PPT Presentation

About This Presentation
Title:

Compiler Term Project

Description:

Compiler Term Project MilkTea a simplified C to Java Compiler NTUCSIE, B86506010 B86506054 – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 20
Provided by: csie2
Category:

less

Transcript and Presenter's Notes

Title: Compiler Term Project


1
Compiler Term Project
  • MilkTea
  • a simplified C to Java Compiler
  • NTUCSIE,
  • B86506010 ???
  • B86506054 ???

2
Scanner
  • ??? Scanner ?? flex ??
  • ??????
  • ???????Hand Coding,??????flex???,????regular
    expression,???escape character???(?\\?\
    ),??????code???????
  • ????????escape character

3
Scanner(continue)
  • ?? Identifier ??,???? Parser ???? Conflict, ????
    Semantic-Feedback Scanning,?? Scanning ????
    Semantic ??????????,???IDENTIFIER?FIDENTIFIER?TIDE
    NTIFIER,code????

4
Scanner(continue)
  • a-zA-Z_a-zA-Z0-9_
  • yylval.StringValue new
    TString(yytext)
  • if (IsTypeName(yylval.StringValue)
    )
  • return TIDENTIFIER
  • else if (IsFuncName(yylval.StringV
    alue))
  • return FIDENTIFIER
  • else
  • return IDENTIFIER

5
Scanner(continue)
  • System support functions ? scanner ??????,????
    Symbol Table ?????????,? printint, printfloat,
    malloc ?,??????????

6
Parser
  • Parser ?? bison ??,???? LR-parsing,?????? C ?????
    Conflict,????
  • If-then-else,? Dangling-else ?????
    bison(yacc)?????? precedence ????????? ELSE (?
    shift)? Rule ?????? ELSE (? reduce)?????

7
Parser(continue)
  • Struct Dec List, Func Dec List, GlobalVar Dec
    List, Func Body List???,??????????(struct
    IDENTIFIER)?? parser ?? lookahead ??????????
    reduce/reduce conflict????? Grammar
  • Program StructDecList FuncDecList
    GlobalVarDecList FuncBodyList
  • StructDecList Lambda StructDecList StructDec
  • ???? LR Grammar,?????? Modify ???? Grammar

8
Parser(continued)
  • Program InfoList
  • InfoList Lambda InfoList Info
  • Info StructDec FuncDec GlobalVarDec
    FuncBody
  • ??? Semantic ??? check ??? Program ?????????????

9
Parser(continued)
  • ???? Pointer ?????,?????????? Conflict
  • Complex x
  • Ab
  • ??? C ????????????,????? scanner ??? IDENTIFIER
    ???????????? Semantics ???????
  • struct Complex x

10
Semantics
  • Semantics ?????????,??? SymbolTable ???????
  • ??? Symbol Table ????? Hash Table,??? Scope ?????
    Symbol Table ????,?? Variable binding ???????
    Display ????????
  • ? Type?GlobalVar?FuncDec??? GlobalSymbTbl ?,????
    Scanner ? IDENTIFIER ??????
  • Symbol Table ??? Symbol Attribute ??
    Object-oriented ????? RTTI ???? Attribute ?????
    Symbol

11
Semantics(continued)
  • Type ???? Symbol Table ?? Key ???,????
    Object-oriented ? RTTI???????????? Type ? Pointer
    Attribute ?? Original Type ??????(???? Original
    Type ?,Primitive Type/Struct Type ????
    TypeWithPointer)
  • Array Type ???? Base ? Length,????? Pointer

12
Semantics(continued)
  • ????? Expression???? Expression ????
  • ??? Java VM ???,? Stack Variable
    ?????????????,????????? Local Variable?
  • ????????,?? Expression SubTerm ??????????Stack(???
    ????????)???????,???? Do Operation
    ???,?????????,?? Modify ? Action Symbol ???

13
Semantics(continued)
  • Add Exp1 Exp1 need to be generated here
    Exp2 Exp2 generated, do add
  • ?????????,?? Exp1 ? Exp2 ? Type ??? Exp1 ?? Type
    Coercion ???? do add ???? Exp1 Swap ???do
    coercion?Swap ???do add?
  • ??,?? Expression Record ????? Expression
    ???(Var?Owner on Stack ?Field?Array Info on Stack
    ? ArrayPos?Value on Stack)????? Expression
    ???????(?? Assignment ?? Load)

14
Semantics(continued)
  • ?? C ?,?????? Assignment ??? Expression,?? JVM
    ?,?? Assign ???????? Stack ?,???? Modify ? Assign
    ?????
  • Make sure the location of assgnment is known
  • Dup the location of assignment
  • Do assign
  • Do load

15
Semantics(continued)
  • ??,C ?? Logic Expression ?? Short-cut
    Evaluation,?? Evaluate Logic Expression
    ????????????????????????? if ? label ????
  • ????? Control Structure????? Code ?????,?????
    Control Structure ? Code Generation ?????? C
    ????????,??????

16
Semantics(continued)
  • if(Exp) State
  • Exp0 .EndIf
  • State
  • EndIf
  • if(Exp) State1 else State2
  • Exp0 . EndIf
  • State1
  • EndIf
  • . EndElse
  • StartElse
  • State2
  • EndElse

17
  • while(Exp) State
  • StartWhile(continue)
  • Exp0 . EndWhile
  • State
  • . StartWhile
  • EndWhile(break)
  • do State while(Exp)
  • StartDo(continue)
  • State
  • Exp!0 . StartDo
  • EndDo(break)

18
Semantics(continue)
  • for(E1E2E3) State
  • E1
  • Check
  • E2
  • . StartFor/EndFor
  • UpDate(continue)
  • E3
  • . Check
  • StartFor
  • State
  • . UpDate
  • EndFor(break)
  • switch(E1) Lstate
  • Branched as many ifs
  • break to end of switch

19
Semantics
  • ??????? Semantics ???
  • Object Allocation as in C
  • Copy initializer of structures similar to C
  • Struct Type Pointer accessing
Write a Comment
User Comments (0)
About PowerShow.com