[antlr-interest] idioms for walking multiple trees
Bryan Ewbank
ewbank at gmail.com
Thu Apr 21 01:33:15 PDT 2005
Hi Folks,
I'm doing some multi-tree walking, and am curious what ANTLR idioms
other people use to accomplish this. The grammar fragment I'm using
looks something like this - can't share the true source code :-(.
It feels lisp'ish to me, but perhaps that's okay given what I'm doing.
anyone have something cleaner/neater?
1 /**
2 * Walks a simple name/value pair:
3 * (NAME_VALUE name value)
4 *
5 * Takes an additional dictionary argument
6 * ( (NAME_VALUE name value) (NAME_VALUE name value) ... )
7 *
8 * Resultant tree, ##, is:
9 * - the walked tree if name is not in dictionary
10 * - the dictionary entry, if name is in the dictionary.
11 */
12 replace [AST actual_arg]
13 {
14 bool foundIt = false;
15 AST newval;
16 }
17 :
18 #(NAME_VALUE lhs:name
19 { newval = lookup(actual_arg, #lhs->getText(), foundIt); }
20 (
21 {foundIt}?
22 /* replace with dictionary entry */
23 { ## = newval; }
24 |
25 /* no match; leave it alone */
26 rhs:value
27 )
28 )
29 ;
30
31 lookup [const std::string &argname, bool & foundIt]
32 returns [AST tree]
33 {
34 foundIt = false;
35 }
36 :
37 (
38 #(NAME_VALUE lhs:name rhs:value)
39 {
40 if (#lhs->getText() == argname)
41 {
42 foundIt = true;
43 tree = ##;
44 }
45 }
46 (
47 {foundIt==true}?
48 /* this one matched; terminate traversal */
49 |
50 tree=lookup[argname, foundIt]
51 )
52 )?
53 ; )
More information about the antlr-interest
mailing list