简体   繁体   中英

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

The interpreter of ANTLRWorks (which ANTLR Eclipse IDE also uses) cannot cope with predicates or embedded (Java) code. Enabling global backtracking 1 (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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM