Using processor classes

The LexicalProcessor and ParserProcessor are designed to be used in a pipeline structure. These processor used class, which implemented the LexicalHandler and ParserHandler interfaces, to transfer the the produced events.

LexicalProcessor processor = new LexicalProcessor();
processor.setLexicalAutomaton(automaton);

before.setTextHandler(processor);

LexicalHandler after = new LexicalHandler() 
{
  public void handleStartDocument() { [...] }

  public void handleLexeme(String symbol, String text) { [...] }

  public void handleEndDocument() { [...] }
}
processor.setLexicalHandler(after);

So the handler received all produced Events. The LexicalProcessor self implements the interface TextHandler, and receives events from a text producer. The ParserProcessor is similar used.

ParserProcessor processor = new ParserProcessor();
processor.setParserAutomaton(automaton);

before.setLexicalHandler(processor);

ParserHandler after = new ParserHandler() 
{
  public void handleStartDocument() { [...] }

  public void handleShiftToken(String symbol, String text) { [...] }

  public void handleReduceProduction(String symbol) { [...] }

  public void handleEndDocument() { [...] }
}
processor.setParserHandler(after);

Similar to the LexicalHandler, the ParserHandler receives all events from the ParserProcessor. And ParserProcessor implements the LexicalHandler interface, so you can just plug the ParserProcessor into the LexicalProcessor.

There also exists a third processor for matching against a pattern. This implementation differ from the others.

PatternProcessor matcher = new PatternProcessor();
matcher.setPatternAutomaton(pattern);

char[] text = "bla bla blub bla".toCharArray();

if (matcher.search(text))
  System.out.println(matcher.getGroup());

This last processor is equivalent to an regex pattern matcher.

by Stephan Michels