[antlr-interest] about space between operator
Bart Kiers
bkiers at gmail.com
Mon Dec 5 04:25:06 PST 2011
Ah, yeah, I forgot a `( ... )?` somewhere, sorry.
This works:
grammar AA;
@parser::members {
public static void main(String[] args) throws Exception {
String src = "1.1<>1.2; 1.3<1.4; 1.5 <> 1.6; 1.7 < 1.8;";
AALexer lexer = new AALexer(new ANTLRStringStream(src));
AAParser parser = new AAParser(new CommonTokenStream(lexer));
parser.parse();
}
}
parse
: (expression ';' {System.out.println("parsed: " + $expression.text);})+
EOF
;
expression
: TYPE_DOUBLE (NOTEQUALS | LT) TYPE_DOUBLE
;
NOTEQUALS
: '<>'
;
LT
: '<' (' '+ ('>' {$type=NOTEQUALS;})?)?
;
TYPE_DOUBLE
: Digit+ '.' Digit*
| '.' Digit+
;
WS
: (' '|'\r'|'\t'|'\u000C'|'\n')+ {$channel=HIDDEN;}
;
fragment Digit : '0'..'9';
Regards,
Bart.
On Mon, Dec 5, 2011 at 12:55 PM, 李志鹏 <lizhipeng at gmail.com> wrote:
> thank you
>
> but not work.....
>
>
> On Monday, December 5, 2011, Bart Kiers wrote:
>
>> Try this:
>>
>> NOTEQUALS
>> : '<>'
>> ;
>>
>> LT
>> : '<' (' '+ '>' {$type=NOTEQUALS;})?
>> ;
>>
>> Regards,
>>
>> Bart.
>>
>>
>> On Mon, Dec 5, 2011 at 8:14 AM, 李志鹏 <lizhipeng at gmail.com> wrote:
>>
>>> hello all
>>>
>>> I have a very simple grammar like this:
>>> grammar AA;
>>>
>>> expression
>>> : TYPE_DOUBLE (NOTEQUALS | LT) TYPE_DOUBLE EOF
>>> ;
>>>
>>> NOTEQUALS
>>> : '<>' | '<' (' '+) '>'
>>> ;
>>>
>>> LT : '<';
>>>
>>> TYPE_DOUBLE
>>> : Digit+ '.' Digit*
>>> | '.' Digit+
>>> ;
>>>
>>> fragment
>>> Digit
>>> : '0'..'9'
>>> ;
>>> WS
>>> : (' '|'\r'|'\t'|'\u000C'|'\n')+ {$channel=HIDDEN;}
>>> ;
>>>
>>> if input "1.6<>1.7" or "1.6<1.7" or "1.6 <> 1.7", it is ok
>>> but input "1.6 < 1.7"(has white space on both sides '<' ), the grammar
>>> can't resolve it
>>>
>>> if I change as this
>>> NOTEQUALS
>>> : '<>'
>>> ;
>>> above all are ok, but "1.6 < > 1.7" is not work, I wan't use "<>" to
>>> mean
>>> not equal, and white space is allow between "< >", How Can i do?
>>>
>>> thank you all
>>>
>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>> Unsubscribe:
>>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>>
>>
>>
More information about the antlr-interest
mailing list