[antlr-interest] Again Cobol:

Mark Taylor mttdgf at gmail.com
Thu Jun 11 05:38:38 PDT 2009


I'm working on a Cobol grammar (oh, the foolishness of youth...  wait I'm
not THAT young...) and I need some advice about the ambiguities.  In
particular I'm getting the famous: "error(211): CobolTest.g:11:30: [fatal]
rule if has non-LL(*) decision due to recursive rule invocations reachable
from alts 1,2.  Resolve by left-factoring or using syntactic predicates or
using backtrack=true option."  Yes, this has come up before, but there was
no clear answer.  This time I have a specific example (see below).

Below is the smallest grammar which exhibits the problem.  You can see I
have stmt+ in both the IF rule and the PERFORM rule.  The problem is the
'END-IF'?.  Since END-IF is optional in Cobol, there is no clear scope
terminator.   I've tried left refactoring the (stmt+ ....) into a separate
rule for both IF and PERFORM but that doesn't seem to work either.  I don't
see how a syntactic predicate could be applied to this either.

If I were writing this as a recursive descent parser by hand (I'm trying
Antlr so I don't have to do this) I would write a statementlist() method
that would simply loop on all statement beginnings keywords.  Then when an
END-IF, ELSE, END-PERFORM, or some other arbitrary scope terminator appeared
in the input the loop would simple exit and return the list of valid
statements.  The question is: how to get Antlr to behave like this?

Any advice is appreciated.

<pre>

grammar CobolTest;

program:     sentence+ EOF;

sentence:    stmt+ '.' ;

stmt:        if
        |    move
        |   perform ;

if:            'if' condition 'then'? stmt+ ('else' stmt+)? 'end-if'? ;

move:        'move' ID 'to' ID ;

perform:     'perform' stmt+ 'end-perform' ;

condition:     ID '==' ID ;

ID  :       ('a'..'z'|'A'..'Z')+ ;
INT :       '0'..'9'+ ;
NEWLINE:    '\r'? '\n' {skip();} ;
WS  :       (' '|'\t')+ {skip();} ;

</pre>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090611/fe77301b/attachment.html 


More information about the antlr-interest mailing list