[antlr-interest] Case insensitive reserved words.
Jim Idle
jimi at temporal-wave.com
Fri Mar 20 09:48:44 PDT 2009
Peter C. Chapin wrote:
> Hi again...
>
> My new project entails building a parser for a language with case
> insensitive reserved words. I see the page here:
>
> http://www.antlr.org/wiki/pages/viewpage.action?pageId=1782
>
> describing how to write a custom look-ahead to deal with this. However,
> David Haubenstricker commented on that page with an alternative approach:
>
> SELECT : S E L E C T
>
> fragment A: ('A'|'a')
> fragment B: ('B'|'b')
> fragment C: ('C'|'c')
> ... etc.
>
> To be honest, David's approach looks pretty good to me. I'm wondering if
> there are any disadvantages to it that I should know about.
>
Depends how many keywords you have, how long they are, and whether you
can be bother to write a short program to generate those rules from a
keyword list ;-), but the main way of extending the input stream to
provide case insensitive comparison vi a custom LA() is generally less
work and more efficient, as the characters of the keyword will be
assumed to be all UPPER (or lower, not sure why the C# example uses
lower, but whatever ;-) and so only one comparison per character is
required.
Jim
More information about the antlr-interest
mailing list