[antlr-interest] Handling optional spaces

Austin Hastings Austin_Hastings at Yahoo.com
Sun Oct 7 19:24:10 PDT 2007


You didn't include the full grammar, so it's hard to know what's going 
on. I'd bet it has something to do with how you defined ID, and whether 
you have a WHITESPACE or WS rule.

=Austin

Justin Crites wrote:
> Hi there,
>
> I am just getting started using ANTLR for a toy language I'm 
> developing.  I am trying to understand, why do these two grammars 
> behave differently?
>
> 1)
>
> expr     :    OptSpace ID OptSpace;
>
> OptSpace :    ' '*;
>
> 2)
>
> expr     :    ' '* ID ' '*;
>
> In grammar #1, the string "x" does not match, and I get mismatched 
> token exceptions.  It looks like ANTLR is trying to match OptSpace and 
> failing.  This confuses me because OptSpace's body is just ' '* which 
> should be able to match nothing, I thought.
>
> Why do these two grammars behave differently?  Does OptSpace always 
> have to match something, even if its body is potentially empty?
>
> More generally, how can I find out about and control ANTLR's handling 
> of whitespace?  I observed the behavior that strings with spaces in 
> them can be parsed even if space is not in the language definition 
> anywhere.  For example, if I have a rule like:
>
> expr : ID+;
>
> It will match input like "a b", even though technically " " should not 
> be matched by anything.  How can I make ANTLR _not_ accept strings 
> like this?  Would I have to write all my rules like this?
>
> expr : ID | (ID Space expr);
>
> Thanks for your help.
>
> -- 
> Justin Crites
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition. 
> Version: 7.5.488 / Virus Database: 269.14.4/1055 - Release Date: 10/7/2007 10:24 AM
>   



More information about the antlr-interest mailing list