[antlr-interest] Handling optional spaces

Justin Crites jcrites at gmail.com
Sun Oct 7 20:08:22 PDT 2007


This is the full grammar that fails to parse "a" (MismatchedTokenException):

expr     :    OptSpace ID OptSpace;
ID  :   ('a'..'z'|'A'..'Z')+ ;
OptSpace :    ' '*;

This is the full grammar that succeeds:

expr     :    ' '* ID ' '*;
ID  :   ('a'..'z'|'A'..'Z')+ ;

 These grammars are identical except that in the latter I have replaced
OptSpace with its definition in the rule "expr".

In my mind these grammars should behave identically -- I would expect the
grammar specification to follow a "substitution rule" such that if I have a
rule A : X; then I can replace instances of "A" in other rules with simply
"X" and get identical behavior.  However, even though OptSpace : ' '*; the
rule

expr : OptSpace ID OptSpace

 behaves differently than:

expr : ' '* ID ' '*;  // substituting ' '* for OptSpace

Does this clarify my question?  Thank you.

-- 
Justin Crites
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071007/aba58093/attachment.html 


More information about the antlr-interest mailing list