[antlr-interest] Is this a bug in Antlr?
Duncan Booth
duncan at rcp.co.uk
Thu Aug 7 06:26:32 PDT 2003
I have a problem parsing a grammar, and it looks to me as though it
may be a bug in Antlr, but then again maybe I'm just missing
something obvious. I've cut the problem down to a (nearly) minimal
grammar attached below.
Compile and run using:
java antlr.Tool grammar.g
csc GrammarTokenTypes.cs Grammar.cs /R:antlr.runtime.dll
Grammar.exe
The output from the program is:
line 0:0: unexpected token [")",<5>,line=0,col=0]
The output I would expect from the program is:
( a )
I can get the expected output in a number of ways, e.g. removing
either branch of the (unused) 'list_iter' rule, using the commented
out line under the 'factor' rule instead of the current line. Any of
these minor changes make the parser work the way I expect, but with
the grammar as given below, the parser seems determined that an
'expr' can never be followed by a ')'.
Any suggestions how to get this to parse the way I expect without
breaking the grammar?
---- begin Grammar.g ----
header
{
using antlr;
}
options
{
language = "CSharp";
}
class Grammar extends Parser;
options
{
k = 2; // two token lookahead
buildAST = true;
defaultErrorHandler = false; // Don't generate parser error
handlers
}
tokens
{
LPAR = "(";
RPAR = ")";
}
{
class DummyTokenStream : TokenStream
{
protected Token[] tokens;
protected int index;
public DummyTokenStream(Token[] tokens)
{
this.tokens = tokens;
index = 0;
}
public Token nextToken()
{
if (index < tokens.Length)
return tokens[index++];
return new CommonToken(EOF, "");
}
}
public static void Main(string[] args) //throws Exception
{
Token[] tokens = new Token[] {
new CommonToken(LPAR, "("),
new CommonToken(NAME, "a"),
new CommonToken(RPAR, ")")
};
try
{
TokenStream s = new DummyTokenStream(tokens);
Grammar parser = new Grammar(s);
parser.expr();
CommonAST t = (CommonAST)parser.getAST();
Console.WriteLine(t.ToStringList());
} catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
expr:
term (("+" | "-") term)* ;
term:
factor (("*"| "/") factor)* ;
factor:
atom (trailer)?;
// atom;
atom:
"(" (expr)? ")"
| NAME | NUMBER ;
trailer:
"(" (expr)? ")" ;
list_iter:
list_for |
list_if ;
list_for:
"for" expr "in" expr (list_iter)? ;
list_if:
"if" expr (list_iter)? ;
---- end Grammar.g ------
Duncan Booth
duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-
p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
http://dales.rmplc.co.uk/Duncan
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list