Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Seems like we sometimes want preorder to get stuff in input order and then postorder to make work on larger patterns after the inner patterns are done. E.g., work on the function after locals are examined.

XPath for parse trees

The interface is very simple. Just call findAll() on your parse tree and passive the parser and an xpath. Put the following jar at the front of your class path so it hides the old version of ANTLR:

http://www.antlr.org/download/antlr-4-xpath.jar             (drop in binary jar)

Here is the source if you want to look at the code or look at github xpath package:

http://www.antlr.org/download/antlr4-xpath-src.zip       (source drop, no binaries)

 Sample that works off of the Java grammar

...

This functionality has now been integrated into the master branch of antlr4.

Code Block
for (ParseTree t : treeXPath.findAll(parsertree, "//expression//Identifier") ) {
			if ( t instanceof RuleContext ) {
				RuleContext r = (RuleContext)t;
				System.out.println("  "+parser.getRuleNames()[r.getRuleIndex()]);
			}
			else {
				TerminalNode token = (TerminalNode)t;
				System.out.println("  "+token.getText());
			}
		}
	}
}

...

xpath, parser) ) { ... }

 For examples, see TestXPath.java