[antlr-interest] White space
Alex Rodriguez
arodmail at gmail.com
Wed Sep 1 13:54:08 PDT 2010
John,
Yes. And it is infinitely better this way. Thank you, sir.
- Alex :)
On Wed, Sep 1, 2010 at 1:19 PM, John B. Brodie <jbb at acm.org> wrote:
> Greetings!
>
> On Wed, 2010-09-01 at 11:30 -0700, Alex Rodriguez wrote:
>> Greetings,
>>
>> Given a very simple grammar for a language that only has an 'if'
>> statement, I would like to be able to parse white space within literal
>> values. So far, this works (case 1):
>>
>> if(value=='white space'){doThis('arg')}
>>
>> But this doesn't work (case 2):
>>
>> if (value == 'white space') { doThis('arg') }
>
> because you have permitted blanks in an ID, the string "if " above is an
> ID under your rules and *NOT* the keyword 'if' followed by a blank.
>
>>
>> Note that case 2 is spaced for readability.
>>
>> Debugging case 2 in ANTLRWorks produces a MismatchedTokenException.
>>
>> What is the best way to handle both cases? Here is the grammar:
>
> move your literalValue rule into the lexer and take out the blank from
> ID.
>
> see attached for the way i would change your grammar to solve this
> issue.
>
> hope this helps...
> -jbb
>
>
>>
>> grammar Lang;
>>
>> statement
>> :
>> 'if' LPAREN ID EQ literalValue RPAREN '{' action '}'
>> ;
>>
>> literalValue
>> :
>> '\'' ID '\''
>> ;
>>
>> action
>> :
>> ID LPAREN literalValue RPAREN
>> ;
>>
>> ID
>> :
>> ('a'..'z' | 'A'..'Z' | '0'..'9' | '@' | ':' | '_' | ' ' | '+')+
>> ;
>>
>> LPAREN
>> :
>> '('
>> ;
>>
>> RPAREN
>> :
>> ')'
>> ;
>>
>> EQ
>> :
>> '=='
>> ;
>>
>> WS
>> :
>> (' ' |'\t' |'\r' |'\n' )+ { $channel=HIDDEN; }
>> ;
>
>
More information about the antlr-interest
mailing list