⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java.y

📁 java interpreter in formal language and translators
💻 Y
字号:
%{

%}

%token OP_ADD%token OP_SUB%token OP_MUL%token OP_DIV
%token OP_EQ 

%token OP_LE 

%token OP_GE 

%token OP_NE

%token  OP_Logical_OR

%token OP_Logical_AND

%token OP_INC
 
%token OP_DEC

%token CLASS 

%token ABSTRACT

%token IMPLEMENTS 

%token INTERFACE

%token EXTENDS

%token PACKAGE 

%token IMPORT 

%token PUBLIC

%token STATIC 

%token PRIVATE 

%token PROTECTED 

%token FINAL 

%token RETURN

%token VOID

%token INT

%token CHAR

%token BOOLEAN 

%token SUPER 

%token THIS 

%token JNULL
 
%token IF 

%token ELSE 

%token WHILE

%token DO

%token FOR

%token OPERATOR

%token OP_DIM

%token IDENTIFIER 

%token LITERAL 

%token BOOLLIT

%start CompilationUnit

%%

/*declare the primitive types*/
PrimitiveType
	: BOOLEAN
	| CHAR
	| INT
	| VOID
	;

/*the name for a method/class/interface*/
QualifiedName
	: IDENTIFIER
	| QualifiedName '.' IDENTIFIER
	;

/*the name could be a primitive type or a name for methods/classes*/
TypeName
	: PrimitiveType
	| QualifiedName
	;


ClassNameList  /*for the class who implements more interfaces*/
        : QualifiedName
        | ClassNameList ',' QualifiedName
	;

/*access modifiers*/
Modifier
	: ABSTRACT
	| FINAL
	| PUBLIC
	| PROTECTED
	| PRIVATE
	| STATIC
	;

/*could more access modifiers for just one class*/
Modifiers
	: Modifier
	| Modifiers Modifier
	;

/*a class coulg be class or interface*/
ClassWord
	: CLASS
	| INTERFACE
	;

/*when a class implements an interface*/
Interfaces
	: IMPLEMENTS ClassNameList
	;

/*when a class extends an interface*/
Extends
	: EXTENDS TypeName
	| Extends ',' TypeName
	;
/*the declaration of a class*/
ClassHeader
   	:           ClassWord IDENTIFIER
      |           ClassWord IDENTIFIER Extends
	|           ClassWord IDENTIFIER       Interfaces
      |           ClassWord IDENTIFIER Extends Interfaces
	| Modifiers ClassWord IDENTIFIER
      | Modifiers ClassWord IDENTIFIER Extends
	| Modifiers ClassWord IDENTIFIER       Interfaces
	| Modifiers ClassWord IDENTIFIER Extends Interfaces
	;

/*package declaration*/
PackageStatement
	: PACKAGE QualifiedName ';'
	;

/*import declaration*/
ImportStatement
	: IMPORT QualifiedName ';'
	| IMPORT QualifiedName '.' '*' ';'
	;
/*could be more import statements*/
ImportStatements
	: ImportStatement
	| ImportStatements ImportStatement
	;

/*the dimension used for the variable declarations (array)*/
Dims
	: OP_DIM
	| Dims OP_DIM
	;

/*we declare an array or a simple variable*/
TypeSpecifier
	: TypeName
	| TypeName Dims
	;

/*for the declaration (1.simple variable, 2. array)*/
DeclaratorName
	: IDENTIFIER
        | DeclaratorName OP_DIM
        ;

/*declare parameter for method*/
Parameter
	: TypeSpecifier DeclaratorName
	;

/*a method can get more parameters */
ParameterList
	: Parameter
	| ParameterList ',' Parameter
	;

/*declare a method using the parameter list also*/
MethodDeclarator
	: DeclaratorName '(' ParameterList ')'
	| DeclaratorName '(' ')'
	| MethodDeclarator OP_DIM
	;

/*using also modifiers*/
MethodDeclaration
	: Modifiers TypeSpecifier MethodDeclarator  MethodBody
	|           TypeSpecifier MethodDeclarator  MethodBody
	;


/*the body of a method is a block of instructions*/
MethodBody
	: Block
	| ';'
	;

/*in block we declare the variable and we have statements, or could be an empty block*/
Block
	: '{' LocalVariableDeclarationsAndStatements '}'
	| '{' '}'
        ;

/*case when we declare variable and statements*/
LocalVariableDeclarationsAndStatements
	: LocalVariableDeclarationOrStatement
	| LocalVariableDeclarationsAndStatements LocalVariableDeclarationOrStatement
	;

/*we declare a variable or we have a statement*/
LocalVariableDeclarationOrStatement
	: LocalVariableDeclarationStatement
	| Statement
	;

/*declaring a variable*/
VariableDeclarator
	: DeclaratorName;

/*we could declare one or more variables*/
VariableDeclarators
	: VariableDeclarator
	| VariableDeclarators ',' VariableDeclarator
	;

/*now we declare and at the end we put ;*/
LocalVariableDeclarationStatement
	: TypeSpecifier VariableDeclarators ';'
	;

FieldVariableDeclaration
	: Modifiers TypeSpecifier VariableDeclarators
	|           TypeSpecifier VariableDeclarators
	;


/*and now the statement could be:*/
Statement
	: EmptyStatement
	| ExpressionStatement ';'
        | SelectionStatement
        | IterationStatement
	| Block
	;

EmptyStatement
	: ';'
        ;

ExpressionStatement
	: Expression
	;

ExpressionStatements
	: ExpressionStatement
	| ExpressionStatements ',' ExpressionStatement
	;


SelectionStatement
	: IF '(' Expression ')' Statement
        | IF '(' Expression ')' Statement ELSE Statement
       ;

IterationStatement
	: WHILE '(' Expression ')' Statement
	| DO Statement WHILE '(' Expression ')' ';'
	| FOR '(' ForInit ForExpr ForIncr ')' Statement
	| FOR '(' ForInit ForExpr         ')' Statement
	;

Operators
      :  Expression OP_EQ Expression
      |  Expression OP_LE Expression
      |  Expression OP_GE Expression
      |  Expression OP_NE Expression
      |  Expression OP_Logical_OR Expression
      |  Expression OP_Logical_AND Expression
      |  Expression OP_INC Expression
      |  Expression OP_DEC Expression
      ;

Expression
	: Expression OP_ADD Expression  {$1=$0 + $2;}        | Expression OP_SUB Expression  {$1=$0 - $2;}
        | Expression OP_MUL Expression  {$1=$0 * $2;}         | Expression OP_DIV Expression  {$1=$0 / $2;}
        | LITERAL
      ;
 

ForInit
	: ExpressionStatements ';'
	| LocalVariableDeclarationStatement
	| ';'
	;

ForExpr
	: Expression ';'
	| ';'
	;

ForIncr
	: ExpressionStatements
	;

/*calling a method*/

SpecialName
	: THIS
	| SUPER
	| JNULL
	;

MethodAccess
	: SpecialName
	| QualifiedName
	;

MethodCall
	: MethodAccess '(' ArgumentList ')'
	| MethodAccess '(' ')'
	;

ArgumentList
	: Expression
	| ArgumentList ',' Expression
	;

/*static modifier*/

StaticInitializer
	: STATIC Block
	;

NonStaticInitializer
        : Block
        ;

/*constructor*/

ConstructorDeclarator
	: IDENTIFIER '(' ParameterList ')'
	| IDENTIFIER '(' ')'
	;

ConstructorDeclaration
	: Modifiers ConstructorDeclarator  Block
	|           ConstructorDeclarator  Block
	;


/*all declarations*/

TypeDeclaration
	: ClassHeader '{' FieldDeclarations '}'
	| ClassHeader '{' '}'
	;

FieldDeclaration
	: FieldVariableDeclaration ';'
	| MethodDeclaration
	| ConstructorDeclaration
	| StaticInitializer
      | NonStaticInitializer
      | TypeDeclaration
	;

TypeDeclarations
	: TypeDeclaration
	| TypeDeclarations TypeDeclaration
	;


FieldDeclarations
	: FieldDeclaration
        | FieldDeclarations FieldDeclaration
	;

/*the main program*/

CompilationUnit
	: ProgramFile
        ;

ProgramFile
	: PackageStatement ImportStatements TypeDeclarations
	| PackageStatement ImportStatements
	| PackageStatement                  TypeDeclarations
	|                  ImportStatements TypeDeclarations
	| PackageStatement
	|                  ImportStatements
	|                                   TypeDeclarations
	;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -