NullData
NullData

Reputation: 414

Antlr 3 can't deal with syntactic predicate

This problem has occurred to me many times. I do not completely understand the problem fully. My syntax looks like this:

grammar Syntax;

options {
  language = Java;
  backtrack = true;
}

rule: ('syntax' (INTEGER | HEX) ';')? (structure | packet)+;

structure: ('struct' | 'structure')? field;

packet: 'packet'? NAME '{' field+ '}';

field: NAME (':' | '=' | ':=' | '->')? value ';';

value: (TYPE (MULT (INTEGER | HEX))?);


MULT: 'x' | 'X';

INTEGER: ('0'..'9')+;

HEX: '0x' ('0'..'9' | 'A'..'F' | 'a'..'f');

TYPE: ('unsigned'? 'byte' | 'short' | 'int16' | 'int8')
    | 'int' | 'int32' 
    | 'long' | 'int64'
    | 'char' | 'char8' | 'char16' | 'unicode'
    | 'utf' | 'utf8' | 'utf16';

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

WHITESPACE: ( ' ' | '\n' | '\r' | '\f' | '\t')+{$channel = HIDDEN;} ;

The test-case looks like this:

syntax 1;
packet a
{
    a int;
    b byte;
    d byte x 10;
    c int x b;
}

Now the error is:

errors

Upvotes: 0

Views: 897

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170278

The interpreter of ANTLRWorks (which ANTLR Eclipse IDE also uses) cannot cope with predicates or embedded (Java) code. Enabling global backtracking1 (which should be avoided, if possible!) causes all parser rules to get predicates in front of them (hence the error "can't deal with predicates yet").

Besides, the interpreter is rather buggy: I don't recommend using it. Use ANTLRWorks' debugger, it's great and will also compile and run any embedded (Java) code in your grammar. I believe the ANTLR Eclipse IDE also has this debugger from ANTLRWorks.

1 http://www.antlr.org/wiki/display/ANTLR3/How+to+remove+global+backtracking+from+your+grammar

Upvotes: 3

Related Questions