[antlr-interest] Using ANTLR's ~ to do match literals causes "no viable alternative" messages in generated Java but not in ANTLRWorks
Davy Landman
davy.landman at gmail.com
Fri Feb 26 00:36:15 PST 2010
Hi Ron,
Thanks, I've been applying the channel=HIDDEN on the WS_CHAR fragment
level, and that messed up everything, but using this extra token works
perfectly.
Cheers,
Davy
On Fri, Feb 26, 2010 at 01:54, Ron Hunter-Duvar
<ron.hunter-duvar at oracle.com> wrote:
> Hi Davy,
>
> You don't have a rule for consuming whitespace. Add something like this:
>
> WS :
> WS_CHAR+ {$channel=HIDDEN;}
> ;
>
> fragment WS_CHAR :
> (' '|'\r'|'\t'|'\u000C'|'\n')
> ;
>
> Ron
>
>
> Davy Landman wrote:
>>
>> Hello,
>>
>> I'm having some problems with my generated java parser/lexer, but I've
>> tried to reduce the problem to a small subset.
>>
>> Let's assume the simple language which has identifiers and numbers.
>> And the only rules are, a number contains out of only numbers, and a
>> identifier can not contain `'?', ':', ' '` and can not begin with a
>> number.
>>
>> I would create the following ANTLR specification of this language.
>>
>> grammar simple;
>>
>> prog: expr* EOF;
>>
>> expr
>> : ID | INT;
>>
>>
>> ID : ~('0'..'9'|SEPERATORS) ~(SEPERATORS)*;
>>
>> fragment
>> SEPERATORS
>> : ' ' | ':' | '?';
>>
>> INT : '0'..'9'+
>> ;
>>
>> And running this in ANTLRWorks debugging and parsing mode, no errors
>> are reported to the console.
>>
>> But if I than generate the Parser and Lexer and try to consume it in a
>> java program as such:
>>
>> import org.antlr.runtime.ANTLRStringStream;
>> import org.antlr.runtime.CommonTokenStream;
>> import org.antlr.runtime.RecognitionException;
>>
>>
>> public class runner {
>>
>> public static void main(String args[])
>> {
>> simpleLexer lex = new simpleLexer(new
>> ANTLRStringStream("a33 44"));
>> CommonTokenStream tokens= new CommonTokenStream(lex);
>> simpleParser parser = new simpleParser(tokens);
>> try {
>> parser.prog();
>> } catch (RecognitionException e) {
>> e.printStackTrace();
>> }
>> }
>> }
>>
>> The parser works, and the prog() continues succesfully. But in the
>> background the following error message is printed to the console.
>>
>> line 1:3 no viable alternative at character ' '
>>
>> The same exact sequence causes no messages to the ANTLRWorks console,
>> so I was wondering, am I abusing the ~ ? Or is there a difference
>> between the ANTLRWorks debugger and default java runtime?
>>
>> So if anybody can help me how to get rid of these messages? Because in
>> the original lexer and parser where this question is based, I get a
>> nice looking AST but only have messages printed to the eclipse
>> console.
>>
>> Versions used:
>> ANTLRWorks 1.3.1
>> ANTLR-3.2
>> Linux x64
>>
>> Kind regards,
>> Davy Landman
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>>
>
> --
> Ron Hunter-Duvar | Software Developer V | 403-272-6580
> Oracle Service Engineering
> Gulf Canada Square 401 - 9th Avenue S.W., Calgary, AB, Canada T2P 3C5
>
> All opinions expressed here are mine, and do not necessarily represent
> those of my employer.
>
>
More information about the antlr-interest
mailing list