[antlr-interest] Several questions

Daniel Plaisted dsplaisted at gmail.com
Mon Jan 1 08:58:21 PST 2007

I am working on a source-to-source compiler using ANTLR 3 and
ANTLRWorks.  I have a few questions.

1. Case insensitivity - I need my grammar to be case insensitive.
There was a Wiki FAQ entry that linked to this thread:

I couldn't find any case insensitive lexer class in the ANTLR source
code, so it appears I will need to write my own.  In addition, I can't
find a way to specify from inside my grammar that the lexer should
derive from a different class than "Lexer".  Is there a way to do

2. C# runtime - I can use Java if need be, but C# would be
preferrable.  I can set the language=CSharp option and get C# output.
However, I can't find the runtime libraries for C#.  Do they exist at
this point?  If I remember correctly, db4o uses a Java to C# compiler
to support both Java and .NET, so maybe the same type of thing could
be used to generate the C# runtime files.

3. If Then Else ambiguity - I have a rule which looks like:
ifStat : 'if' expr 'then' stat ('else' stat)?;
where stat is the nonterminal for a statement, which could be an if
statement also.  This grammar is ambiguous, because with nested if
statements such as "if a=1 then if b=2 then c=3 else c=4" it is not
clear which if the else belongs to.  ANTLR gives the following
[15:49:08] warning(200): EasyLanguage.g:111:30: Decision can match
input such as "'else'" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input

My compiler book suggests a solution such as the following to remove
the ambiguity:

ifStat : 'if' expr 'then' ifWithElse 'else' stat
    | 'if' expr 'then' stat

ifWithElse : 'if' expr 'then' ifWithElse 'else' ifWithElse
    | nonIfStat

This resolves the ambiguity in the grammar, but makes parsing the
grammar harder.  I get this error:
[15:58:51] error(211): EasyLanguage.g:109:21: [fatal] rule ifStat has
non-LL(*) decision due to recursive rule invocations reachable from
alts 1,2.  Resolve by left-factoring or using syntactic predicates or
using backtrack=true option.

If I turn on backtracking, it works.  However, I would prefer to be
able to parse the grammar without backtracking.

Is there a solution for this problem that avoids both backtracking and


More information about the antlr-interest mailing list