Remove tool/playground from source control, add to .gitignore (fixes #293)

This commit is contained in:
Sam Harwell 2013-06-25 14:30:25 -05:00
parent 3fb9a60386
commit 931df811db
36 changed files with 3 additions and 8784 deletions

3
.gitignore vendored
View File

@ -25,3 +25,6 @@ nbactions*.xml
# Profiler results
*.hprof
# Playground
/tool/playground/

View File

@ -1 +0,0 @@
{{x} }

View File

@ -1,27 +0,0 @@
lexer grammar A;
/*
For input
{{x}
}
This matches {{x} and then thinks that it can stop because it can match that
without going into the recursive call. The context for the stop state in ACTION
is (2,1,[[$, 6 $]]) so it deletes everything else associated with this token.
Seems like we should favor the first alternative, but we can't do that within
a single rule.
weird though that this one works
STRING : '"' ( '\\' '"' | . )*? '"' ;
wouldn't it get to the end of the rule also by the wild-card route?
Maybe it's a simple order of operations or order in which i process the
alternatives?
*/
//STRING : '"' ( 'x' | . )* '"' ;
ACTION : '{' ( ACTION | . )*? '}' ;
WS : [ \r\t\n]+ -> skip ;

View File

@ -1,13 +0,0 @@
grammar A2;
s : e {System.out.println($e.v);} ;
e returns [int v]
: a=e '*' b=e {$v = $a.v * $b.v;} # Mult
| a=e '+' b=e {$v = $a.v + $b.v;} # Add
| INT {$v = $INT.int;} # Int
| '(' x=e ')' {$v = $x.v;} # Parens
;
INT : [0-9]+ ;
WS : [ \t\n]+ -> skip ;

View File

@ -1,11 +0,0 @@
class Bar {
private int bitsOrSingle(int bits, int ch) {
boolean d =
!(3==4 &&
(ch == 0xff || ch == 0xb5 ||
// ch == 0x53 || ch == 0x73 ||
// ch == 0x4b || ch == 0x6b ||
ch == 0xc5 || ch == 0xe5));
return 9;
}
}

View File

@ -1,26 +0,0 @@
grammar CSV;
@members {
double x, y;
}
file: row+ {System.out.printf("%f, %f\n", x, y);} ;
/** Rows are two real numbers:
0.9962269825793676,0.9224608616182103
0.91673278673353,-0.6374985722530822
0.9841464019977713,0.03539546030010776
...
*/
row : a=field ',' b=field '\r'? '\n'
{
x += Double.valueOf($a.start.getText());
y += Double.valueOf($b.start.getText());
}
;
field
: TEXT
;
TEXT : ~[,\n\r]+ ;

View File

@ -1,82 +0,0 @@
/** Simple statically-typed programming language with methods and variables
* taken from "Language Implementation Patterns" book.
*/
grammar E;
// START: file
file: (methodDecl | varDecl)+ ;
// END: file
// START: var
varDecl
: type ID ('=' expr)? ';'
;
type: 'float' | 'int' | 'void' ; // user-defined types
// END: var
// START: method
methodDecl
: type ID '(' formalParameters? ')' block // "void f(int x) {...}"
;
formalParameters
: type ID (',' type ID)*
;
// END: method
// START: stat
block: '{' stat* '}' ; // possibly empty statement block
stat: block
| varDecl
| 'if' expr 'then' stat ('else' stat)?
| 'return' expr? ';'
| expr '=' expr ';' // assignment
| expr ';' // func call
;
// END: stat
/* expr below becomes the following non-left recursive rule:
expr[int _p]
: ( '-' expr[6]
| '!' expr[5]
| ID
| INT
| '(' expr ')'
)
( {8 >= $_p}? '*' expr[9]
| {7 >= $_p}? ('+'|'-') expr[8]
| {4 >= $_p}? '==' expr[5]
| {10 >= $_p}? '[' expr ']'
| {9 >= $_p}? '(' exprList? ')'
)*
;
*/
// START: expr
expr: expr '[' expr ']' // array index like a[i], a[i][j]
| expr '(' exprList? ')' // func call like f(), f(x), f(1,2)
| expr '*' expr
| expr ('+'|'-') expr
| '-' expr // unary minus
| '!' expr // boolean not
| expr '==' expr // equality comparison (lowest priority op)
| ID // variable reference
| INT
| '(' expr ')'
;
exprList : expr (',' expr)* ; // arg list
// END: expr
ID : LETTER (LETTER | [0-9])* ;
fragment
LETTER : [a-zA-Z] ;
INT : [0-9]+ ;
WS : [ \t\n\r]+ -> skip ;
SL_COMMENT
: '//' .* '\n' -> skip
;

View File

@ -1,54 +0,0 @@
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.util.List;
public class ExtractInheritance extends JavaLRBaseListener {
JavaLRParser parser;
public ExtractInheritance(JavaLRParser parser) { this.parser = parser; }
/*
normalClassDeclaration
: 'class' Identifier typeParameters?
('extends' type)?
('implements' typeList)?
classBody
;
*/
@Override
public void enterNormalClassDeclaration(JavaLRParser.NormalClassDeclarationContext ctx) {
TerminalNode id = ctx.Identifier();
String sup = null;
if ( ctx.type()!=null ) {
sup = ctx.type().getText();
System.out.println("\""+id+"\" -> \""+sup+"\"");
}
if ( ctx.typeList()!=null ) {
List<? extends JavaLRParser.TypeContext> type = ctx.typeList().type();
for (JavaLRParser.TypeContext t : type) {
System.out.println("\""+id+"\" -> \""+t.getText()+"\"");
}
}
}
/*
normalInterfaceDeclaration
: 'interface' Identifier typeParameters? ('extends' typeList)? interfaceBody
;
*/
@Override
public void enterNormalInterfaceDeclaration(JavaLRParser.NormalInterfaceDeclarationContext ctx) {
TerminalNode id = ctx.Identifier();
System.out.println("###### interface "+id);
String args = null;
if ( ctx.typeList()!=null ) {
List<? extends JavaLRParser.TypeContext> type = ctx.typeList().type();
for (JavaLRParser.TypeContext t : type) {
System.out.println("\""+id+"\" -> \""+t.getText()+"\"");
}
}
}
}

View File

@ -1,14 +0,0 @@
class Foo {
private int bitsOrSingle(int bits, int ch) {
int d;
if (ch < 256 &&
!(3==4 && 5==6 &&
(ch == 0xff || ch == 0xb5 ||
ch == 0x49 || ch == 0x69 || //I and i // cmt out and it continues!
ch == 0x53 || ch == 0x73 || //S and s
ch == 0x4b || ch == 0x6b || //K and k
ch == 0xc5 || ch == 0xe5))) //A+ring
return 0;
return 9;
}
}

View File

@ -1,61 +0,0 @@
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class GenHierarchy {
public static void main(String[] args) throws Exception {
// START: input
String inputFile = null;
if ( args.length>0 ) inputFile = args[0];
List<String> files = getFilenames(new File(inputFile));
for (String file : files) {
InputStream is = new FileInputStream(file);
ANTLRInputStream input = new ANTLRInputStream(is);
// END: input
// System.out.println(file);
// START: launch
JavaLRLexer lexer = new JavaLRLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
JavaLRParser parser = new JavaLRParser(tokens);
ParserRuleContext tree = parser.compilationUnit(); // parse
ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker
ExtractInheritance extractor = new ExtractInheritance(parser);
walker.walk(extractor, tree); // initiate walk of tree with listener
}
// END: launch
}
public static List<String> getFilenames(File f) throws Exception {
List<String> files = new ArrayList<String>();
getFilenames_(f, files);
return files;
}
public static void getFilenames_(File f, List<String> files) throws Exception {
// If this is a directory, walk each file/dir in that directory
if (f.isDirectory()) {
String flist[] = f.list();
for (String aFlist : flist) {
getFilenames_(new File(f, aFlist), files);
}
}
// otherwise, if this is a java file, parse it!
else if ( ((f.getName().length()>5) &&
f.getName().substring(f.getName().length()-5).equals(".java")) )
{
files.add(f.getAbsolutePath());
}
}
}

View File

@ -1,14 +0,0 @@
parser grammar HTMLParser;
options { tokenVocab=HTMLParser; }
file : ( TAG_START (starttag | endtag) | TEXT
{System.out.println("TEXT "+$TEXT);} )+ EOF ;
starttag : ID attr* TAG_STOP ;
attr : ID (EQ (ID|STRING))? ;
endtag
: END_TAG {System.out.println("END tag "+$END_TAG);}
;

View File

@ -1,860 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2012 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Java 1.6 grammar derived from:
https://github.com/antlr/examples-v3/blob/master/java/java/Java.g
*/
grammar JavaLR;
@lexer::members {
protected boolean enumIsKeyword = true;
protected boolean assertIsKeyword = true;
}
@parser::members {
/*
public void enterRule(ParserRuleContext<Token> localctx, int ruleIndex) {
super.enterRule(localctx, ruleIndex);
System.out.println("enter "+ruleNames[ruleIndex]+
", LT(1)="+_input.LT(1)+
", LT(2)="+_input.LT(2));
}
@Override
public void exitRule(int ruleIndex) {
super.exitRule(ruleIndex);
System.err.println("exit "+ruleNames[ruleIndex]+
", LT(1)="+_input.LT(1)+
", LT(2)="+_input.LT(2));
}
*/
}
// starting point for parsing a java file
/* The annotations are separated out to make parsing faster, but must be associated with
a packageDeclaration or a typeDeclaration (and not an empty one). */
compilationUnit
: annotations
( packageDeclaration importDeclaration* typeDeclaration*
| classOrInterfaceDeclaration typeDeclaration*
)
EOF
| packageDeclaration? importDeclaration* typeDeclaration*
EOF
;
packageDeclaration
: 'package' qualifiedName ';'
;
importDeclaration
: 'import' 'static'? qualifiedName ('.' '*')? ';'
;
typeDeclaration
: classOrInterfaceDeclaration
| ';'
;
classOrInterfaceDeclaration
: classOrInterfaceModifiers (classDeclaration | interfaceDeclaration)
;
classOrInterfaceModifiers
: classOrInterfaceModifier*
;
classOrInterfaceModifier
: annotation // class or interface
| 'public' // class or interface
| 'protected' // class or interface
| 'private' // class or interface
| 'abstract' // class or interface
| 'static' // class or interface
| 'final' // class only -- does not apply to interfaces
| 'strictfp' // class or interface
;
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
annotations
: annotation+
;
annotation
: '@' annotationName ( '(' ( elementValuePairs | elementValue )? ')' )?
;
annotationName
: Identifier ('.' Identifier)*
;
elementValuePairs
: elementValuePair (',' elementValuePair)*
;
elementValuePair
: Identifier '=' elementValue
;
elementValue
: expression
| 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
;
// STATEMENTS / BLOCKS
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)?
| '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* switchLabel*
;
/* The change here (switchLabel -> switchLabel+) technically makes this grammar
ambiguous; but with appropriately greedy parsing it yields the most
appropriate AST, one in which each group, except possibly the last one, has
labels and statements. */
switchBlockStatementGroup
: switchLabel+ blockStatement+
;
switchLabel
: 'case' constantExpression ':'
| 'case' enumConstantName ':'
| 'default' ':'
;
forControl
: enhancedForControl
| forInit? ';' expression? ';' forUpdate?
;
forInit
: localVariableDeclaration
| expressionList
;
enhancedForControl
: variableModifiers type Identifier ':' expression
;
forUpdate
: expressionList
;
// EXPRESSIONS
parExpression
: '(' expression ')'
;
expressionList
: expression (',' expression)*
;
statementExpression
: expression
;
constantExpression
: expression
;
//expression : expression_[0] ;
expression
: primary
| expression '.' Identifier
| expression '.' 'this'
| expression '.' 'super' '(' expressionList? ')'
| expression '.' 'new' Identifier '(' expressionList? ')'
| expression '.' 'super' '.' Identifier arguments?
| expression '.' explicitGenericInvocation
| 'new' creator
| expression '[' expression ']'
| '(' type ')' expression
| expression ('++' | '--')
| expression '(' expressionList? ')'
| ('+'|'-'|'++'|'--') expression
| ('~'|'!') expression
| expression ('*'|'/'|'%') expression
| expression ('+'|'-') expression
| expression ('<' '<' | '>' '>' '>' | '>' '>') expression
| expression ('<' '=' | '>' '=' | '>' | '<') expression
| expression 'instanceof' type
| expression ('==' | '!=') expression
| expression '&' expression
| expression '^'<assoc=right> expression
| expression '|' expression
| expression '&&' expression
| expression '||' expression
| expression '?' expression ':' expression
| expression
('^='<assoc=right>
|'+='<assoc=right>
|'-='<assoc=right>
|'*='<assoc=right>
|'/='<assoc=right>
|'&='<assoc=right>
|'|='<assoc=right>
|'='<assoc=right>
|'>' '>' '='<assoc=right>
|'>' '>' '>' '='<assoc=right>
|'<' '<' '='<assoc=right>
|'%='<assoc=right>
)
expression
;
primary
: '(' expression ')'
| 'this'
| 'super'
| literal
| Identifier
| type '.' 'class'
| 'void' '.' 'class'
;
/*
expression_[int _p]
: expression_primary
(
{13 >= $_p}? ('*'|'/'|'%') expression_[14]
| {12 >= $_p}? ('+'|'-') expression_[13]
| {11 >= $_p}? ('<' '<' | '>' '>' '>' | '>' '>') expression_[12]
| {10 >= $_p}? ('<=' | '>=' | '>' | '<') expression_[11]
| {8 >= $_p}? ('==' | '!=') expression_[9]
| {7 >= $_p}? '&' expression_[8]
| {6 >= $_p}? '^'<assoc=right> expression_[6]
| {5 >= $_p}? '|' expression_[6]
| {4 >= $_p}? '&&' expression_[5]
| {3 >= $_p}? '||' expression_[4]
| {1 >= $_p}? ('^='<assoc=right>
|'+='<assoc=right>
|'-='<assoc=right>
|'*='<assoc=right>
|'/='<assoc=right>
|'&='<assoc=right>
|'|='<assoc=right>
|'='<assoc=right>
|'>' '>' '='<assoc=right>
|'>' '>' '>' '='<assoc=right>
|'<' '<' '='<assoc=right>
|'%='<assoc=right>
)
expression_[1]
| {2 >= $_p}? '?' expression ':' expression_[3]
| {26 >= $_p}? '.' Identifier
| {25 >= $_p}? '.' 'this'
| {24 >= $_p}? '.' 'super' '(' expressionList? ')'
| {23 >= $_p}? '.' 'new' Identifier '(' expressionList? ')'
| {22 >= $_p}? '.' 'super' '.' Identifier arguments?
| {21 >= $_p}? '.' explicitGenericInvocation
| {19 >= $_p}? '[' expression ']'
| {17 >= $_p}? ('++' | '--')
| {16 >= $_p}? '(' expressionList? ')'
| {9 >= $_p}? 'instanceof' type
)*
;
2011-11-30 18:39:48:343 left-recursion LogManager.java:48 expression_primary
: '(' type ')' expression_[18]
| ('+'|'-'|'++'|'--') expression_[15]
| ('~'|'!') expression_[14]
| '(' expression ')'
| 'this'
| 'super'
| literal
| Identifier
| type '.' 'class'
| 'new' creator
;
*/
creator
: nonWildcardTypeArguments createdName classCreatorRest
| createdName (arrayCreatorRest | classCreatorRest)
;
createdName
: classOrInterfaceType
| primitiveType
;
innerCreator
: nonWildcardTypeArguments? Identifier classCreatorRest
;
explicitGenericInvocation
: nonWildcardTypeArguments Identifier arguments
;
arrayCreatorRest
: '['
( ']' ('[' ']')* arrayInitializer
| expression ']' ('[' expression ']')* ('[' ']')*
)
;
classCreatorRest
: arguments classBody?
;
nonWildcardTypeArguments
: '<' typeList '>'
;
arguments
: '(' expressionList? ')'
;
// LEXER
HexLiteral : '0' ('x'|'X') HexDigit+ IntegerTypeSuffix? ;
DecimalLiteral : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix? ;
OctalLiteral : '0' ('0'..'7')+ IntegerTypeSuffix? ;
fragment
HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;
fragment
IntegerTypeSuffix : ('l'|'L') ;
FloatingPointLiteral
: ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix?
| '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
| ('0'..'9')+ Exponent FloatTypeSuffix?
| ('0'..'9')+ FloatTypeSuffix
| ('0x' | '0X') (HexDigit )*
('.' (HexDigit)*)?
( 'p' | 'P' )
( '+' | '-' )?
( '0' .. '9' )+
FloatTypeSuffix?
;
fragment
Exponent : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
fragment
FloatTypeSuffix : ('f'|'F'|'d'|'D') ;
CharacterLiteral
: '\'' ( EscapeSequence | ~('\''|'\\') ) '\''
;
StringLiteral
: '"' ( EscapeSequence | ~('\\'|'"') )* '"'
;
fragment
EscapeSequence
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
| UnicodeEscape
| OctalEscape
;
fragment
OctalEscape
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;
fragment
UnicodeEscape
: '\\' 'u' HexDigit HexDigit HexDigit HexDigit
;
ENUM: 'enum' {if (!enumIsKeyword) setType(Identifier);}
;
ASSERT
: 'assert' {if (!assertIsKeyword) setType(Identifier);}
;
Identifier
: Letter (Letter|JavaIDDigit)*
;
/**I found this char range in JavaCC's grammar, but Letter and Digit overlap.
Still works, but...
*/
fragment
Letter
: '\u0024' |
'\u0041'..'\u005a' |
'\u005f' |
'\u0061'..'\u007a' |
'\u00c0'..'\u00d6' |
'\u00d8'..'\u00f6' |
'\u00f8'..'\u00ff' |
'\u0100'..'\u1fff' |
'\u3040'..'\u318f' |
'\u3300'..'\u337f' |
'\u3400'..'\u3d2d' |
'\u4e00'..'\u9fff' |
'\uf900'..'\ufaff'
;
fragment
JavaIDDigit
: '\u0030'..'\u0039' |
'\u0660'..'\u0669' |
'\u06f0'..'\u06f9' |
'\u0966'..'\u096f' |
'\u09e6'..'\u09ef' |
'\u0a66'..'\u0a6f' |
'\u0ae6'..'\u0aef' |
'\u0b66'..'\u0b6f' |
'\u0be7'..'\u0bef' |
'\u0c66'..'\u0c6f' |
'\u0ce6'..'\u0cef' |
'\u0d66'..'\u0d6f' |
'\u0e50'..'\u0e59' |
'\u0ed0'..'\u0ed9' |
'\u1040'..'\u1049'
;
WS : (' '|'\r'|'\t'|'\u000C'|'\n')+ {setChannel(HIDDEN);}
;
COMMENT
: '/*' .*? '*/' {setChannel(HIDDEN);}
;
LINE_COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {setChannel(HIDDEN);}
;

View File

@ -1,204 +0,0 @@
lexer grammar JavaLexer;
@members {
protected boolean enumIsKeyword = true;
protected boolean assertIsKeyword = true;
}
T__25 : 'package' ;
T__26 : ';' ;
T__27 : 'import' ;
T__28 : 'static' ;
T__29 : '.' ;
T__30 : '*' ;
T__31 : 'public' ;
T__32 : 'protected' ;
T__33 : 'private' ;
T__34 : 'abstract' ;
T__35 : 'final' ;
T__36 : 'strictfp' ;
T__37 : 'class' ;
T__38 : 'extends' ;
T__39 : 'implements' ;
T__40 : '<' ;
T__41 : ',' ;
T__42 : '>' ;
T__43 : '&' ;
T__44 : '{' ;
T__45 : '}' ;
T__46 : 'interface' ;
T__47 : 'void' ;
T__48 : '[' ;
T__49 : ']' ;
T__50 : 'throws' ;
T__51 : '=' ;
T__52 : 'native' ;
T__53 : 'synchronized' ;
T__54 : 'transient' ;
T__55 : 'volatile' ;
T__56 : 'boolean' ;
T__57 : 'char' ;
T__58 : 'byte' ;
T__59 : 'short' ;
T__60 : 'int' ;
T__61 : 'long' ;
T__62 : 'float' ;
T__63 : 'double' ;
T__64 : '?' ;
T__65 : 'super' ;
T__66 : '(' ;
T__67 : ')' ;
T__68 : '...' ;
T__69 : 'this' ;
T__70 : 'null' ;
T__71 : 'true' ;
T__72 : 'false' ;
T__73 : '@' ;
T__74 : 'default' ;
T__75 : ':' ;
T__76 : 'if' ;
T__77 : 'else' ;
T__78 : 'for' ;
T__79 : 'while' ;
T__80 : 'do' ;
T__81 : 'try' ;
T__82 : 'finally' ;
T__83 : 'switch' ;
T__84 : 'return' ;
T__85 : 'throw' ;
T__86 : 'break' ;
T__87 : 'continue' ;
T__88 : 'catch' ;
T__89 : 'case' ;
T__90 : '+=' ;
T__91 : '-=' ;
T__92 : '*=' ;
T__93 : '/=' ;
T__94 : '&=' ;
T__95 : '|=' ;
T__96 : '^=' ;
T__97 : '%=' ;
T__98 : '||' ;
T__99 : '&&' ;
T__100 : '|' ;
T__101 : '^' ;
T__102 : '==' ;
T__103 : '!=' ;
T__104 : 'instanceof' ;
T__105 : '+' ;
T__106 : '-' ;
T__107 : '/' ;
T__108 : '%' ;
T__109 : '++' ;
T__110 : '--' ;
T__111 : '~' ;
T__112 : '!' ;
T__113 : 'new' ;
// $ANTLR src "JavaCombined.g" 911
HexLiteral : '0' ('x'|'X') HexDigit+ IntegerTypeSuffix? ;// $ANTLR src "JavaCombined.g" 913
DecimalLiteral : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix? ;// $ANTLR src "JavaCombined.g" 915
OctalLiteral : '0' ('0'..'7')+ IntegerTypeSuffix? ;// $ANTLR src "JavaCombined.g" 917
fragment
HexDigit : ('0'..'9'|'a'..'f'|'A'..'F') ;// $ANTLR src "JavaCombined.g" 920
fragment
IntegerTypeSuffix : ('l'|'L') ;// $ANTLR src "JavaCombined.g" 923
FloatingPointLiteral
: ('0'..'9')+ '.' ('0'..'9')* Exponent? FloatTypeSuffix?
| '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
| ('0'..'9')+ Exponent FloatTypeSuffix?
| ('0'..'9')+ FloatTypeSuffix
| ('0x' | '0X') (HexDigit )*
('.' (HexDigit)*)?
( 'p' | 'P' )
( '+' | '-' )?
( '0' .. '9' )+
FloatTypeSuffix?
;// $ANTLR src "JavaCombined.g" 930
fragment
Exponent : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;// $ANTLR src "JavaCombined.g" 933
fragment
FloatTypeSuffix : ('f'|'F'|'d'|'D') ;// $ANTLR src "JavaCombined.g" 936
CharacterLiteral
: '\'' ( EscapeSequence | ~('\''|'\\') ) '\''
;// $ANTLR src "JavaCombined.g" 940
StringLiteral
: '"' ( EscapeSequence | ~('\\'|'"') )* '"'
;// $ANTLR src "JavaCombined.g" 944
fragment
EscapeSequence
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
| UnicodeEscape
| OctalEscape
;// $ANTLR src "JavaCombined.g" 951
fragment
OctalEscape
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;// $ANTLR src "JavaCombined.g" 958
fragment
UnicodeEscape
: '\\' 'u' HexDigit HexDigit HexDigit HexDigit
;// $ANTLR src "JavaCombined.g" 963
ENUM: 'enum' {if (!enumIsKeyword) setType(Identifier);}
;// $ANTLR src "JavaCombined.g" 966
ASSERT
: 'assert' {if (!assertIsKeyword) setType(Identifier);}
;// $ANTLR src "JavaCombined.g" 970
Identifier
: Letter (Letter|JavaIDDigit)*
;// $ANTLR src "JavaCombined.g" 974
/**I found this char range in JavaCC's grammar, but Letter and Digit overlap.
Still works, but...
*/
fragment
Letter
: '\u0024' |
'\u0041'..'\u005a' |
'\u005f' |
'\u0061'..'\u007a' |
'\u00c0'..'\u00d6' |
'\u00d8'..'\u00f6' |
'\u00f8'..'\u00ff' |
'\u0100'..'\u1fff' |
'\u3040'..'\u318f' |
'\u3300'..'\u337f' |
'\u3400'..'\u3d2d' |
'\u4e00'..'\u9fff' |
'\uf900'..'\ufaff'
;// $ANTLR src "JavaCombined.g" 994
fragment
JavaIDDigit
: '\u0030'..'\u0039' |
'\u0660'..'\u0669' |
'\u06f0'..'\u06f9' |
'\u0966'..'\u096f' |
'\u09e6'..'\u09ef' |
'\u0a66'..'\u0a6f' |
'\u0ae6'..'\u0aef' |
'\u0b66'..'\u0b6f' |
'\u0be7'..'\u0bef' |
'\u0c66'..'\u0c6f' |
'\u0ce6'..'\u0cef' |
'\u0d66'..'\u0d6f' |
'\u0e50'..'\u0e59' |
'\u0ed0'..'\u0ed9' |
'\u1040'..'\u1049'
;// $ANTLR src "JavaCombined.g" 1013
WS : (' '|'\r'|'\t'|'\u000C'|'\n')+ {skip();}
;
LINE_COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {skip();}
;
COMMENT_START
: '/*' {pushMode(COMMENT_MODE); more();}
;
mode COMMENT_MODE;
COMMENT : '*/' {skip(); popMode();} ;
COMMENT_INSIDE : . {more();} ;

View File

@ -1,756 +0,0 @@
parser grammar JavaParser;
options {backtrack=true; memoize=true; tokenVocab=JavaLexer;}
// starting point for parsing a java file
/* The annotations are separated out to make parsing faster, but must be associated with
a packageDeclaration or a typeDeclaration (and not an empty one). */
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 // class or interface
| 'public' // class or interface
| 'protected' // class or interface
| 'private' // class or interface
| 'abstract' // class or interface
| 'static' // class or interface
| 'final' // class only -- does not apply to interfaces
| 'strictfp' // class or interface
;
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
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
;
// STATEMENTS / BLOCKS
block
: '{' blockStatement* '}'
;
blockStatement
: localVariableDeclarationStatement
| classOrInterfaceDeclaration
| statement
;
localVariableDeclarationStatement
: localVariableDeclaration ';'
;
localVariableDeclaration
: variableModifiers type variableDeclarators
;
variableModifiers
: variableModifier*
;
statement
: block
| ASSERT expression (':' expression)? ';'
| 'if' parExpression statement (options {k=1;}:'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)*
;
/* The change here (switchLabel -> switchLabel+) technically makes this grammar
ambiguous; but with appropriately greedy parsing it yields the most
appropriate AST, one in which each group, except possibly the last one, has
labels and statements. */
switchBlockStatementGroup
: switchLabel+ blockStatement*
;
switchLabel
: 'case' constantExpression ':'
| 'case' enumConstantName ':'
| 'default' ':'
;
forControl
options {k=3;} // be efficient for common case: for (ID ID : ID) ...
: enhancedForControl
| forInit? ';' expression? ';' forUpdate?
;
forInit
: localVariableDeclaration
| expressionList
;
enhancedForControl
: variableModifiers type Identifier ':' expression
;
forUpdate
: expressionList
;
// EXPRESSIONS
parExpression
: '(' expression ')'
;
expressionList
: expression (',' expression)*
;
statementExpression
: expression
;
constantExpression
: expression
;
expression
: conditionalExpression (assignmentOperator expression)?
;
assignmentOperator
: '='
| '+='
| '-='
| '*='
| '/='
| '&='
| '|='
| '^='
| '%='
| '<' '<' '='
| '>' '>' '>' '='
| '>' '>' '='
/*
| ('<' '<' '=')=> t1='<' t2='<' t3='='
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() &&
$t2.getLine() == $t3.getLine() &&
$t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() }?
| ('>' '>' '>' '=')=> t1='>' t2='>' t3='>' t4='='
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() &&
$t2.getLine() == $t3.getLine() &&
$t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() &&
$t3.getLine() == $t4.getLine() &&
$t3.getCharPositionInLine() + 1 == $t4.getCharPositionInLine() }?
| ('>' '>' '=')=> t1='>' t2='>' t3='='
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() &&
$t2.getLine() == $t3.getLine() &&
$t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() }?
*/
;
conditionalExpression
: conditionalOrExpression ( '?' conditionalExpression ':' conditionalExpression )?
;
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
: '<' '='
| '>' '='
| '<'
| '>'
;
/*
relationalOp
: ('<' '=')=> t1='<' t2='='
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }?
| ('>' '=')=> t1='>' t2='='
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }?
| '<'
| '>'
;
*/
shiftExpression
: additiveExpression ( shiftOp additiveExpression )*
;
shiftOp
: '<' '<'
| '>' '>' '>'
| '>' '>'
;
/*
shiftOp
: ('<' '<')=> t1='<' t2='<'
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }?
| ('>' '>' '>')=> t1='>' t2='>' t3='>'
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() &&
$t2.getLine() == $t3.getLine() &&
$t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() }?
| ('>' '>')=> t1='>' t2='>'
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }?
;
*/
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 ']')+ // can also be matched by selector, but do here
| 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? ')'
;

View File

@ -1,756 +0,0 @@
parser grammar JavaParser;
options {backtrack=true; memoize=true; tokenVocab=JavaLexer;}
// starting point for parsing a java file
/* The annotations are separated out to make parsing faster, but must be associated with
a packageDeclaration or a typeDeclaration (and not an empty one). */
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 // class or interface
| 'public' // class or interface
| 'protected' // class or interface
| 'private' // class or interface
| 'abstract' // class or interface
| 'static' // class or interface
| 'final' // class only -- does not apply to interfaces
| 'strictfp' // class or interface
;
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
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
;
// STATEMENTS / BLOCKS
block
: '{' blockStatement* '}'
;
blockStatement
: localVariableDeclarationStatement
| classOrInterfaceDeclaration
| statement
;
localVariableDeclarationStatement
: localVariableDeclaration ';'
;
localVariableDeclaration
: variableModifiers type variableDeclarators
;
variableModifiers
: variableModifier*
;
statement
: block
| ASSERT expression (':' expression)? ';'
| 'if' parExpression statement (options {k=1;}:'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)*
;
/* The change here (switchLabel -> switchLabel+) technically makes this grammar
ambiguous; but with appropriately greedy parsing it yields the most
appropriate AST, one in which each group, except possibly the last one, has
labels and statements. */
switchBlockStatementGroup
: switchLabel+ blockStatement*
;
switchLabel
: 'case' constantExpression ':'
| 'case' enumConstantName ':'
| 'default' ':'
;
forControl
options {k=3;} // be efficient for common case: for (ID ID : ID) ...
: enhancedForControl
| forInit? ';' expression? ';' forUpdate?
;
forInit
: localVariableDeclaration
| expressionList
;
enhancedForControl
: variableModifiers type Identifier ':' expression
;
forUpdate
: expressionList
;
// EXPRESSIONS
parExpression
: '(' expression ')'
;
expressionList
: expression (',' expression)*
;
statementExpression
: expression
;
constantExpression
: expression
;
expression
: conditionalExpression (assignmentOperator expression)?
;
assignmentOperator
: '='
| '+='
| '-='
| '*='
| '/='
| '&='
| '|='
| '^='
| '%='
| '<' '<' '='
| '>' '>' '>' '='
| '>' '>' '='
/*
| ('<' '<' '=')=> t1='<' t2='<' t3='='
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() &&
$t2.getLine() == $t3.getLine() &&
$t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() }?
| ('>' '>' '>' '=')=> t1='>' t2='>' t3='>' t4='='
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() &&
$t2.getLine() == $t3.getLine() &&
$t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() &&
$t3.getLine() == $t4.getLine() &&
$t3.getCharPositionInLine() + 1 == $t4.getCharPositionInLine() }?
| ('>' '>' '=')=> t1='>' t2='>' t3='='
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() &&
$t2.getLine() == $t3.getLine() &&
$t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() }?
*/
;
conditionalExpression
: conditionalOrExpression ( '?' conditionalExpression ':' conditionalExpression )?
;
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
: '<' '='
| '>' '='
| '<'
| '>'
;
/*
relationalOp
: ('<' '=')=> t1='<' t2='='
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }?
| ('>' '=')=> t1='>' t2='='
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }?
| '<'
| '>'
;
*/
shiftExpression
: additiveExpression ( shiftOp additiveExpression )*
;
shiftOp
: '<' '<'
| '>' '>' '>'
| '>' '>'
;
/*
shiftOp
: ('<' '<')=> t1='<' t2='<'
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }?
| ('>' '>' '>')=> t1='>' t2='>' t3='>'
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() &&
$t2.getLine() == $t3.getLine() &&
$t2.getCharPositionInLine() + 1 == $t3.getCharPositionInLine() }?
| ('>' '>')=> t1='>' t2='>'
{ $t1.getLine() == $t2.getLine() &&
$t1.getCharPositionInLine() + 1 == $t2.getCharPositionInLine() }?
;
*/
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 ']')+ // can also be matched by selector, but do here
| 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? ')'
;

View File

@ -1,6 +0,0 @@
lexer grammar L;
STRING_START : '"' -> pushMode(STRING_MODE), more ;
WS : (' '|'\n') -> skip ;
mode STRING_MODE;
STRING : '"' -> popMode ;
ANY : . -> more ;

View File

@ -1,164 +0,0 @@
/**
derived from http://svn.r-project.org/R/trunk/src/main/gram.y
http://cran.r-project.org/doc/manuals/R-lang.html#Parser
*/
grammar R;
// ambig upon a(i)<- (delayed a bit since ';' could follow--really ambig on "a(i)")
/** ambig since stacks are exact as it loops around; no way to distinguish
I tried tracking input index in stack to differentiate the 2 invocations
of expr_or_assign, but that would mean altering the our context from
the decision-making in expr_or_assign. Also, later we need to have
context stacks that are not dependent on input position to reuse them.
The fact that the recursive version correctly matches the input while the
looping version does not is a problem. We base the notion of ambiguous
on the same state, different alternatives, same stack. But, if the
rule invocation stack does not uniquely indicate context, we are not accurately
detecting ambiguities. We are detecting ambiguities overzealously.
We need a way for the context stack or configuration to distinguish between
iterations of the loop that dive into the same rule such as expr_or_assign*.
Perhaps the answer is to track iteration number in the configuration:
(s, alt, ctx, iter#)
When we reached the state following '<-', say p, in expr then we need
(p, 1, [expr expr_or_assign prog], 1)
(p, 2, [expr expr_or_assign prog], 2)
But, that number would be useful... we might pass through 3 or 4 loops.
The iteration index really has to be a part of the stack context.
Perhaps we and an additional stack element as if we were doing the
recursive version
prog : expr_or_assign prog | ;
(p, 1, [expr expr_or_assign prog])
(p, 2, [expr expr_or_assign prog expr_or_assign prog])
The "expr expr_or_assign prog" represents the second call back down
into expr_or_assign like the loop would except that the stack looks different.
Or, we could mark stack references with the loop iteration index.
(p, 1, [expr expr_or_assign prog])
(p, 2, [expr expr_or_assign.2 prog])
This seems reusable as opposed to the input index. It might be complicated
to track this. In the general case, we would need a mapping from rule
invocation of rule r to a count, and within a specific rule context. That
might add a HashMap for every RuleContext. ick. Also, what about the context
that I create during ATN simulation? I would have to track that as well
as the generated code in the parser. Rule invocation states would act
like triggers that would bump account for that target rule in the current ctx.
Actually, maybe only my ATN sim would have to do it. prog then expr_or_assign
would be real elements on stack then I would create expr, expr_primary, pop
them both (for 2nd alt of expr_or_assign) and pop back into prog. Then, I'd
push expr_or_assign again but could notice I was calling 2nd time from prog.
Maybe make one big map: count[ctx][invocation-state] -> value to keep out
of RuleContext. Used only during sim anyway.
Make sure that this doesn't cause r* for optional r to miss an ambiguity
since 2nd invocation would have diff stack.
*/
prog : expr_or_assign* ;
/** This one is not ambig since 2nd time into expr_or_assign has different
context where expr_or_assign* shows same context.
*/
//prog : expr_or_assign expr_or_assign ;
// not ambig, context different
//prog : expr_or_assign prog | ;
expr_or_assign
@after {System.out.println(getRuleInvocationStack());}
: expr '++'
| expr // match ID a, fall out, reenter, match "(i)<-x" via alt 1
// it thinks it's same context from prog, but it's not; it's
// 2nd time through expr_or_assign* loop.
;
expr : expr_primary ('<-' ID)? ;
expr_primary
: '(' ID ')'
| ID '(' ID ')'
| ID
;
/*
expr : '(' ID ')' // and this
| expr '<-'<assoc=right> ID
| ID '(' ID ')'
| ID
;
*/
HEX : '0' ('x'|'X') HEXDIGIT+ [Ll]? ;
INT : DIGIT+ [Ll]? ;
fragment
HEXDIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
FLOAT : DIGIT+ '.' DIGIT* EXP? [Ll]?
| DIGIT+ EXP? [Ll]?
| '.' DIGIT+ EXP? [Ll]?
;
fragment
DIGIT : '0'..'9' ;
fragment
EXP : ('E' | 'e') ('+' | '-')? INT ;
COMPLEX : INT 'i'
| FLOAT 'i'
;
STRING : '"' ( ESC | ~('\\'|'"') )* '"'
| '\'' ( ESC | ~('\\'|'\'') )* '\''
;
fragment
ESC
: '\\' ([abtnfrv]|'"'|'\'')
| UNICODE_ESCAPE
| HEX_ESCAPE
| OCTAL_ESCAPE
;
fragment
UNICODE_ESCAPE
: '\\' 'u' HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT
| '\\' 'u' '{' HEXDIGIT HEXDIGIT HEXDIGIT HEXDIGIT '}'
;
fragment
OCTAL_ESCAPE
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;
fragment
HEX_ESCAPE
: '\\' HEXDIGIT HEXDIGIT?
;
ID : '.'? (LETTER|'_'|'.') (LETTER|DIGIT|'_'|'.')*
| LETTER (LETTER|DIGIT|'_'|'.')*
;
fragment
LETTER : 'a'..'z'|'A'..'Z'|'\u0080'..'\u00FF' ;
USER_OP : '%' .*? '%' ;
COMMENT : '#' .*? '\n' {skip();} ;
/** Doesn't handle '\n' correctly. it's context-sensitive */
WS : (' '|'\t'|'\n'|'\r')+ {skip();} ;

File diff suppressed because it is too large Load Diff

View File

@ -1,31 +0,0 @@
scannerless grammar T;
method : 'fun' name=ID '(' a+=arg (',' a+=arg)* ')' body ;
body : '{' (body|CMT|.)* '}' ; // nongreedy
stat: 'return' INT
| ID '=' expr
;
expr: atom ('*' atom)* ;
atom: INT
| sql
;
sql : 'select' '*' 'from' ID ;
// literals like 'select' become 'select' WS rules? hmm..nope
// might not want WS. or might need &!id-letter or something
always call WS implicitly after any token match; if don't want, then make WS undefined.
RETURN : 'return' {!Character.isJavaId(input.LA(1))}? WS ;
ID : 'a'..'z'+ ;
WS : (' '|'\n')* ;
CMT : '/*' (options {greedy=false;}:.)* '*'/ ;

View File

@ -1 +0,0 @@
34 abc

View File

@ -1,3 +0,0 @@
grammar T;
s : ({}A|B|C)? ;

View File

@ -1,26 +0,0 @@
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenFactory;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.UnbufferedCharStream;
import java.io.FileInputStream;
import java.io.InputStream;
public class TestA {
public static void main(String[] args) throws Exception {
String inputFile = null;
if ( args.length>0 ) inputFile = args[0];
InputStream is = System.in;
if ( inputFile!=null ) {
is = new FileInputStream(inputFile);
}
CharStream input = new UnbufferedCharStream(is);
A lex = new A(input);
lex.setTokenFactory(new CommonTokenFactory(true));
CommonTokenStream tokens = new CommonTokenStream(lex);
tokens.fill();
System.out.println(tokens.getTokens());
}
}

View File

@ -1,34 +0,0 @@
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.CommonTokenFactory;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.UnbufferedCharStream;
import org.antlr.v4.runtime.UnbufferedTokenStream;
import java.io.FileInputStream;
import java.io.InputStream;
public class TestCSV {
public static void main(String[] args) throws Exception {
String inputFile = null;
if ( args.length>0 ) inputFile = args[0];
InputStream is = System.in;
if ( inputFile!=null ) {
is = new FileInputStream(inputFile);
}
CharStream input = new UnbufferedCharStream(is);
CSVLexer lex = new CSVLexer(input);
// copy text out of sliding buffer and store in tokens
lex.setTokenFactory(new CommonTokenFactory(true));
TokenStream tokens = new UnbufferedTokenStream<CommonToken>(lex);
CSVParser parser = new CSVParser(tokens);
parser.setBuildParseTree(false);
parser.file();
// Token t = tokens.LT(1);
// while ( t.getType()!=-1 ) {
// tokens.consume();
// t = tokens.LT(1);
// }
}
}

View File

@ -1,11 +0,0 @@
import org.antlr.v4.runtime.*;
public class TestE {
public static void main(String[] args) throws Exception {
CharStream input = new ANTLRFileStream(args[0]);
ELexer lex = new ELexer(input);
CommonTokenStream tokens = new CommonTokenStream(lex);
tokens.fill();
for (Object t : tokens.getTokens()) System.out.println(t);
}
}

View File

@ -1,145 +0,0 @@
import org.antlr.runtime.debug.BlankDebugEventListener;
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.atn.LexerATNSimulator;
import org.antlr.v4.runtime.atn.ParserATNSimulator;
import org.antlr.v4.runtime.atn.PredictionMode;
import java.io.File;
/** Parse a java file or directory of java files using the generated parser
* ANTLR builds from java.g4
*/
class TestJava {
public static long lexerTime = 0;
public static boolean profile = false;
public static JavaLexer lexer;
public static JavaParser parser = null;
public static boolean showTree = false;
public static boolean printTree = false;
public static boolean SLL = false;
public static boolean diag = false;
public static void main(String[] args) {
doAll(args);
// doAll(args);
// doAll(args);
}
static void doAll(String[] args) {
try {
lexerTime = 0;
long start = System.currentTimeMillis();
if (args.length > 0 ) {
// for each directory/file specified on the command line
for(int i=0; i< args.length;i++) {
if ( args[i].equals("-tree") ) showTree = true;
else if ( args[i].equals("-ptree") ) printTree = true;
else if ( args[i].equals("-SLL") ) SLL = true;
else if ( args[i].equals("-diag") ) diag = true;
doFile(new File(args[i])); // parse it
}
}
else {
System.err.println("Usage: java Main <directory or file name>");
}
long stop = System.currentTimeMillis();
System.out.println("Lexer total time " + lexerTime + "ms.");
System.out.println("Total time " + (stop - start) + "ms.");
System.out.println("finished parsing OK");
System.out.println(LexerATNSimulator.match_calls+" lexer match calls");
System.out.println(ParserATNSimulator.predict_calls +" parser predict calls");
System.out.println(ParserATNSimulator.retry_with_context +" retry_with_context after SLL conflict");
System.out.println(ParserATNSimulator.retry_with_context_indicates_no_conflict +" retry sees no conflict");
if ( profile ) {
System.out.println("num decisions "+profiler.numDecisions);
}
}
catch(Exception e) {
System.err.println("exception: "+e);
e.printStackTrace(System.err); // so we can get stack trace
}
}
// This method decides what action to take based on the type of
// file we are looking at
public static void doFile(File f)
throws Exception {
// If this is a directory, walk each file/dir in that directory
if (f.isDirectory()) {
String files[] = f.list();
for(int i=0; i < files.length; i++)
doFile(new File(f, files[i]));
}
// otherwise, if this is a java file, parse it!
else if ( ((f.getName().length()>5) &&
f.getName().substring(f.getName().length()-5).equals(".java"))
|| f.getName().equals("input") )
{
System.err.println("parsing "+f.getAbsolutePath());
parseFile(f.getAbsolutePath());
}
}
static class CountDecisions extends BlankDebugEventListener {
public int numDecisions = 0;
public void enterDecision(int decisionNumber) {
numDecisions++;
}
}
static CountDecisions profiler = new CountDecisions();
// Here's where we do the real work...
public static void parseFile(String f)
throws Exception {
try {
// Create a scanner that reads from the input stream passed to us
if ( lexer==null ) {
lexer = new JavaLexer(null);
}
lexer.setInputStream(new ANTLRFileStream(f));
CommonTokenStream tokens = new CommonTokenStream(lexer);
long start = System.currentTimeMillis();
tokens.fill();
// System.out.println(tokens.getTokens());
long stop = System.currentTimeMillis();
lexerTime += stop-start;
// for (Object t : tokens.getTokens()) {
// System.out.println(t);
// }
if ( true ) {
// Create a parser that reads from the scanner
if ( parser==null ) {
parser = new JavaParser(tokens);
// parser.getInterpreter().setContextSensitive(false);
// parser.setErrorHandler(new BailErrorStrategy<Token>());
// parser.getInterpreter().setContextSensitive(true);
}
parser.setTokenStream(tokens);
if ( diag ) parser.addErrorListener(new DiagnosticErrorListener());
if ( SLL ) parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
// start parsing at the compilationUnit rule
ParserRuleContext tree = parser.compilationUnit();
if ( showTree ) tree.inspect(parser);
if ( printTree ) System.out.println(tree.toStringTree(parser));
//System.err.println("finished "+f);
// System.out.println("cache size = "+DefaultErrorStrategy.cache.size());
}
}
catch (Exception e) {
System.err.println("parser exception: "+e);
e.printStackTrace(); // so we can get stack trace
}
}
}

View File

@ -1,274 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.atn.LexerATNSimulator;
import org.antlr.v4.runtime.atn.ParserATNSimulator;
import org.antlr.v4.runtime.atn.PredictionMode;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
class TestJavaLR {
// public static long lexerTime = 0;
public static boolean profile = false;
public static boolean notree = false;
public static boolean gui = false;
public static boolean printTree = false;
public static boolean SLL = false;
public static boolean diag = false;
public static boolean bail = false;
public static boolean x2 = false;
public static boolean threaded = false;
public static boolean quiet = false;
// public static long parserStart;
// public static long parserStop;
public static Worker[] workers = new Worker[3];
static int windex = 0;
public static CyclicBarrier barrier;
public static volatile boolean firstPassDone = false;
public static class Worker implements Runnable {
public long parserStart;
public long parserStop;
List<String> files;
public Worker(List<String> files) {
this.files = files;
}
@Override
public void run() {
parserStart = System.currentTimeMillis();
for (String f : files) {
parseFile(f);
}
parserStop = System.currentTimeMillis();
try {
barrier.await();
}
catch (InterruptedException ex) {
return;
}
catch (BrokenBarrierException ex) {
return;
}
}
}
public static void main(String[] args) {
doAll(args);
}
public static void doAll(String[] args) {
List<String> inputFiles = new ArrayList<String>();
long start = System.currentTimeMillis();
try {
if (args.length > 0 ) {
// for each directory/file specified on the command line
for(int i=0; i< args.length;i++) {
if ( args[i].equals("-notree") ) notree = true;
else if ( args[i].equals("-gui") ) gui = true;
else if ( args[i].equals("-ptree") ) printTree = true;
else if ( args[i].equals("-SLL") ) SLL = true;
else if ( args[i].equals("-bail") ) bail = true;
else if ( args[i].equals("-diag") ) diag = true;
else if ( args[i].equals("-2x") ) x2 = true;
else if ( args[i].equals("-threaded") ) threaded = true;
else if ( args[i].equals("-quiet") ) quiet = true;
if ( args[i].charAt(0)!='-' ) { // input file name
inputFiles.add(args[i]);
}
}
List<String> javaFiles = new ArrayList<String>();
for (String fileName : inputFiles) {
List<String> files = getFilenames(new File(fileName));
javaFiles.addAll(files);
}
doFiles(javaFiles);
// DOTGenerator gen = new DOTGenerator(null);
// String dot = gen.getDOT(JavaLRParser._decisionToDFA[112], false);
// System.out.println(dot);
// dot = gen.getDOT(JavaLRParser._decisionToDFA[81], false);
// System.out.println(dot);
if ( x2 ) {
System.gc();
System.out.println("waiting for 1st pass");
if ( threaded ) while ( !firstPassDone ) { } // spin
System.out.println("2nd pass");
doFiles(javaFiles);
}
}
else {
System.err.println("Usage: java Main <directory or file name>");
}
}
catch(Exception e) {
System.err.println("exception: "+e);
e.printStackTrace(System.err); // so we can get stack trace
}
long stop = System.currentTimeMillis();
// System.out.println("Overall time " + (stop - start) + "ms.");
System.gc();
}
public static void doFiles(List<String> files) throws Exception {
long parserStart = System.currentTimeMillis();
// lexerTime = 0;
if ( threaded ) {
barrier = new CyclicBarrier(3,new Runnable() {
public void run() {
report(); firstPassDone = true;
}
});
int chunkSize = files.size() / 3; // 10/3 = 3
int p1 = chunkSize; // 0..3
int p2 = 2 * chunkSize; // 4..6, then 7..10
workers[0] = new Worker(files.subList(0,p1+1));
workers[1] = new Worker(files.subList(p1+1,p2+1));
workers[2] = new Worker(files.subList(p2+1,files.size()));
new Thread(workers[0], "worker-"+windex++).start();
new Thread(workers[1], "worker-"+windex++).start();
new Thread(workers[2], "worker-"+windex++).start();
}
else {
for (String f : files) {
parseFile(f);
}
long parserStop = System.currentTimeMillis();
System.out.println("Total lexer+parser time " + (parserStop - parserStart) + "ms.");
}
}
private static void report() {
// parserStop = System.currentTimeMillis();
// System.out.println("Lexer total time " + lexerTime + "ms.");
long time = 0;
if ( workers!=null ) {
// compute max as it's overlapped time
for (Worker w : workers) {
long wtime = w.parserStop - w.parserStart;
time = Math.max(time,wtime);
System.out.println("worker time " + wtime + "ms.");
}
}
System.out.println("Total lexer+parser time " + time + "ms.");
System.out.println("finished parsing OK");
System.out.println(LexerATNSimulator.match_calls+" lexer match calls");
System.out.println(ParserATNSimulator.predict_calls +" parser predict calls");
System.out.println(ParserATNSimulator.retry_with_context +" retry_with_context after SLL conflict");
System.out.println(ParserATNSimulator.retry_with_context_indicates_no_conflict +" retry sees no conflict");
System.out.println(ParserATNSimulator.retry_with_context_predicts_same_alt +" retry predicts same alt as resolving conflict");
}
public static List<String> getFilenames(File f) throws Exception {
List<String> files = new ArrayList<String>();
getFilenames_(f, files);
return files;
}
public static void getFilenames_(File f, List<String> files) throws Exception {
// If this is a directory, walk each file/dir in that directory
if (f.isDirectory()) {
String flist[] = f.list();
for(int i=0; i < flist.length; i++) {
getFilenames_(new File(f, flist[i]), files);
}
}
// otherwise, if this is a java file, parse it!
else if ( ((f.getName().length()>5) &&
f.getName().substring(f.getName().length()-5).equals(".java")) )
{
files.add(f.getAbsolutePath());
}
}
// This method decides what action to take based on the type of
// file we are looking at
// public static void doFile_(File f) throws Exception {
// // If this is a directory, walk each file/dir in that directory
// if (f.isDirectory()) {
// String files[] = f.list();
// for(int i=0; i < files.length; i++) {
// doFile_(new File(f, files[i]));
// }
// }
//
// // otherwise, if this is a java file, parse it!
// else if ( ((f.getName().length()>5) &&
// f.getName().substring(f.getName().length()-5).equals(".java")) )
// {
// System.err.println(f.getAbsolutePath());
// parseFile(f.getAbsolutePath());
// }
// }
public static void parseFile(String f) {
try {
if ( !quiet ) System.err.println(f);
// Create a scanner that reads from the input stream passed to us
Lexer lexer = new JavaLRLexer(new ANTLRFileStream(f));
CommonTokenStream tokens = new CommonTokenStream(lexer);
// long start = System.currentTimeMillis();
// tokens.fill(); // load all and check time
// long stop = System.currentTimeMillis();
// lexerTime += stop-start;
// Create a parser that reads from the scanner
JavaLRParser parser = new JavaLRParser(tokens);
if ( diag ) parser.addErrorListener(new DiagnosticErrorListener());
if ( bail ) parser.setErrorHandler(new BailErrorStrategy());
if ( SLL ) parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
// start parsing at the compilationUnit rule
ParserRuleContext t = parser.compilationUnit();
if ( notree ) parser.setBuildParseTree(false);
if ( gui ) t.inspect(parser);
if ( printTree ) System.out.println(t.toStringTree(parser));
}
catch (Exception e) {
System.err.println("parser exception: "+e);
e.printStackTrace(); // so we can get stack trace
}
}
}

View File

@ -1,44 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
public class TestL {
public static void main(String[] args) throws Exception {
CharStream input = new ANTLRFileStream(args[0]);
// input = new ANTLRStringStream("3 3");
L lexer = new L(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
// System.out.println(tokens.getTokens());
for (Object t : tokens.getTokens()) System.out.println(t);
}
}

View File

@ -1,47 +0,0 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
public class TestR {
public static void main(String[] args) throws Exception {
RLexer t = new RLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(t);
// tokens.fill();
// for (Object tok : tokens.getTokens()) {
// System.out.println(tok);
// }
RParser p = new RParser(tokens);
p.setBuildParseTree(true);
p.addErrorListener(new DiagnosticErrorListener());
p.prog();
}
}

View File

@ -1,36 +0,0 @@
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenFactory;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import java.io.FileInputStream;
import java.io.InputStream;
public class TestT {
public static void main(String[] args) throws Exception {
String inputFile = null;
if ( args.length>0 ) inputFile = args[0];
InputStream is = System.in;
if ( inputFile!=null ) {
is = new FileInputStream(inputFile);
}
CharStream input = new ANTLRInputStream(is);
TLexer lex = new TLexer(input);
lex.setTokenFactory(new CommonTokenFactory(true));
CommonTokenStream tokens = new CommonTokenStream(lex);
TParser parser = new TParser(tokens);
parser.addErrorListener(new DiagnosticErrorListener());
// parser.getInterpreter().setSLL(true);
// parser.setTrace(true);
ParserRuleContext tree = parser.s();
System.out.println(tree.toStringTree(parser));
// tree.save(parser, "/tmp/t.ps");
}
}

View File

@ -1,13 +0,0 @@
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
public class TestU {
public static void main(String[] args) throws Exception {
ULexer t = new ULexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(t);
UParser p = new UParser(tokens);
p.setBuildParseTree(true);
// ParserRuleContext r = p.s();
// System.out.println(r.toStringTree(p));
}
}

View File

@ -1,14 +0,0 @@
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RuleContext;
public class TestW {
public static void main(String[] args) throws Exception {
WLexer t = new WLexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(t);
WParser p = new WParser(tokens);
p.setBuildParseTree(true);
RuleContext ctx = p.s();
//System.out.println("ctx="+ctx.toStringTree(p));
}
}

View File

@ -1,31 +0,0 @@
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenFactory;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import java.io.FileInputStream;
import java.io.InputStream;
public class TransformEType {
public static void main(String[] args) throws Exception {
String inputFile = null;
if ( args.length>0 ) inputFile = args[0];
InputStream is = System.in;
if ( inputFile!=null ) {
is = new FileInputStream(inputFile);
}
CharStream input = new ANTLRInputStream(is);
JavaLRLexer lex = new JavaLRLexer(input);
lex.setTokenFactory(new CommonTokenFactory(true));
CommonTokenStream tokens = new CommonTokenStream(lex);
JavaLRParser parser = new JavaLRParser(tokens);
parser.addErrorListener(new DiagnosticErrorListener());
ParserRuleContext tree = parser.compilationUnit();
}
}

View File

@ -1,5 +0,0 @@
grammar U;
a : X ;
X : 'a' -> skip ;
Y : 'z' -> skip, more ;

View File

@ -1,16 +0,0 @@
grammar W;
s
@init {setBuildParseTree(true);}
@after {System.out.println(_localctx.toStringTree(this));}
: a
;
a : 'x' | 'y'
;
Z : 'z';
EQ : '=' ;
INT : '0'..'9'+ ;
ID : 'a'..'z'+ ;
WS : (' '|'\n')+ {skip();} ;

View File

@ -1,2 +0,0 @@
0.9962269825793676, 0.9224608616182103
0.91673278673353, -0.6374985722530822
1 0.9962269825793676 0.9224608616182103
2 0.91673278673353 -0.6374985722530822

View File

@ -1,29 +0,0 @@
java -Xmx500M -cp "/usr/local/lib/antlr4-complete.jar:$CLASSPATH" org.antlr.v4.Tool JavaLexer.g
java -Xmx500M -cp "/usr/local/lib/antlr4-complete.jar:$CLASSPATH" org.antlr.v4.Tool JavaParser.g
javac -cp .:/usr/local/lib/antlr4-complete.jar *.java
time java -Xmx400M -cp .:/usr/local/lib/antlr4-complete.jar TestJava .
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./input
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./JavaLexer.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./JavaParser.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./L.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./MLexer.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./MParser.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./T.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./TestJava.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./TestL.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./TestT.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./TestYang.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./TLexer.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./TParser.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./U.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./UParser.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./YangJavaLexer.java
# parsing /Users/parrt/antlr/code/antlr4/main/tool/playground/./YangJavaParser.java
# Lexer total time 182ms.
# Total time 603ms.
# finished parsing OK
# 726 lexer failovers
# 104048 lexer match calls
# 220 parser failovers
# 107785 parser predict calls