Hi all. Yep, I put ^ and ! suffix operators into ANTLR in about '93. Makes building ASTs really easy for things like expressions. Note that turning on AST construction with no operators builds a linked list (flat tree) of entire input; very convenient.
Here's an example. This builds proper ASTs for additive expressions: add : atom ('+'^ atom)* ; For non-expressions, i like my rewrite syntax: ifstat : 'if' '(' expr ')' s1=stat ('else' s2=stat)? -> ^('if' expr s1 s2?) ; Grammar to tree grammar rewrite. Terence On Jun 14, 2009, at 12:00 PM, David-Sarah Hopwood wrote: > Peter Goodman wrote: >> I wanted to mention one of the way in which I have been building an >> AST from >> my parse tree while the tree itself is being constructed (a more >> appropriate >> term might be: reduced parse tree). I use one rule within the >> parser and also >> two operators within the grammar. One of these operators is already >> well- >> understood and has been mentioned on this list several times. >> >> My two operators are: >> >> i) A non-excludable operator (-), which requires that a non/ >> terminal appear >> in the reduced parse tree. If no such operator is used, then >> terminals are >> *not* added to the tree as leafs, and non-terminals, where only one >> child >> exists, are replaced with their child (my automatic reduction rule). > > ANTLR (v3) has a '!' operator which excludes a child from appearing > in the > reduced tree (i.e. the opposite of your '-'). It has a similar rule to > yours for automatically reducing nodes with only one child. I think > that > I prefer the approach of including children by default; it means > that the > default at least includes all of the necessary information. > >> ii) A raise children operator (^), which puts the derivations of a >> particular >> non-terminal in a production in place of the non-terminal. This >> allows me to >> collect lists of nodes. > > This is also similar to ANTLR. > > -- > David-Sarah Hopwood ⚥ http://davidsarah.livejournal.com > > > > > _______________________________________________ > PEG mailing list > PEG@lists.csail.mit.edu > https://lists.csail.mit.edu/mailman/listinfo/peg _______________________________________________ PEG mailing list PEG@lists.csail.mit.edu https://lists.csail.mit.edu/mailman/listinfo/peg