質問

Consider the following simple grammar.

grammar test;

options {  
  language = Java; 
  output = AST;
}                     
//imaginary tokens
tokens{ 
}


parse
    : declaration
    ;

declaration
    : forall
    ;
forall
    :'forall' '('rule1')' '[' (( '(' rule2 ')' '|' )* ) ']' 
    ;
rule1
    : INT
    ;
rule2
    : ID
    ;
ID  
    : ('a'..'z' | 'A'..'Z'|'_')('a'..'z' | 'A'..'Z'|'0'..'9'|'_')*
    ;
INT 
    : ('0'..'9')+
    ;
WHITESPACE
    : ('\t' | ' ' | '\r' | '\n' | '\u000C')+ {$channel = HIDDEN;}
    ;

and here is the input

forall (1) [(first) | (second) | (third) | (fourth) | (fifth) |]  

The grammar works fine for the above input but I want to get rid of the extra pipe symbol (2nd last character in the input) from the input.
Any thoughts/ideas?

役に立ちましたか?

解決

My antlr syntax is a bit rusty but you should try something like this:

forall
    :'forall' '('rule1')' '[' ('(' rule2 ')' ('|' '(' rule2 ')' )* )? ']' 
    ;

That is, instead of (r|)* write (r(|r)*)?. You can see how the latter allows for zero, one or many rules with pipes inbetween.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top