0

I am using antlr 2.7.6. I am programming a parser for plc 61131-3 ST language and I can't resolve an issue with my grammar.

The grammar is:

case_Stmt      : 'CASE' expression 'OF' case_Selection + ( 'ELSE' stmt_List )? 'END_CASE';
case_Selection : case_List ':' stmt_List;
case_List      : case_List_Elem ( ',' case_List_Elem )*; 
case_List_Elem : subrange | constant_Expr; 
constant_Expr  : constant | enum_Value; 
stmt_List      : ( Stmt ? ';' )*; 
stmt           : assign_Stmt | subprog_Ctrl_Stmt | selection_Stmt | Iteration_Stmt; 
assign_Stmt    : ( variable ':=' expression ) 
enum_Value     : ( identifier '#' )? identifier;
variable       : identifier | ...

The problem occurs with "enum_Value" as "case_Selection", the parser interprets it as a new "stmt" instead of the new "Case_Selection" it was supposed to. Example:

CASE (enumVariable) OF
    enum#literal1: Variable1 := 1;      
    enum#liteal2:  Variable1 := 2;
    enum#liteal3:  Variable1 := 3;
ELSE 
    Variable1 := 4;
END_CASE;

In the above example instead of taking " enum.liteal2" as the new "case_Selection" it interprets it as "assign_Stmt" and gives error because it doesn't found the ':='. Is there a way to try to read the maximum of characthers till we find the ':' or the ':=' to understand if we realy have a new "stmt" or not?

Thank you! Edit1: better syntax;

mfabruno
  • 131
  • 3
  • 12
  • Is there a particular reason why you're still using ANTLR2? It is rather old: if you can, switch to ANTLR4. – Bart Kiers Sep 12 '16 at 11:20
  • legacy... This would be simpler to resolve in antlr4? i am using lookhead k=3 btw – mfabruno Sep 12 '16 at 11:24
  • Mainly because there are (probably) few people who can help you with v2. Anyway, shouldn't your parser rules start with a lower case? Upper case makes them tokens, right? At least, that is how v3 and v4 work. – Bart Kiers Sep 12 '16 at 11:38
  • sure, but for the problem it doesn't matter.. I have a working CASE with constants, the enums because of the parser thinks they are the begining of one assign it follows the wrong path.. – mfabruno Sep 12 '16 at 11:49
  • 1
    k = 3 might not be enough actually, depending on the complexity of your statement rule and it's subrules. Both, case_Selection and stmt can begin with an identifier, which is probably the root of your ambiquity. ANTLR4 uses a totally different approach for prediction and probably can solve this automatically. I'd rather give that a try. Otherwise you can only try to shuffle around the rules to give ANTLR a hint what to pick first or use predicates. You can also simplify your rules to a point where it works as expected and then re-add the parts to see what causes trouble. – Mike Lischke Sep 14 '16 at 06:59

0 Answers0