Mini Java Compiler (III)

Source: Internet
Author: User
Compile three, attribute translation grammar
Main class declaration (MainClass)
<MainClass>:: = "Class" <Identifier>↑<id_name1>



@fillMemory ↓<id_name1>↑<id_address>

@fillTable ↓<id_name1>,<id_address>



@fillTree ↓<id_name1>



' {' Public ' ' static ' ' void ' ' main ' ' (' String ' ' ['] '

<identifier>↑<id_name2>,<id_belongsto>=<id_name1>,<id_type>=stringarraytype



@fillTable ↓<id_name2>,<id_belongsto>



"{" <PrintStatement> "}" "}"





class declaration (ClassDeclaration)
<ClassDeclaration>:: = "Class" <Identifier>↑<id_name>



@checkTable ↓<id_name>



@fillMemory ↓<id_name1>↑<id_address>



@fillTable ↓<id_name>,<id_address>



@fillTree ↓<node_name>=<id_name>



"{" (vardeclaration) * (methoddeclaration) * "}"





Class inheritance declaration (Classextendsdeclaration)
<ClassExtendsDeclaration>:: = "Class" <Identifier>↑<id_name1>



@checkTable ↓<id_name1>



"Extends" <Identifier>↑<id_name2>



@checkTree ↓<id_name2>



@fillMemory ↓<id_name1>↑<id_address>



@fillTable ↓<id_name1>,<id_name2>,<id_address>



@fillTree ↓<child_name>=<id_name1>,<parent_name>=<id_name2>



"{" (vardeclaration) * (methoddeclaration) * "}"





Variable declaration (vardeclaration)
<VarDeclaration>:: = <Type>↑<id_type>



<Idetifier>↑<id_name>,<id_belongsTo> ";"

@checkTable ↓<id_name>,<id_belongsto>



@fillMemory ↓<id_name>↑<id_address>



@fillTable ↓<id_name>,<id_type>,<id_belongsto>,<address>







<Type>↑<id_type>:: = <arraytype>↑<id_type>=arraytype

|<booleantype>↑<id_type>=booleantype

|<integertype>↑<id_type>=integertype







<VarDeclaration>:: = <type>↑<id_type>=objecttype



@checkTable ↓<id_type>



<Idetifier>↑<id_name>,<id_belongsTo> ";"

@checkTable ↓<id_name>,<id_belongsto>

@fillTable ↓<id_name>,<id_type>,<id_belongsto>







Method declaration (MethodDeclaration)
<MethodDeclaration>:: = "public" <Type>↑<id_type>



<Identifier>↑<id_name>,<id_belongsTo>



@checkTable ↓<id_name>,<id_belongsto>



@fillMemory ↓<id_name>↑<id_address>



@fillTable ↓<id_name>,<id_type>,<id_belongsto>,<id_address>



"(<FormalParameterList>)") "

"{" (<VarDeclaration>) * (<Statement>) * "Return"

<Expression>↑<exp_type>,<exp_value>



@checkType ↓<id_type>,<exp_type> ";" "}"





<Type>↑<id_type>:: = <arraytype>↑<id_type>=arraytype

|<booleantype>↑<id_type>=booleantype

|<integertype>↑<id_type>=integertype







<FormalParameterList>:: = <FormalParameter>

(<FormalParameterRest>) *





<FormalParameter>:: = <Type>↑<id_type>



<Identifier>↑<id_name>,<id_belongsTo>



@checkTable ↓<id_name>,<id_belongsto>



@fillTable ↓<id_name>,<id_type>







<MethodDeclaration>:: = "public" <type>↑<id_type>=objecttype



@checkTable ↓<id_type>



<Identifier>↑<id_name>,<id_belongsTo>



@checkTable ↓<id_name>,<id_belongsto>



@fillMemory ↓<id_name>↑<id_address>



@fillTable ↓<id_name>,<id_type>,<id_belongsto>,<id_address>



"(<FormalParameterList>)") "

"{" (<VarDeclaration>) * (<Statement>) * "Return"

<Expression>↑<exp_type>,<exp_value>



@checkType ↓<id_type>,<exp_type> ";" "}"





<Type>↑<id_type>:: = <arraytype>↑<id_type>=arraytype

|<booleantype>↑<id_type>=booleantype

|<integertype>↑<id_type>=integertype







<FormalParameterList>:: = <FormalParameter>

(<FormalParameterRest>) *





<FormalParameter>:: = <type>↑<id_type>=objecttype



@checkTable ↓<id_type>



<Identifier>↑<id_name>,<id_belongsTo>



@checkTable ↓<id_name>,<id_belongsto>



@fillMemory ↓<id_name>↑<id_address>

@fillTable ↓<id_name>,<id_type>,<id_address>







<FormalParameterRest>:: = "," <FormalParameter>





Assignment Statement (assignmentstatement)
<AssignmentStatement>:: = <identifier>↑id_name @checkTable ↓<id_name>↑<id_type>

"=" <Expression>↑<exp_type>,<exp_value> ";"

@checkType ↓<id_type>,<exp_type>



@generateCode ↓<stmt_type>=stmt_assign,<extra_para>





Array element assignment statement (arrayassignmentstatement)
<ArrayAssignmentStatement>:: = <Identifier>↑<id_name> @checkTable ↓<id_name>↑<id_type >



@checkType ↓<id_type>,arraytype



"[" <Expression>↑<exp_type1>,<exp_value1>



@checkType ↓<exp_type1>,integertype



"]" "=" <Expression>↑<exp_type2>,<exp_value2>



@checkType ↓<exp_type2>,integertype ";"

@generateCode ↓<stmt_type>=stmt_array_assign,<extra_para>





If statement (ifstatement)
<IfStatement>: = "If" "(" <Expression>↑<exp_type>,<exp_value>



@checkType ↓<exp_type>,booleantype



")" <Statement> "Else" <Statement>

@generateCode ↓<stmt_type>=stmt_if,<extra_para>





While statement (Whilestatemen)
<WhileStatement>:: = "while" ("<Expression>↑<exp_type>,<exp_value>



@checkType ↓<exp_type>,booleantype



")" <Statement>

@generateCode ↓<stmt_type>=stmt_while,<extra_para>





Print statement (printstatement)
<PrintStatement>:: = "System.out.println" "("

<Expression>↑<exp_type>,<exp_value>



@checkType ↓<exp_type>,integertype



")" ";"

@generateCode ↓<stmt_type>=stmt_print,<extra_para>





Attributes and action explanations
Action
Action


Role



@checkTable ↓<id_name>,<id_level>


Check the legality of identifiers (Traverse symbol table)

@checkTable ↓<class_name>


Check whether the class name is declared

@checkTable ↓<id_name>↑<id_type>


Check whether the variable has been declared, and if the return variable type is declared

@checkTable ↓<id_type>


Check whether the type has been declared

@fillTable ↓<id_name1>,<id_address>


Insert an identifier into a symbol table

@fillTable ↓<child_name>,<parent_name>,<id_address>


Inserts a subclass into the symbol table while copying the parent class's methods and class variables to subclasses

@fillTable ↓<id_name>,<id_belongsto>


Insert an identifier into a symbol table

@fillTable ↓<id_name>,<id_sort>,<id_level>,<id_type>


Insert an identifier into a symbol table

@fillMemory ↓<id_name>↑<id_address>


Register memory, return address

@generateCode ↓<stmt_type>,<extra_para>


Generate pseudo Code

@checkType ↓<child_name>,<parent_name>


Check type legality (traverse inheritance tree, symbol table)

@checkType ↓<id_type>,<exp_type>


To check whether a variable type matches an expression type

@checkTree ↓<child_name>,<parent_name>


Check whether Child_name is a subclass of parent_name (Traverse inheritance tree)

@checkTree ↓<class_name>


Check if Child_name already exists in the inheritance tree

@fillTree ↓<child_name>,<parent_name>


Insert Child_name into the inheritance tree parent_name

@fillTree ↓<node_name>


Insert Node_name into the inheritance tree (the parent class is object)







Property
Property


Data type (for the Java language)


Meaning



Id_name


String
Identifier name

Id_level


Int
Hierarchical structure of identifiers

Id_belongsto


String
Hierarchy name, (see annex)

Id_address


Int
Memory address

Stmt_type


Int
Type of operation (see appendix)

Class_name


String
Class name

Extra_para


Object
Additional parameters

Child_name


String
Child class name

Parent_name


String
Parent class Name

Class_name


String
Class name

Id_type


Compiler.type.Type
Variable type type (see appendix)

Para_list


Java.util.List
Parameter table

Exp_value


An int or a Boolean or a defined object type in any program
An expression value

Exp_type


Compiler.type.Type
Type of expression







Symbol table Structure
Name


Data type (for the Java language)


Meaning



Name


String
Identifier name

Sort


String
Identifier category

Type


Compiler.type.Type
Variable type

Belongsto


String
Attribution information (see appendix)

Address


Int
Store Address







Memory allocation Table structure
Name


Data type (for the Java language)


Meaning



Address


Int
Identifier Address

Sort


String
Identifier category

Name


String
Identifier name

Value


Object
Identifier value







Schedule
Stmt_type
Name


Value


Meaning



Stmt_assign


1
Assignment statement

Stmt_array_assign


2
Array element Assignment statement

Stmt_if


3
If statement

Stmt_while


4
While statement

Stmt_print


5
Print Statements







Id_type
Class


Meaning


Size (Word length (bytes))



ArrayType


Integer array type
4

Booleantype


Boolean type
1

Integertype


Type of integer
4

ObjectType


Object type
Not sure

Nonvariabletype


Non-variable
0







Id_belongsto
Identifier type


"Belongsto" Property content



Class
None (in file as an analysis)

Method
Class name

class variables
Class name

Method Internal variables
Method name

Parameters
Method name










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.