Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier...

16
Complier Implementation in JAVABy 10072130154 程栋梁

Transcript of Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier...

Page 1: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

Complier

Implementation in JAVA™

By 10072130154 程栋梁

Page 2: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

Introduction• Pascal-like grammar:

Initial PL/x grammar has been extended to cover the full Pascal

grammar.

• Graphic User Interface

User interface is implemented with JAVA™ swing framework.

Functionally, the compiler has syntax highlighting, Abstract Syntax Tree view , generated instructions view , data stack view and debug model that can pause after every single instruction.

• ToolUsing JavaCC™ to help, instead of LEX & YACC.

Page 3: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

About JavaCC• Java Compiler-Compiler

Formerly named Jack (rhymes with YACC), similar to YACC in

that it generates a parser from a formal grammar written in EBNF notation, except the output is Java source code.

• LL(K) rather than LR(1)Unlike YACC, however, JavaCC generates top-down parsers,

which limits it to the LL(k) class of grammars (in particular, left recursion cannot be used).

• Abstract Syntax Tree with JJTreeThe tree builder that accompanies it, JJTree, constructs its

trees from the bottom up.

Page 4: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

32 Key Words

array, begin, break, case, char, const, continue, do, downto, else, end, exit, false, for, function, if, integer, logical, of, procedure, program, read, real, repeat, result, then, to, true, until, var, while, write

Page 5: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

Identifier Types

• IntegerAutomatically cast to real when calculating with a real

number

• RealWhen a real number assigned to an integer ID, it is forced

to discard the fractional part.

• Logical

• ArrayOf any dimension and index

Page 6: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

Function and procedure

• Function

With return value

• Procedure

Without return value

• Parameters

Passed by value

• Check in symbol tableCheck name along with Parameters types

• Nested definition

Page 7: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

Statements• assignment

• if..then..[else..]

• repeat...until..

• do..while..

• for..to/downto..do..

• case..of..

• break/continue in loop block

• exit in procedure

• read

• write

Page 8: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

Following pages are some screenshots and my illustrations in

RED and Italic

Page 9: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

Menus and buttons

Code pane

Output pane

AST view

Instructions view and stack view

Page 10: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

Create a new or open an existing code.

Syntax highlighting:4 different colors forkeywords, numbers,comments and others

Page 11: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

After parsing

Outputs indicates parsed successfully.

AST view show the program’s structure.

Page 12: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

After compiling

Generated instructions

Page 13: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

Stack view in debug

Debug in one step

Page 14: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

nested functions

Page 15: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

case statement

Page 16: Complier - East China Normal Universityldou/8studentswork/10072130154.pdf · Complier Implementation in JAVA™ ... Identifier Types •Integer Automatically cast to real when calculating

Functions with the same name but different parameters Array of multi-dimension