Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException in ATNConfig.hashCode #45

Closed
JFinis opened this issue Mar 17, 2012 · 10 comments
Closed

NullPointerException in ATNConfig.hashCode #45

JFinis opened this issue Mar 17, 2012 · 10 comments

Comments

@JFinis
Copy link

JFinis commented Mar 17, 2012

Hi,

I receive a NullPointerException when running the generated parser on an input. If you need a grammar and input to provoque the bug, let me know (but that will take some time).

Here is the stack trace:

Exception in thread "main" java.lang.NullPointerException
    at org.antlr.v4.runtime.atn.ATNConfig.hashCode(ATNConfig.java:149)
    at java.util.HashMap.put(Unknown Source)
    at java.util.HashSet.add(Unknown Source)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:924)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:946)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:912)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.computeReachSet(ParserATNSimulator.java:711)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.execATN(ParserATNSimulator.java:521)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.predictATN(ParserATNSimulator.java:331)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.adaptivePredict(ParserATNSimulator.java:296)
    at org.eeve.antlrgen.parser.ANTLRv4Parser.strongExpr(ANTLRv4Parser.java:886)
    at org.eeve.antlrgen.parser.ANTLRv4Parser.expr(ANTLRv4Parser.java:1146)
    at org.eeve.antlrgen.parser.ANTLRv4Parser.grammarRule(ANTLRv4Parser.java:398)
    at org.eeve.antlrgen.parser.ANTLRv4Parser.gramma(ANTLRv4Parser.java:150)
    at org.eeve.antlrgen.ANTLRGenTest.parseStatic(ANTLRGenTest.java:92)
    at org.eeve.antlrgen.ANTLRGenTest.main(ANTLRGenTest.java:38)
@sharwell
Copy link
Member

Do you happen to know which commit you're using? (Ideally the SHA hash for it)

@JFinis
Copy link
Author

JFinis commented Mar 17, 2012

Yes, it is
28ee391
from parrt/antlr4

@sharwell
Copy link
Member

Can you add the following assertions to track down the source of failure?

In the two ATNConfig constructors that set state and semanticContext, add:

assert state != null;
assert semanticContext != null;

@JFinis
Copy link
Author

JFinis commented Mar 17, 2012

okay, here we go:

Exception in thread "main" java.lang.AssertionError: state is null
    at org.antlr.v4.runtime.atn.ATNConfig.<init>(ATNConfig.java:112)
    at org.antlr.v4.runtime.atn.ATNConfig.<init>(ATNConfig.java:98)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.actionTransition(ParserATNSimulator.java:1043)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.getEpsilonTarget(ParserATNSimulator.java:1032)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:990)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:946)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:1012)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.closure(ParserATNSimulator.java:912)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.computeReachSet(ParserATNSimulator.java:711)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.execATN(ParserATNSimulator.java:521)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.predictATN(ParserATNSimulator.java:331)
    at org.antlr.v4.runtime.atn.ParserATNSimulator.adaptivePredict(ParserATNSimulator.java:296)
    at org.eeve.antlrgen.parser.ANTLRv4Parser.strongExpr(ANTLRv4Parser.java:886)
    at org.eeve.antlrgen.parser.ANTLRv4Parser.expr(ANTLRv4Parser.java:1146)
    at org.eeve.antlrgen.parser.ANTLRv4Parser.grammarRule(ANTLRv4Parser.java:398)
    at org.eeve.antlrgen.parser.ANTLRv4Parser.gramma(ANTLRv4Parser.java:150)
    at org.eeve.antlrgen.ANTLRGenTest.parseStatic(ANTLRGenTest.java:92)
    at org.eeve.antlrgen.ANTLRGenTest.main(ANTLRGenTest.java:38)

@sharwell
Copy link
Member

It would definitely help if you are able to post an example grammar and input for me to duplicate this issue. You may not have to post input - it looks like you have a case where Transition is being constructed with target==null. You could add an assertion to the Transition constructor as a check.

Have you tried regenerating the code?

@JFinis
Copy link
Author

JFinis commented Mar 18, 2012

Okay, here is the grammar

grammar ANTLRv4;

ruler returns [String r]
  :  t=ID  ':' alts+=expr ('|' alts+= expr)* ';'
      {  $r = "x"; }
   ;

strongExpr returns [String r]
  : ID                      { $r = "x"; }
  | '(' ep=parenExpr ')'            { $r = "x"; }
  | e1=strongExpr '?'             { $r = "x"; }
  ;  

expr returns [String r]
  : e=strongExpr (e2=expr)? { $r = "x"; }
  | e1=expr '->' ID {$r = "x"; }
  ;

parenExpr returns [String r]
  : e1=expr '|' e2=parenExpr { $r = "x"; }
  | expr              {$r = $expr.r; }
  ;

ID  :   ('A'..'Z'|'a'..'z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;    

And here is a test input:

x:x;

Code for parser execution:

ANTLRv4Lexer lex = new ANTLRv4Lexer(new ANTLRInputStream(new FileInputStream(new File("test.input"))));
CommonTokenStream tokens = new CommonTokenStream(lex);

new ANTLRv4Parser(tokens).ruler();

@sharwell
Copy link
Member

Thanks, that's perfect. I was able to derive the following grammar as a unit test for the issue which I'm now working to fix.

grammar T;
start
  : ID ':' expr
  ;
expr
  : primary expr? {}
  | expr '->' ID
  ;
primary
  : ID
  ;

ID : [a-z]+;

Input: x:x

@JFinis
Copy link
Author

JFinis commented Mar 27, 2012

well, after merging your commits, I get the anticipated exception during parser building

    [java] Exception in thread "main" java.lang.IllegalStateException: Cannot serialize a transition to a removed state.
    [java]  at org.antlr.v4.automata.ATNSerializer.serialize(ATNSerializer.java:138)
    [java]  at org.antlr.v4.automata.ATNSerializer.getSerialized(ATNSerializer.java:281)
    [java]  at org.antlr.v4.codegen.model.SerializedATN.<init>(SerializedATN.java:43)
    [java]  at org.antlr.v4.codegen.model.Parser.<init>(Parser.java:86)
    [java]  at org.antlr.v4.codegen.ParserFactory.parser(ParserFactory.java:54)
    [java]  at org.antlr.v4.codegen.OutputModelController.parser(OutputModelController.java:145)
    [java]  at org.antlr.v4.codegen.OutputModelController.buildParserOutputModel(OutputModelController.java:84)
    [java]  at org.antlr.v4.codegen.CodeGenerator.generateParser(CodeGenerator.java:132)
    [java]  at org.antlr.v4.codegen.CodeGenPipeline.process(CodeGenPipeline.java:51)
    [java]  at org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:353)
    [java]  at org.antlr.v4.Tool.process(Tool.java:319)
    [java]  at org.antlr.v4.Tool.processGrammarsOnCommandLine(Tool.java:283)
    [java]  at org.antlr.v4.Tool.main(Tool.java:173)

While this is better than a fail at runtime, it is still not satisfactory, as I don't know what I have done wrong.

@sharwell
Copy link
Member

Once this issue is corrected, it will be tagged with "status:pull-request" until it gets closed. :) The test I've implemented so far is in place to help track down the issue, but like you saw it doesn't create a working grammar.

@sharwell
Copy link
Member

I am no longer able to reproduce this bug.

parrt pushed a commit that referenced this issue Jun 30, 2015
Add the ability to explicitly specify the path to the Java executable
parrt pushed a commit that referenced this issue Jun 30, 2015
Added optional predicate parameter to ParserRuleContext.getChildren(), a...
parrt pushed a commit that referenced this issue Nov 7, 2016
small rule attribute fixes
@ericvergnaud ericvergnaud mentioned this issue Nov 8, 2016
sharwell added a commit to sharwell/antlr4 that referenced this issue Dec 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants