got java LR parser working :) allows e A B C e type LR alts now

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9506]
This commit is contained in:
parrt 2011-11-30 18:57:45 -08:00
parent b6b4583b92
commit a8219d943e
6 changed files with 109 additions and 72 deletions

View File

@ -43,7 +43,7 @@ import org.stringtemplate.v4.misc.MultiMap;
import java.util.*;
public class ParserATNSimulator<Symbol> extends ATNSimulator {
public static boolean debug = false;
public static boolean debug = true;
public static boolean dfa_debug = false;
public static int ATN_failover = 0;

View File

@ -349,7 +349,7 @@ constructorBody
explicitConstructorInvocation
: nonWildcardTypeArguments? ('this' | 'super') arguments ';'
| expression '.' nonWildcardTypeArguments? 'super' arguments ';'
| primary '.' nonWildcardTypeArguments? 'super' arguments ';'
;
qualifiedName
@ -561,12 +561,7 @@ constantExpression
//expression : expression_[0] ;
expression
: '(' expression ')'
| 'this'
| 'super'
| literal
| Identifier
| type '.' 'class'
: primary
| expression '.' Identifier
| expression '.' 'this'
| expression '.' 'super' '(' expressionList? ')'
@ -582,8 +577,8 @@ expression
| ('~'|'!') expression
| expression ('*'|'/'|'%') expression
| expression ('+'|'-') expression
// | expression ('<' '<' | '>' '>' '>' | '>' '>') expression !!! can't handle multi-token ops :(
| expression ('<=' | '>=' | '>' | '<') expression
| expression ('<' '<' | '>' '>' '>' | '>' '>') expression
| expression ('<' '=' | '>' '=' | '>' | '<') expression
| expression 'instanceof' type
| expression ('==' | '!=') expression
| expression '&' expression
@ -601,27 +596,38 @@ expression
|'&='<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
(
{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]{}
{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>
@ -630,29 +636,29 @@ expression_[int _p]
|'&='<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]{}
| {25 >= $_p}? '.' Identifier
| {24 >= $_p}? '.' 'this'
| {23 >= $_p}? '.' 'super' '(' expressionList? ')'
| {22 >= $_p}? '.' 'new' Identifier '(' expressionList? ')'
| {21 >= $_p}? '.' 'super' '.' Identifier arguments?
| {20 >= $_p}? '.' explicitGenericInvocation
| {18 >= $_p}? '[' expression ']'
| {16 >= $_p}? ('++' | '--')
| {15 >= $_p}? '(' expressionList? ')'
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 12:07:02:218 left-recursion LogManager.java:48 expression_primary
: '(' type ')' expression_[17]{}
| ('+'|'-'|'++'|'--') expression_[14]{}
| ('~'|'!') expression_[13]{}
2011-11-30 18:39:48:343 left-recursion LogManager.java:48 expression_primary
: '(' type ')' expression_[18]
| ('+'|'-'|'++'|'--') expression_[15]
| ('~'|'!') expression_[14]
| '(' expression ')'
| 'this'
| 'super'
@ -662,6 +668,7 @@ expression_[int _p]
| 'new' creator
;
*/
creator
: nonWildcardTypeArguments createdName classCreatorRest
| createdName (arrayCreatorRest | classCreatorRest)
@ -718,6 +725,12 @@ FloatingPointLiteral
| '.' ('0'..'9')+ Exponent? FloatTypeSuffix?
| ('0'..'9')+ Exponent FloatTypeSuffix?
| ('0'..'9')+ FloatTypeSuffix
| ('0x' | '0X') (HexDigit )*
('.' (HexDigit)*)?
( 'p' | 'P' )
( '+' | '-' )?
( '0' .. '9' )+
FloatTypeSuffix?
;
fragment

View File

@ -1,7 +1,7 @@
grammar T;
s : a a;
a : ID {System.out.println("alt 1");}
| {true}? ID {System.out.println("alt 2");}
s : e ;
e : e ('+='<assoc=right>|'-='<assoc=right>) e
| INT
;
ID : 'a'..'z'+ ;
INT : '0'..'9'+;

View File

@ -121,7 +121,7 @@ class TestJavaLR {
CommonTokenStream tokens = new CommonTokenStream(lexer);
long start = System.currentTimeMillis();
// tokens.fill();
tokens.fill(); // load all and check time
// System.out.println(tokens.getTokens());
long stop = System.currentTimeMillis();
lexerTime += stop-start;
@ -130,7 +130,7 @@ class TestJavaLR {
// Create a parser that reads from the scanner
if ( parser==null ) {
parser = new JavaLRParser(null);
// parser.setBuildParseTree(true);
if ( showTree ) parser.setBuildParseTree(true);
// parser.setErrorHandler(new BailErrorStrategy<Token>());
// parser.getInterpreter().setContextSensitive(true);
}

View File

@ -1,30 +1,30 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
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:
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.
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.
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.
*/
package org.antlr.v4.parse;
@ -32,6 +32,7 @@ package org.antlr.v4.parse;
import org.antlr.runtime.CommonToken;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.antlr.runtime.tree.Tree;
import org.antlr.v4.Tool;
import org.antlr.v4.codegen.CodeGenerator;
import org.antlr.v4.tool.AttributeDict;
@ -125,6 +126,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
altTree = altTree.dupTree();
stripLeftRecursion(altTree);
stripAssocOptions(altTree);
// rewrite e to be e_[rec_arg]
int nextPrec = nextPrecedence(alt);
@ -135,7 +137,6 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
String altText = text(altTree);
altText = altText.trim();
altText += "{}"; // add empty alt to prevent pred hoisting
ST nameST = recRuleTemplates.getInstanceOf("recRuleName");
nameST.add("ruleName", ruleName);
if ( rewriteTree!=null ) {
@ -153,6 +154,7 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
altTree = altTree.dupTree();
stripLeftRecursion(altTree);
stripAssocOptions(altTree);
int nextPrec = nextPrecedence(alt);
ST refST = recRuleTemplates.getInstanceOf("recRuleRef");
@ -162,7 +164,6 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
String altText = text(altTree);
altText = altText.trim();
altText += "{}"; // add empty alt to prevent pred hoisting
ST nameST = recRuleTemplates.getInstanceOf("recRuleName");
nameST.add("ruleName", ruleName);
if ( rewriteTree!=null ) {
@ -186,7 +187,6 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
altTree = replaceRuleRefs(altTree, refST.render());
String altText = text(altTree);
altText = altText.trim();
altText += "{}"; // add empty alt to prevent pred hoisting
ST nameST = recRuleTemplates.getInstanceOf("recRuleName");
nameST.add("ruleName", ruleName);
@ -297,6 +297,27 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
return t;
}
public void stripAssocOptions(GrammarAST t) {
if ( t==null ) return;
for (GrammarAST options : t.getNodesWithType(ELEMENT_OPTIONS)) {
int i=0;
while ( i<options.getChildCount() ) {
GrammarAST c = (GrammarAST)options.getChild(i);
if ( c.getChild(0).getText().equals("assoc") ) {
options.deleteChild(i); // kill this option
}
else {
i++;
}
}
if ( options.getChildCount()==0 ) {
Tree parent = options.getParent();
parent.deleteChild(options.getChildIndex()); // no more options
return;
}
}
}
/**
* Match (RULE ID (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*)))
*/
@ -316,6 +337,9 @@ public class LeftRecursiveRuleAnalyzer extends LeftRecursiveRuleWalker {
return t;
}
// TODO: this strips the tree properly, but since text()
// uses the start of stop token index and gets text from that
// ineffectively ignores this routine.
public void stripLeftRecursion(GrammarAST altAST) {
GrammarAST rref = (GrammarAST)altAST.getChild(0);
if ( rref.getType()== ANTLRParser.RULE_REF &&

View File

@ -134,11 +134,11 @@ outerAlternative[GrammarAST rew] returns [boolean isLeftRec]
;
binary
: ^( ALT recurseNoLabel op=token recurse ) {setTokenPrec($op.t, currentOuterAltNumber);}
: ^( ALT recurseNoLabel (op=token)+ {setTokenPrec($op.t, currentOuterAltNumber);} recurse )
;
binaryMultipleOp
: ^( ALT recurseNoLabel ^( BLOCK ( ^( ALT op=token {setTokenPrec($op.t, currentOuterAltNumber);} ) )+ ) recurse )
: ^( ALT recurseNoLabel ^( BLOCK ( ^( ALT (op=token)+ {setTokenPrec($op.t, currentOuterAltNumber);} ) )+ ) recurse )
;
ternary