// Java1.1.cd developed by Binoy Samuel // Uses the lookahead notation of the Java Compiler Compiler // Allows for adaptive manipulation of Java programs /***************************************** * THE JAVA LANGUAGE GRAMMAR STARTS HERE * *****************************************/ import edu.neu.ccs.demeter.Ident ; import edu.neu.ccs.demeterf.* ; import edu.neu.ccs.demeterf.util.*; import java.lang.* ; /* * Program structuring syntax follows. */ CompilationUnit = [PackageDeclaration] ImportDeclarations TypeDeclarations . PackageDeclaration = "package" Name ";" . ImportDeclarations ~ {ImportDeclaration} . ImportDeclaration = "import" Name [ DotStar ] ";" . DotStar = ".*" . TypeDeclarations ~ {TypeDeclaration} . TypeDeclaration : *lookahead*(@ ( "abstract" | "final" | "public" )* "class" @) ClassDeclaration | InterfaceDeclaration | SemiColonTD . SemiColonTD = SemiColon . /* * Declaration syntax follows. */ ClassDeclaration = ClassModifiers UnmodifiedClassDeclaration . ClassModifiers ~ {ClassModifier} . ClassModifier : CM_Abstract | CM_Final | CM_Public . // Following style in order to get // around lack of multiple inheritance CM_Abstract = CAbstract . CM_Final = CFinal . CM_Public = CPublic . UnmodifiedClassDeclaration = "class" Ident ["extends" Name] ["implements" NameList] ClassBody . ClassBody = "{" ClassBodyDeclarations "}" . ClassBodyDeclarations ~ {ClassBodyDeclaration} . NestedClassDeclaration = NestedClassModifiers UnmodifiedClassDeclaration . NestedClassModifiers ~ {NestedClassModifier} . NestedClassModifier : NCM_Static | NCM_Abstract | NCM_Final | NCM_Public | NCM_Protected | NCM_Private . NCM_Static = CStatic . NCM_Abstract = CAbstract . NCM_Final = CFinal . NCM_Public = CPublic . NCM_Protected = CProtected . NCM_Private = CPrivate. ClassBodyDeclaration : *lookahead*(@2@) Initializer | *lookahead*(@("static"|"abstract"|"final"|"public"|"protected"|"private")*"class"@) CBD_NestedClassDeclaration | *lookahead*(@("static"|"abstract"|"final"|"public"|"protected"|"private")*"interface"@) CBD_NestedInterfaceDeclaration | *lookahead*(@["public"|"protected"|"private"] _Name() "("@) ConstructorDeclaration | *lookahead*(@("public"|"protected"|"private"|"static"|"abstract"|"final"|"native"|"synchronized")* _ResultType() _Identifier() "("@) CBD_MethodDeclaration | CBD_FieldDeclaration . CBD_NestedClassDeclaration = NestedClassDeclaration . CBD_NestedInterfaceDeclaration = NestedInterfaceDeclaration . CBD_MethodDeclaration = MethodDeclaration . CBD_FieldDeclaration = FieldDeclaration . InterfaceDeclaration = InterfaceModifiers UnmodifiedInterfaceDeclaration . InterfaceModifiers ~ {InterfaceModifier} . InterfaceModifier : IM_Abstract | IM_Public . IM_Abstract = CAbstract . IM_Public = CPublic . NestedInterfaceDeclaration = NestedInterfaceModifiers UnmodifiedInterfaceDeclaration . NestedInterfaceModifiers~ {NestedInterfaceModifier} . NestedInterfaceModifier : NIM_Static | NIM_Abstract | NIM_Final | NIM_Public | NIM_Protected | NIM_Private . NIM_Static = CStatic . NIM_Abstract = CAbstract . NIM_Final = CFinal . NIM_Public = CPublic . NIM_Protected = CProtected . NIM_Private = CPrivate. UnmodifiedInterfaceDeclaration = "interface" Identifier [ "extends" NameList ] "{" InterfaceMemberDeclarations "}" . InterfaceMemberDeclarations ~ {InterfaceMemberDeclaration} . InterfaceMemberDeclaration : *lookahead*(@("static"|"abstract"|"final"|"public"|"protected"|"private")* "class"@) IMD_NestedClassDeclaration | *lookahead*(@("static"|"abstract"|"final"|"public"|"protected"|"private")* "interface"@) IMD_NestedInterfaceDeclaration | *lookahead*(@("public"|"protected"|"private"|"static"|"abstract"|"final"|"native"|"synchronized")* _ResultType() _Identifier() "("@) IMD_MethodDeclaration | IMD_FieldDeclaration . IMD_NestedClassDeclaration = NestedClassDeclaration . IMD_NestedInterfaceDeclaration = NestedInterfaceDeclaration . IMD_MethodDeclaration = MethodDeclaration . IMD_FieldDeclaration = FieldDeclaration . FieldDeclaration = FieldModifiers Type VariableDeclarators";" . FieldModifiers ~ {FieldModifier} . FieldModifier : FM_Public | FM_Protected | FM_Private | FM_Static | FM_Final | FM_Transient | FM_Volatile . FM_Public = CPublic . FM_Protected = CProtected . FM_Private = CPrivate . FM_Static = CStatic . FM_Final = CFinal . FM_Transient = CTransient . FM_Volatile = CVolatile . VariableDeclarators ~ VariableDeclarator {","VariableDeclarator}. VariableDeclarator = VariableDeclaratorId ["=" VariableInitializer] . VariableDeclaratorId = Identifier SquareBrackets . SquareBrackets ~ {*lookahead*(@2@) SquareBracket} . SquareBracket = "[" "]". VariableInitializer : ArrayInitializer | Expression . ArrayInitializer = "{" [VariableInitializers] [Comma] "}" . VariableInitializers ~ VariableInitializer {*lookahead*(@2@) "," VariableInitializer} . Comma = "," . MethodDeclaration = MethodModifiers ResultType MethodDeclarator [ "throws" NameList ] AnyBlock . MethodModifiers ~ {MethodModifier} . MethodModifier : MM_Public | MM_Protected | MM_Private | MM_Static | MM_Abstract | MM_Final | MM_Native | MM_Synchronized . MM_Public = CPublic . MM_Protected = CProtected . MM_Private = CPrivate . MM_Static = CStatic . MM_Abstract = CAbstract . MM_Final = CFinal . MM_Native = CNative . MM_Synchronized = CSynchronized . AnyBlock : A_Block | A_SemiColon . A_Block = Block . A_SemiColon = SemiColon . MethodDeclarator = Identifier FormalParameters SquareBrackets . FormalParameters = "(" [FormalParameterList] ")" . FormalParameterList ~ FormalParameter {"," FormalParameter} . FormalParameter = [ CFinal ] Type VariableDeclaratorId . ConstructorDeclaration = [ConstructorModifier] Identifier FormalParameters ["throws" NameList] "{" [*lookahead*(@_ExplicitConstructorInvocation()@) ExplicitConstructorInvocation] BlockStatements "}" . ConstructorModifier : CSM_Public | CSM_Protected | CSM_Private . CSM_Public = CPublic . CSM_Protected = CProtected . CSM_Private = CPrivate . BlockStatements ~ {BlockStatement} . ExplicitConstructorInvocation : *lookahead*(@"this" _Arguments() ";"@) ThisConsInvoc | SuperConsInvoc . ThisConsInvoc = "this" Arguments ";" . SuperConsInvoc = [*lookahead*(@2@) PrimaryExpression "."] "super" Arguments ";" . Initializer = [ CStatic] Block . CPublic = "public" . CProtected = "protected" . CPrivate = "private" . CStatic = "static" . CAbstract = "abstract" . CFinal = "final" . CNative = "native" . CSynchronized = "synchronized" . CTransient = "transient" . CVolatile = "volatile" . /* * Type, name and expression syntax follows. */ Type = TypeAlt SquareBrackets . TypeAlt : PrimitiveType | TName . TName = Name . PrimitiveType : boolType | byteType | charType | shortType | intType | floatType | longType | doubleType . boolType = "boolean" . byteType = "byte" . charType = "char" . shortType = "short" . intType = "int" . floatType = "float" . longType = "long" . doubleType = "double" . ResultType : Void | Type . Void = "void". Name ~ Identifier { *lookahead*(@2@) "." Identifier } . NameList ~ Name {"," Name} . Identifier = Ident . SemiColon = ";" . /* * Expression syntax follows. */ Expression : *lookahead*(@ _PrimaryExpression() _AssignmentOperator()@) E_Assignment | ConditionalExpression . E_Assignment = Assignment . Assignment = PrimaryExpression AssignmentOperator Expression . AssignmentOperator : Eq | starEq | divEq | modEq | plEq | minEq | lshiftEq | rshiftEq | rrshiftEq | andEq | xorEq | orEq . Eq = "=" . starEq = "*=" . divEq = "/=" . modEq = "%=" . plEq = "+=" . minEq = "-=" . lshiftEq = "<<=" . rshiftEq = ">>=" . rrshiftEq = ">>>=" . andEq = "&=" . xorEq = "^=" . orEq = "|=" . ConditionalExpression = ConditionalOrExpression [ TernExp ] . TernExp = "?" Expression ":" ConditionalExpression . ConditionalOrExpression ~ ConditionalAndExpression {"||" ConditionalAndExpression } . ConditionalAndExpression~ InclusiveOrExpression {"&&" InclusiveOrExpression } . InclusiveOrExpression ~ ExclusiveOrExpression {"|" ExclusiveOrExpression} . ExclusiveOrExpression ~ AndExpression {"^" AndExpression} . AndExpression ~ EqualityExpression {"&" EqualityExpression} . EqualityExpression = InstanceOfExpression RHSEqualityExpression . RHSEqualityExpression ~ {RInstanceOfExpression} . RInstanceOfExpression = EqNotEq InstanceOfExpression . EqNotEq : EqEq | NotEq . EqEq = "==" . NotEq = "!=" . InstanceOfExpression = RelationalExpression [ "instanceof" Type ] . RelationalExpression = ShiftExpression RHSRelationalExpression . CompExp : Le | Ge | LeEq | GeEq . Le = "<" . Ge = ">" . LeEq = "<=" . GeEq = ">=" . RHSRelationalExpression ~ {RShiftExpression} . RShiftExpression = CompExp ShiftExpression . ShiftExpression = AdditiveExpression RHSShiftExpression . RHSShiftExpression ~ {RAdditiveExpression} . RAdditiveExpression = ShiftExp AdditiveExpression . ShiftExp : SEright | SEleft | SEunright . SEright = "<<" . SEleft = ">>" . SEunright = ">>>" . AdditiveExpression = MultiplicativeExpression RHSAdditiveExpression . RHSAdditiveExpression ~ {RMultiplicativeExpression} . RMultiplicativeExpression = AddExp MultiplicativeExpression . AddExp : Plus | Minus . Plus = "+" . Minus = "-" . MultiplicativeExpression = UnaryExpression RHSMultiplicativeExpression . RHSMultiplicativeExpression ~ {RUnaryExpression} . RUnaryExpression = MulExp UnaryExpression . MulExp : Mul | Div | Mod . Mul = "*" . Div = "/" . Mod = "%" . UnaryExpression : PlusMinusUnaryExpression | U_PreIncrementExpression | U_PreDecrementExpression | UnaryExpressionNotPlusMinus . U_PreIncrementExpression = PreIncrementExpression . U_PreDecrementExpression = PreDecrementExpression . PlusMinusUnaryExpression = AddExp UnaryExpression . PreIncrementExpression = "++" PrimaryExpression . PreDecrementExpression = "--" PrimaryExpression . UnaryExpressionNotPlusMinus : NUnaryExpression | *lookahead*(@ "(" (_PrimitiveType() | _Name()) ("[""]")* ")"@) CastExpression | UN_PostfixExpression . UN_PostfixExpression = PostfixExpression . NUnaryExpression = NotPlusMinus UnaryExpression . NotPlusMinus : Not | Tilde . Not = "!" . Tilde = "~" . PostfixExpression = PrimaryExpression [ PAddExp ] . PAddExp : PPlus | PMinus . PPlus = "++" . PMinus = "--" . CastExpression : *lookahead*(@"(" _PrimitiveType()@) CastExpSimple | *lookahead*(@"(" _Name()@) CastExpCmplx . CastExpSimple = "(" Type ")" UnaryExpression . CastExpCmplx = "(" Type ")" UnaryExpressionNotPlusMinus . PrimaryExpression = PrimaryPrefix PrimarySuffixes . PrimarySuffixes ~ { *lookahead*(@2@) PrimarySuffix } . PrimaryPrefix : Literal | PName | This | SuperPP | ParenExp | AllocationExpression . PName = Name. This = "this" . SuperPP = "super" "." Identifier . ParenExp = "(" Expression ")" . PrimarySuffix : *lookahead*(@2@) dotThis | *lookahead*(@2@) dotClass | *lookahead*(@2@) dotAlloc | dotParam | dotIdent | Arguments . dotThis = "." "this" . dotClass = "." "class" . dotAlloc = "." AllocationExpression . dotParam = "[" Expression "]" . dotIdent = "." Identifier . Literal : *lookahead*(@2@) Integer_literal | Floating_point_literal | Character_literal | *lookahead*(@1@) NullLiteral | BooleanLiteral | String_literal. Integer_literal = Integer . Floating_point_literal = Float . Character_literal = Character . String_literal = String . BooleanLiteral = Boolean . NullLiteral = "null" . Arguments = "(" [ArgumentList] ")" . ArgumentList ~ Expression { "," Expression} . AllocationExpression : *lookahead*(@2@) newCmplx | newSimple . newCmplx = "new" PrimitiveType ArrayDimensions [ ArrayInitializer ] . newSimple = "new" Name ArrAlloc. ArrAlloc : ArrAllocIni | ArrAllocClas . ArrAllocIni = ArrayDimensions [ ArrayInitializer ] . ArrAllocClas = Arguments [ ClassBody] . ArrayDimensions = ArrayDimension SquareBrackets . ArrayDimension ~ DimExp {*lookahead*(@2@) DimExp} . DimExp = "[" Expression "]" . /* * Statement syntax follows. */ Statement : *lookahead*(@2@) LabeledStatement | SBlock | EmptyStatement | StatementExpressionSe | SwitchStatement | IfStatement | WhileStatement | DoStatement | ForStatement | BreakStatement | ContinueStatement | ReturnStatement | ThrowStatement | SynchronizedStatement | TryStatement . StatementExpressionSe = StatementExpression ";" . LabeledStatement = Identifier ":" Statement . SBlock = Block . Block = "{" BlockStatements "}" . BlockStatement : *lookahead*(@[ "final" ] _Type() _Identifier()@) LocVarDecl | BStatement | UnmodifiedClassDeclaration . BStatement = Statement . LocVarDecl = LocalVariableDeclaration ";" . LocalVariableDeclaration = [CFinal] Type VariableDeclarators . EmptyStatement = SemiColon . StatementExpression : SE_PreIncrementExpression | SE_PreDecrementExpression | *lookahead*(@_PrimaryExpression() _AssignmentOperator()@) SE_Assignment | SE_PostfixExpression . SE_PreIncrementExpression = PreIncrementExpression . SE_PreDecrementExpression = PreDecrementExpression . SE_Assignment = Assignment . SE_PostfixExpression = PostfixExpression . SwitchStatement = "switch" "(" Expression ")" "{" CaseBlocks "}" . CaseBlocks ~ {CaseBlock} . CaseBlock = SwitchLabel BlockStatements . SwitchLabel : CaseExp | DefExp . CaseExp = "case" Expression ":" . DefExp = "default" ":" . IfStatement = "if" "(" Expression ")" Statement [*lookahead*(@1@) "else" Statement] . WhileStatement = "while" "(" Expression ")" Statement . DoStatement = "do" Statement "while" "(" Expression ")" ";" . ForStatement = "for" "(" ForInitOpt ForEvalExp [ForUpdate]")"Statement . ForInitOpt = [ForInit] ";" . ForEvalExp = [Expression]";" . ForInit : *lookahead*(@ [ "final" ] _Type() _Identifier() @) LocalVariableDeclaration | StatementExpressionList . StatementExpressionList ~ StatementExpression { "," StatementExpression} . ForUpdate = StatementExpressionList . BreakStatement = "break" [ Identifier ] ";" . ContinueStatement = "continue" [ Identifier ] ";" . ReturnStatement = "return" [ Expression ] ";" . ThrowStatement = "throw" Expression ";" . SynchronizedStatement = "synchronized" "(" Expression")" Block . TryStatement = "try" Block CatchBlocks [ "finally" Block ] . CatchBlocks ~ {CatchBlock}. CatchBlock = "catch" "(" FormalParameter ")" Block . //visitors ClassVisitor = . MethodVisitor = . Main = . // MethodDeclarationCounter = .