Mini Java Compiler (3)

xiaoxiao2021-03-06  14

Third, property translation literacy

Main Classification (Mainclass)

:: = "Class"

@ FillMemory ↓

@ FillTable ↓ ,

@ FillTree ↓

"{" "public" "static" "void" "main" "(" String "" ["]"

, = , = StringArrayType

@ FillTable ↓ ,

"{" "}" "}"

Class Declaration

:: = "class"

@ CheckTable ↓

@ FillMemory ↓

@ FillTable ↓ ,

@ FillTree ↓ =

"{" (MethodDeclaration) * "}"

Class inheritance declaration (ClassExtendsDeclaration)

:: = "class"

@ CheckTable ↓

"extend"

@ CheckTree ↓

@ FillMemory ↓

@ FillTable ↓ , ,

@ FillTree ↓ = , =

"{" (MethodDeclaration) * "}"

Variable Declaration

:: =

, ";"

@ CheckTable ↓ , @ FillMemory ↓

@ FillTable ↓ , , ,

:: = = arraytype

| = Booleantype

| = Integertype

:: = = ObjectType

@ CheckTable ↓

, ";"

@ CheckTable ↓ ,

@ FillTable ↓ , ,

Method declaration (MethodDeclaration)

:: = "public"

,

@ CheckTable ↓ ,

@ FillMemory ↓

@ FillTable ↓ , , ,

"(" ()? ")"

"{" () * () * "Return"

,

@ CHECKTYPE ↓ , ";"} "

:: = = arraytype

| = Booleantype

| = Integertype

:: =

() *

:: =

, @ checktable ↓ ,

@ FillTable ↓ ,

:: = "public" = ObjectType

@ CheckTable ↓

,

@ CheckTable ↓ ,

@ FillMemory ↓

@ FillTable ↓ , , ,

"(" ()? ")"

"{" () * () * "Return"

,

@ CHECKTYPE ↓ , ";"} "

:: = = arraytype

| = Booleantype

| = Integertype

:: =

() *

:: = = ObjectType

@ CheckTable ↓

,

@ CheckTable ↓ ,

@ FillMemory ↓

@ FillTable ↓ , ,

:: = ","

AssignmentStatement

:: = ↑ id_name @ Checktable ↓

"=" , ";" @ checktype ↓ ,

@generatecode ↓ = stmt_assign,

ArrayasSignmentStatement in array element

:: = @ Checktable ↓

@ CheckType ↓ , arraytype

"[" ,

@ CheckType ↓ , integertype

"]" "=" ,

@ CHECKTYPE ↓ , integertype ";"

@generatecode ↓ = stmt_Array_assign,

IF statement (ifstatement)

: = "if" (" ,

@ CheckType ↓ , booleantype

")" "else"

@generatecode ↓ = stmt_if,

While statement (Whilestatemen)

:: = "while" (" ,

@ CheckType ↓ , booleantype

")"

@generatecode ↓ = stmt_while,

Print statement (PrintStatement)

:: = "System.out.println" ("

,

@ CheckType ↓ , integertype

")" ""; "

@generatecode ↓ = stmt_print,

Property and action explanation

action

Action @ CheckTable ↓ , Check the legitimacy of the identifier (traversal symbol table) @ checktable ↓ Check if the class name is declared @ checktable ↓ Check if the variable has declared If the return variable type @ checktable ↓ check the type of @ FillTable ↓ , Put the identifier insert symbol table @ FillTable ↓ , , will Class Insert symbol table, and copy the method of the parent class and class variables to the subclass @ FillTable ↓ , to insert the identifier into the symbol table @ FillTable ↓ , , , Insert the identifier into the symbol table @ FillMemory ↓ registration memory, return address @ generatecode ↓ , Generate pseudo code @ checktype ↓ , Check type legality ( Traversing inheritance tree, symbolic table) @ checktype ↓ , Check the variable type and expression type match @ checktree ↓ , Check if the child_name is a subclass of parent_name (traversing inheritance tree) @ checkTree ↓ check child_name already exists in the inheritance within the tree @ fillTree ↓ , the child_name inserted inheritance tree parent_name lower @ fillTree ↓ the node_name inserted inheritance tree (parent class Object) Attributes

Attribute data types (for Java language) meaning id_nameString id_belongsToString identifier name belongs level hierarchy name id_levelint identifiers (see table) id_addressint memory address stmt_typeint type of operation (see table) class_nameString class name extra_paraObject additional parameters child_nameString subclass name Object type expression parent_nameString class name parent name class_nameString id_typecompiler.type.Type type variable type (see table) para_listjava.util.List boolean parameter table exp_valueint or program or any defined value expression type exp_typecompiler.type.Type

Symbol table structure

Name Data Type (For Java Language) Meaning NameString Identifier Name SortString Identifier Category TypeCompiler.Type.Type Variable Type Belongstostring Home Information (see Schedule) Addressint storage address

Memory allocation table structure

Name Data Type (For Java Language) Meaning Addressint Identifier Address SortString Identifier Category NameString Identifier Name ValueObject Identifier Value Schedule

STMT_TYPE

Name Value STMT_ASSIGN1 Assignment Statement STMT_ARRAY_ASIGN2 Array Element Assignment Footword STMT_IF3IF Statement STMT_WHILE4WHILE Statement STMT_PRINT5 Print Statement

ID_TYPE

Class Meaning Size (Bytes) ArrayType Integer Array Type 4Booleantype Boolean Type 1integertype Integer Type 4 ObjectType Object Type Uns OK NonvariableType Non-Variable 0

ID_Belongsto

Identifier Type "BelongSTO" property content class is not (analysis, file-based) method class name class variable class name method internal variable method name parameter method name

转载请注明原文地址:https://www.9cbs.com/read-45978.html

New Post(0)