[antlr-interest] Interesting problem with ANTLR and CSS 2.1 (tokens added to AST rewrites almost works)

Simon Janes simon.janes at gmail.com
Fri Oct 12 17:21:51 PDT 2007


Thanks dave!

That almost works-- now I no longer get the "more than one node as
root" error, but only receive one child in the list.  I got this by
changing the grammar rules here and introducing some "semantic"
tokens:

----
parse	:	stylesheet* -> ^(STYLESHEET stylesheet);

stylesheet
	: comment_stylesheet -> ^(COMMENT comment_stylesheet)
	| ruleset -> ^(RULESET ruleset)
	| media -> ^(MEDIA media)
	| page -> ^(PAGE page)
	;
----

The CSS for this run:

/* hello comment */
.hello { color: blue; font-face: Arial; }
.again { color: blue; }

The getStringTree() dump:

(STYLESHEET (COMMENT /* hello comment */))

Is there something I need to do tell it to append to a node?


> On Fri, Oct 12, 2007 at 04:23:04PM -0400, Simon Janes wrote:
> > parse    :    stylesheet -> ^(stylesheet); /* I think this sets the "root"
> > of the AST. */
>
> > stylesheet
> >     : (comment_stylesheet|ruleset|media|page)* ;
>
> > I'll get a runtime error:
> >
> > "more than one node as root"
>
> I've not seen that before, but I assume the problem is that..
>
>   -> ^(stylesheet)
>
> ..attempts to make the AST referenced by 'stylesheet' be the root node of
> the result AST, but in your case 'stylesheet' does not represent a
> single node, but a list of nodes.
>
> Probably you should just remove the '^'..
>
>   parse    :    stylesheet -> stylesheet;
>
> ..but then you might as well just remove the rewrite, because it doesn't
> add anything..
>
>   parse    :    stylesheet;
>
>
> Alternatively, you could add an 'imaginary' node to act as the root, if
> you really want a single node result rather than a list..
>
>   parse    :    stylesheet -> ^(STYLESHEET stylesheet);
>
>
> Note that for the standard ANTLR tree implementation, a 'list' of nodes
> is really just a special case node:
>
>   http://www.antlr.org/api/Java/classorg_1_1antlr_1_1runtime_1_1tree_1_1_common_tree.html#670edeb282b219bc714ed9490aa5a728
>
>
> ta,
> dave
>


More information about the antlr-interest mailing list