TEXT   11

variables.txt

Guest on 23rd May 2021 08:04:00 AM

  1.                             Variables in Patterns
  2.  
  3. Anywhere you specify a label, you may append a '(' <variable name> ')' and
  4. the pattern will track all the nodes which match the appropriate subpattern.
  5. For example, /foo(X)//bar(Y) will find any tree rooted with a label "foo"
  6. which dominates some node labelled bar.  When you specify variable names
  7. and a tree matches, the formatter will identify all the variables associated
  8. with every node.
  9.  
  10.  
  11. In the programming API, you can find all the nodes which were associated
  12. with both X and Y via Match.get("X") and Match.get("Y").  More particularly:
  13.  
  14.     Pattern p = new Pattern("/foo(X)//bar(Y)");
  15.     Match m = p.getMatch(some_tree_object);
  16.     if (m.matches()) {
  17.         List nodes = m.get("Y");
  18.         Iterator i = nodes.iterator();
  19.         while(i.hasNext()) {
  20.             Tree t = (Tree) i.next();
  21.         }
  22.     }

Raw Paste


Login or Register to edit or fork this paste. It's free.