Skip to content

Commit

Permalink
Java/ANTLR extracted grammar #1
Browse files Browse the repository at this point in the history
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@967 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed Mar 7, 2011
1 parent 482e839 commit 1abcefd
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
9 changes: 9 additions & 0 deletions topics/grammars/java/antlr-java-5/Makefile
@@ -0,0 +1,9 @@
all:
cd ../../../extraction/antlr && make build
make build

build:
java -cp ${CLASSPATH}:../../../../download/antlrworks-1.1.7.jar:../../../extraction/antlr slps.antlr2bgf.Tool parr/Java.stripped.g parr.bgf

clean:
rm *.bgf
145 changes: 145 additions & 0 deletions topics/grammars/java/antlr-java-5/parr/Java.stripped.g
@@ -0,0 +1,145 @@
grammar Java;
compilationUnit: annotations ( packageDeclaration importDeclaration* typeDeclaration*| classOrInterfaceDeclaration typeDeclaration*)| packageDeclaration? importDeclaration* typeDeclaration*;
packageDeclaration: 'package' qualifiedName ';';
importDeclaration: 'import' 'static'? qualifiedName ('.' '*')? ';';
typeDeclaration: classOrInterfaceDeclaration | ';';
classOrInterfaceDeclaration: classOrInterfaceModifiers (classDeclaration | interfaceDeclaration);
classOrInterfaceModifiers: classOrInterfaceModifier*;
classOrInterfaceModifier: annotation | 'public'| 'protected'| 'private'| 'abstract'| 'static'| 'final'| 'strictfp';
modifiers: modifier*;
classDeclaration: normalClassDeclaration | enumDeclaration ;
normalClassDeclaration: 'class' Identifier typeParameters? ('extends' type)? ('implements' typeList)? classBody ;
typeParameters: '<' typeParameter (',' typeParameter)* '>';
typeParameter: Identifier ('extends' typeBound)?;
typeBound: type ('&' type)*;
enumDeclaration: ENUM Identifier ('implements' typeList)? enumBody ;
enumBody: '{' enumConstants? ','? enumBodyDeclarations? '}';
enumConstants: enumConstant (',' enumConstant)*;
enumConstant: annotations? Identifier arguments? classBody?;
enumBodyDeclarations: ';' (classBodyDeclaration)*;
interfaceDeclaration: normalInterfaceDeclaration | annotationTypeDeclaration ;
normalInterfaceDeclaration: 'interface' Identifier typeParameters? ('extends' typeList)? interfaceBody ;
typeList: type (',' type)*;
classBody: '{' classBodyDeclaration* '}';
interfaceBody: '{' interfaceBodyDeclaration* '}';
classBodyDeclaration: ';'| 'static'? block | modifiers memberDecl ;
memberDecl: genericMethodOrConstructorDecl | memberDeclaration | 'void' Identifier voidMethodDeclaratorRest | Identifier constructorDeclaratorRest | interfaceDeclaration | classDeclaration ;
memberDeclaration: type (methodDeclaration | fieldDeclaration);
genericMethodOrConstructorDecl: typeParameters genericMethodOrConstructorRest ;
genericMethodOrConstructorRest: (type | 'void') Identifier methodDeclaratorRest | Identifier constructorDeclaratorRest ;
methodDeclaration: Identifier methodDeclaratorRest ;
fieldDeclaration: variableDeclarators ';';
interfaceBodyDeclaration: modifiers interfaceMemberDecl | ';';
interfaceMemberDecl: interfaceMethodOrFieldDecl | interfaceGenericMethodDecl | 'void' Identifier voidInterfaceMethodDeclaratorRest | interfaceDeclaration | classDeclaration ;
interfaceMethodOrFieldDecl: type Identifier interfaceMethodOrFieldRest ;
interfaceMethodOrFieldRest: constantDeclaratorsRest ';'| interfaceMethodDeclaratorRest ;
methodDeclaratorRest: formalParameters ('[' ']')* ('throws' qualifiedNameList)? ( methodBody | ';');
voidMethodDeclaratorRest: formalParameters ('throws' qualifiedNameList)? ( methodBody | ';');
interfaceMethodDeclaratorRest: formalParameters ('[' ']')* ('throws' qualifiedNameList)? ';';
interfaceGenericMethodDecl: typeParameters (type | 'void') Identifier interfaceMethodDeclaratorRest ;
voidInterfaceMethodDeclaratorRest: formalParameters ('throws' qualifiedNameList)? ';';
constructorDeclaratorRest: formalParameters ('throws' qualifiedNameList)? constructorBody ;
constantDeclarator: Identifier constantDeclaratorRest ;
variableDeclarators: variableDeclarator (',' variableDeclarator)*;
variableDeclarator: variableDeclaratorId ('=' variableInitializer)?;
constantDeclaratorsRest: constantDeclaratorRest (',' constantDeclarator)*;
constantDeclaratorRest: ('[' ']')* '=' variableInitializer ;
variableDeclaratorId: Identifier ('[' ']')*;
variableInitializer: arrayInitializer | expression ;
arrayInitializer: '{' (variableInitializer (',' variableInitializer)* (',')?)? '}';
modifier: annotation | 'public'| 'protected'| 'private'| 'static'| 'abstract'| 'final'| 'native'| 'synchronized'| 'transient'| 'volatile'| 'strictfp';
packageOrTypeName: qualifiedName ;
enumConstantName: Identifier ;
typeName: qualifiedName ;
type: classOrInterfaceType ('[' ']')*| primitiveType ('[' ']')*;
classOrInterfaceType: Identifier typeArguments? ('.' Identifier typeArguments?)*;
primitiveType: 'boolean'| 'char'| 'byte'| 'short'| 'int'| 'long'| 'float'| 'double';
variableModifier: 'final'| annotation ;
typeArguments: '<' typeArgument (',' typeArgument)* '>';
typeArgument: type | '?' (('extends'| 'super') type)?;
qualifiedNameList: qualifiedName (',' qualifiedName)*;
formalParameters: '(' formalParameterDecls? ')';
formalParameterDecls: variableModifiers type formalParameterDeclsRest ;
formalParameterDeclsRest: variableDeclaratorId (',' formalParameterDecls)?| '...' variableDeclaratorId ;
methodBody: block ;
constructorBody: '{' explicitConstructorInvocation? blockStatement* '}';
explicitConstructorInvocation: nonWildcardTypeArguments? ('this'| 'super') arguments ';'| primary '.' nonWildcardTypeArguments? 'super' arguments ';';
qualifiedName: Identifier ('.' Identifier)*;
literal: integerLiteral | FloatingPointLiteral | CharacterLiteral | StringLiteral | booleanLiteral | 'null';
integerLiteral: HexLiteral | OctalLiteral | DecimalLiteral ;
booleanLiteral: 'true'| 'false';
annotations: annotation+;
annotation: '@' annotationName ( '(' ( elementValuePairs | elementValue )? ')')?;
annotationName: Identifier ('.' Identifier)*;
elementValuePairs: elementValuePair (',' elementValuePair)*;
elementValuePair: Identifier '=' elementValue ;
elementValue: conditionalExpression | annotation | elementValueArrayInitializer ;
elementValueArrayInitializer: '{' (elementValue (',' elementValue)*)? (',')? '}';
annotationTypeDeclaration: '@' 'interface' Identifier annotationTypeBody ;
annotationTypeBody: '{' (annotationTypeElementDeclaration)* '}';
annotationTypeElementDeclaration: modifiers annotationTypeElementRest ;
annotationTypeElementRest: type annotationMethodOrConstantRest ';'| normalClassDeclaration ';'?| normalInterfaceDeclaration ';'?| enumDeclaration ';'?| annotationTypeDeclaration ';'?;
annotationMethodOrConstantRest: annotationMethodRest | annotationConstantRest ;
annotationMethodRest: Identifier '(' ')' defaultValue?;
annotationConstantRest: variableDeclarators ;
defaultValue: 'default' elementValue ;
block: '{' blockStatement* '}';
blockStatement: localVariableDeclarationStatement | classOrInterfaceDeclaration | statement ;
localVariableDeclarationStatement: localVariableDeclaration ';';
localVariableDeclaration: variableModifiers type variableDeclarators ;
variableModifiers: variableModifier*;
statement: block | ASSERT expression (':' expression)? ';'| 'if' parExpression statement ('else' statement)?| 'for' '(' forControl ')' statement | 'while' parExpression statement | 'do' statement 'while' parExpression ';'| 'try' block ( catches 'finally' block | catches | 'finally' block )| 'switch' parExpression '{' switchBlockStatementGroups '}'| 'synchronized' parExpression block | 'return' expression? ';'| 'throw' expression ';'| 'break' Identifier? ';'| 'continue' Identifier? ';'| ';'| statementExpression ';'| Identifier ':' statement ;
catches: catchClause (catchClause)*;
catchClause: 'catch' '(' formalParameter ')' block ;
formalParameter: variableModifiers type variableDeclaratorId ;
switchBlockStatementGroups: (switchBlockStatementGroup)*;
switchBlockStatementGroup: switchLabel+ blockStatement*;
switchLabel: 'case' constantExpression ':'| 'case' enumConstantName ':'| 'default' ':';
forControl: enhancedForControl | forInit? ';' expression? ';' forUpdate?;
forInit: localVariableDeclaration | expressionList ;
enhancedForControl: variableModifiers type Identifier ':' expression ;
forUpdate: expressionList ;
parExpression: '(' expression ')';
expressionList: expression (',' expression)*;
statementExpression: expression ;
constantExpression: expression ;
expression: conditionalExpression (assignmentOperator expression)?;
assignmentOperator: '='| '+='| '-='| '*='| '/='| '&='| '|='| '^='| '%='| t1='<' t2='<' t3='=' | t1='>' t2='>' t3='>' t4='=' | t1='>' t2='>' t3='=';
conditionalExpression: conditionalOrExpression ( '?' expression ':' expression )?;
conditionalOrExpression: conditionalAndExpression ( '||' conditionalAndExpression )*;
conditionalAndExpression: inclusiveOrExpression ( '&&' inclusiveOrExpression )*;
inclusiveOrExpression: exclusiveOrExpression ( '|' exclusiveOrExpression )*;
exclusiveOrExpression: andExpression ( '^' andExpression )*;
andExpression: equalityExpression ( '&' equalityExpression )*;
equalityExpression: instanceOfExpression ( ('=='| '!=') instanceOfExpression )*;
instanceOfExpression: relationalExpression ('instanceof' type)?;
relationalExpression: shiftExpression ( relationalOp shiftExpression )*;
relationalOp: t1='<' t2='=' | t1='>' t2='=' | '<'| '>';
shiftExpression: additiveExpression ( shiftOp additiveExpression )*;
shiftOp: t1='<' t2='<' | t1='>' t2='>' t3='>' | t1='>' t2='>';
additiveExpression: multiplicativeExpression ( ('+'| '-') multiplicativeExpression )*;
multiplicativeExpression: unaryExpression ( ( '*'| '/'| '%') unaryExpression )*;
unaryExpression: '+' unaryExpression | '-' unaryExpression | '++' unaryExpression | '--' unaryExpression | unaryExpressionNotPlusMinus ;
unaryExpressionNotPlusMinus: '~' unaryExpression | '!' unaryExpression | castExpression | primary selector* ('++'|'--')?;
castExpression: '(' primitiveType ')' unaryExpression | '(' (type | expression) ')' unaryExpressionNotPlusMinus ;
primary: parExpression | 'this' ('.' Identifier)* identifierSuffix?| 'super' superSuffix | literal | 'new' creator | Identifier ('.' Identifier)* identifierSuffix?| primitiveType ('[' ']')* '.' 'class'| 'void' '.' 'class';
identifierSuffix: ('[' ']')+ '.' 'class'| ('[' expression ']')+| arguments | '.' 'class'| '.' explicitGenericInvocation | '.' 'this'| '.' 'super' arguments | '.' 'new' innerCreator ;
creator: nonWildcardTypeArguments createdName classCreatorRest | createdName (arrayCreatorRest | classCreatorRest);
createdName: classOrInterfaceType | primitiveType ;
innerCreator: nonWildcardTypeArguments? Identifier classCreatorRest ;
arrayCreatorRest: '[' ( ']' ('[' ']')* arrayInitializer | expression ']' ('[' expression ']')* ('[' ']')*);
classCreatorRest: arguments classBody?;
explicitGenericInvocation: nonWildcardTypeArguments Identifier arguments ;
nonWildcardTypeArguments: '<' typeList '>';
selector: '.' Identifier arguments?| '.' 'this'| '.' 'super' superSuffix | '.' 'new' innerCreator | '[' expression ']';
superSuffix: arguments | '.' Identifier arguments?;
arguments: '(' expressionList? ')';
HexLiteral: '0' ('x'|'X') HexDigit+ IntegerTypeSuffix?;
IntegerTypeSuffix: ('l'|'L');
FloatTypeSuffix: ('f'|'F'|'d'|'D');
EscapeSequence: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')| UnicodeEscape | OctalEscape ;
UnicodeEscape: '\\' 'u' HexDigit HexDigit HexDigit HexDigit ;
ENUM: 'enum';
ASSERT: 'assert';
Identifier: Letter (Letter|JavaIDDigit)*;
WS: (' '|'\r'|'\t'|'\u000C'|'\n');
13 changes: 13 additions & 0 deletions topics/grammars/java/antlr-java-5/parr/README.txt
Expand Up @@ -4,3 +4,16 @@ http://www.antlr.org/grammar/list:
Java 1.5 grammar for ANTLR v3
Terence Parr Wed Jul 5, 2006 16:20
A Java 1.5 grammar written from the spec. Very clean and pretty fast. I have not tried to optimize for speed.



Java.stripped.g is derived from Java.g by:
* removing all comments
* removing all header sections (options, @lexer, ...)
* removing fragments
* removing predicates
* removing rewrite rules
* removing semantic actions
* removing productions with .. or . or ~
* removing local symbol options
------------------------------------------------------ most of this was done automatically in Rascal

0 comments on commit 1abcefd

Please sign in to comment.