ANTLR equivalent of Bison REJECT action?

I am trying to parse a list of pairs Name=Value

where the value can contain anything but whitespace (that is, the values ​​can contain the same characters).
The name is limited to common identification characters.

The problem is that the "Value" token matches everyone. For example, to enter:

dude=sweet

      

the parser will match an integer input with a "Value" marker (and throw a MismatchedTokenException

).

In bison, it was possible to assign states to tokens (or was it just for non-terminals?) So that they would become "fit" for matching after an explicit transition to that state.

EDIT Thinking about it, this won't work in bison either - the token split has already happened (in flex); however, I think there is a way to use tags , forcing flex to try the second match. REJECT

Here is my ANTLR grammar.

grammar command_string;

start   
    :    commandParam* EOF
    ;
commandParam 
    :   IDENTIFIER '=' CONTINUOUS_VALUE 
    ;
IDENTIFIER 
    :   ('-'|'_'|'a'..'z'|'A'..'Z'|'0'..'9')+ 
    ;
CONTINUOUS_VALUE
    :   ~( ALL_WS )+
    ;
WS
    :   (ALL_WS) +      { $channel = HIDDEN; }
    ;
fragment ALL_WS     
    :   ' ' | '\t' | '\r' | '\n' 
    ;

      

0


a source to share


1 answer


You have some overlap between CONTINUOUS_VALUE and IDENTIFIER (characters in IDENTIFIER are a subset of CONTINUOUS_VALUE. There might be several ways to solve this problem. One way is to run CONTINUOUS_VALUE with "=" and then remove it from the text. In CSharp this would look like So:

CONTINUOUS_VALUE
    :   '=' ~( ALL_WS )+ { Text = Text.Substring(1, Text.Length - 1); }
    ;

      

Then just take the '=' from the commandParam rule.



The second way is to make the parser rules IDENTIFIER and CONTINUOUS_VALUE (lowercase at least the first letter), then you have the context to figure out which one should match. You might also be able to make them fragments and reference them in commandParam, but I'm not sure if you can insert fragments or not, since you already have ALL_WS fragment.

Also, don't you need some sort of separator between the NameValue pairs?

+1


a source







All Articles