ANTLR4 DefaultErrorStrategy fails to inject missing token -
i'm trying run in testrig following grammar:
grammar cobolfragment; // hidden tokens ws : [ ]+ -> channel(hidden); nl : '\n' -> channel(hidden); // keywords period : '.'; division : 'division'; section : 'section'; data : 'data'; working_storage : 'working-storage'; file : 'file'; fd : 'fd'; external : 'external'; global : 'global'; block : 'block'; contains : 'contains'; characters : 'characters'; // data integer : [0-9]+; id : [a-z][a-z0-9]*; datadivision : data division period filesection? workingstoragesection? ; filesection : file section period filedescription* ; filedescription : fd filename=id // (is? global)? // 1. global clause // (is? external)? // 2. external clause blockclause? period ; blockclause : block contains? blocksize=integer characters ; workingstoragesection : working_storage section period ; with following input:
data division. file section. fd fd01 working-storage section. clearly third line of input ("fd fd01") missing terminator period asked in filedescription rule.
the defaulterrorstrategy correctly acknowledges , conjures missing token:

on stderr correct report displayed: line 4:0 missing '.' @ 'working-storage'.
but if fragments commented out enabled (that is, clauses 'is external' , 'is global' brought in grammar again), single token insertion fails:

on stderr misleading report displayed: line 4:0 no viable alternative @ input 'working-storage'
how enable full grammar (with external , global clauses) retaining ability correct missing period?
side note 1: if enable either external or global, not both clauses, defaulterrorstrategy works nicely , injects missing token.
side note 2: code generated grammar both clauses enabled has following code (compared grammar 1 of them enabled):
public final filedescriptioncontext filedescription() ... { ... try { ... switch ( getinterpreter().adaptivepredict(_input,4,_ctx) ) { case 1: { setstate(31); _la = _input.la(1); if (_la==is) { { setstate(30); match(is); } } setstate(33); match(global); } break; } ... } catch (recognitionexception re) { ... and adaptivepredict() call culprit, because throws no viable alternative @ input 'working-storage' before parser has chance match(period) (in generated code, not pasted here).
i've managed solve adding new clause both clauses:
(here fragments changed)
... filedescription : fd filename=id isclauses? blockclause? period ; isclauses : is? global (is? external)? | is? external ; ... now defaulterrorstrategy work , injects missing period.
why not isclauses : (is? global?) (is? external)?; well, tried first, of course. got warning (warning(154): rule 'filedescription' contains optional block @ least 1 alternative can match empty string) , no missing period injected.
Comments
Post a Comment