'Why does getInterpreter().adaptivePredict in generated parser return incorrect value?
I am creating custom language (EO language) plugin for IntelliJ. I use antlr4 adapter and I've already generated parser and lexer. I am working on syntax highlighting.
ANTLR4 plugin allows building tokens tree for EO code. I used this option and pasted simple EO program, the tree was built successfully. Nevertheless when I paste this code in IDEA I see an error.
After doing a research I found that problem appears in getInterpreter().adaptivePredict()
In my grammar there are 2 possible code variants (case 1 and case 2 in code below) but getInterpreter().adaptivePredict()
returns 3.
Why it works like this? May be my grammar is not correct but it works in ANTLR tree viewer (I checked several times and added picture in this post)
EO program
+alias org.eolang.io.stdout
+alias org.eolang.txt.sprintf
[args...] > main # This line is an abstraction. Highlighting error (">" is red, expecting '' or EOL)
[y] > leap
or. > @
and.
eq. (mod. y 4) 0
not. (eq. (mod. y 100) 0)
eq. (mod. y 400) 0
stdout > @
sprintf
"%d is %sa leap year!"
(args.get 0).as-int > year!
if. (leap year:y) "" "not "
It is how tree draws "abstraction" line in IDEA. It is correct.
Here is the code from my parser class.
public final AbstractionContext abstraction() throws RecognitionException {
AbstractionContext _localctx = new AbstractionContext(_ctx, getState());
enterRule(_localctx, 10, RULE_abstraction);
int _la;
try {
enterOuterAlt(_localctx, 1);
{
setState(99);
attributes();
setState(107);
_errHandler.sync(this);
/* ? */ System.out.println(getInterpreter().adaptivePredict(_input,14,_ctx)); // prints 3
switch ( getInterpreter().adaptivePredict(_input,14,_ctx) ) {
case 1:
{
{
setState(100);
suffix();
setState(104);
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,13,_ctx) ) {
case 1:
{
setState(101);
match(SPACE);
setState(102);
match(SLASH);
setState(103);
_la = _input.LA(1);
if ( !(_la==QUESTION || _la==NAME) ) {
_errHandler.recoverInline(this);
}
else {
if ( _input.LA(1)==Token.EOF ) matchedEOF = true;
_errHandler.reportMatch(this);
consume();
}
}
break;
}
}
}
break;
case 2:
{
setState(106);
htail();
}
break;
}
}
}
catch (RecognitionException re) {
_localctx.exception = re;
_errHandler.reportError(this, re);
_errHandler.recover(this, re);
}
finally {
exitRule();
}
return _localctx;
}
Here is part from EO.g4
abstraction
:
attributes
(
(suffix (SPACE SLASH (NAME | QUESTION))?)
| htail
)?
;
github link of project: https://github.com/yasamprom/EOplugin_antlrbased/blob/main/src/main/java/org/antlr/jetbrains/eo/parser/EOParser.java
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|