[antlr-interest] Something in my little grammar throws an Unable to cast CommonTree to type GrammarAST
Arturo Hernandez
arthernan at hotmail.com
Wed Jun 8 07:20:42 PDT 2011
This is a modified version of the xml example from the ANTLR website. Eventually I want to extract data from XHTML.
I probably just need a second to spot a simple mistake. I have the reference book, not finished it yet, but did spend plenty of time on this and I am stuck.
error(100): XMLParser.g:29:11: syntax error: antlr: NoViableAltException(72@[475:4: ( ( id ( ASSIGN | PLUS_ASSIGN ) ( atom | block ) ) (sub= ebnfSuffix[root_0,false] )? |a= atom (sub2= ebnfSuffix[$a.tree,false] )? | ebnf | FORCED_ACTION | ACTION |p= SEMPRED ( IMPLIES )? |t3= tree_ )])error(100): XMLParser.g:29:11: syntax error: antlr: NoViableAltException(72@[475:4: ( ( id ( ASSIGN | PLUS_ASSIGN ) ( atom | block ) ) (sub= ebnfSuffix[root_0,false] )? |a= atom (sub2= ebnfSuffix[$a.tree,false] )? | ebnf | FORCED_ACTION | ACTION |p= SEMPRED ( IMPLIES )? |t3= tree_ )])error(100): XMLParser.g:0:1: syntax error: assign.types: MismatchedTreeNodeException(0!=32)error(100): XMLParser.g:0:1: syntax error: assign.types: MismatchedTreeNodeException(3!=33)error(100): XMLParser.g:0:1: syntax error: assign.types: MismatchedTreeNodeException(3!=34)error(10): internal error: XMLParser.g : System.InvalidCastException: Unable to cast object of type 'Antlr.Runtime.Tree.CommonTree' to type 'Antlr3.Tool.GrammarAST'.
parser grammar XMLParser;options{ language=CSharp3; tokenVocab=XMLLexer;}
@header {using System;}@namespace { XMLParserN }
document : element ;
element : startTag (element | PCDATA )* endTag | emptyElement ;
startTag : TAG_START_OPEN GENERIC_ID {Console.Write(@"<" + $GENERIC_ID.text); } (attribute {if $attribute.cl!=@"" then Console.Write(@" class=\"" + $attribute.cl + @"\"");} )* TAG_CLOSE {Console.Write(@">" + $GENERIC_ID.text); }
attribute returns [string cl] : GENERIC_ID ATTR_EQ ATTR_VALUE { if ($GENERIC_ID.text==@"class") $cl = $ATTR_VALUE.text else $cl = @""; } ;
endTag : TAG_END_OPEN GENERIC_ID TAG_CLOSE {Console.Write(@"</" + $GENERIC_ID.text + @">"); };
emptyElement : TAG_START_OPEN GENERIC_ID (attribute)* TAG_EMPTY_CLOSE ;
More information about the antlr-interest
mailing list