[antlr-interest] Handling optional spaces

Justin Crites jcrites at gmail.com
Sun Oct 7 18:46:51 PDT 2007


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071007/24ecb5dc/attachment.html 


More information about the antlr-interest mailing list