[antlr-interest] Tree rewriting (filter=true, output=ast)

Sam Barnett-Cormack s.barnett-cormack at lancaster.ac.uk
Sat Mar 21 09:16:53 PDT 2009


So, I'm trying to deal with some of the parse ambiguities in my grammar
lazily - accept lots of stuff in the parser, and do some tree rewriting
to fix things (and find some errors).

What I want to do is look at all VLIST trees, determine which of the
following cases they fit:

1) All children are either ^(NAMENUM ...), ^(EXTREF ...), LCID or NUMBER
2) Children are *anything* except ^(NAMENUM ...)
3) Children include ^(NAMENUM ...) but are not limited to those
mentioned in (1) (ie. the complement of the union of (1) and (2)).

In case (2), the tree should be passed. In case (1), the VLIST tree
should become an OBJID tree, but with the same children. In case (3), an
error should be generated (not sure of my error-handling strategy yet,
probably error nodes in the tree).

My attempt, so far, is:

tree grammar ASN_1_tree_pass1;

options {
	language = Java;
	output = AST;
	tokenVocab=ASN_1;
	ASTLabelType=CommonTree;
	filter=true;
}

valueListValue
  : ^(VLIST
    (
      (~(NAMENUM))*
      | v+=(nameAndNumberForm | LCID | NUMBER | externalValueReference)+
        -> ^(OBJID $v+)
    ))
  ;

nameAndNumberForm : ^(NAMENUM LCID NUMBER) ;

externalValueReference : ^(EXTVREF CAPID LCID) ;

I get "input such as ... can match multiple alternatives" with regard to
the subrule in valueListValue - the "such as" refers to an awful lot of
tokens. Of course, this is trying to match a NAMENUM node, not a
subtree, so it'd probably break horribly, but it's the nearest I can get
and still only get a warning. Using nameAndNumberForm in place of
NAMENUM gives syntax errors. If I understand how the trees work
correctly, just matching NAMENUM won't work, because it's okay in that
case for NAMENUM to appear *somewhere* under VLIST, just not as an
immediate child.

The work so far doesn't do anything for case (3).

So, can anyone point me in the right direct (including a wiki page that
will actually help - I did some searching, but nothing seems to address
my particular problem)? I'd appreciate it.

Sam


More information about the antlr-interest mailing list