Parboiled (Java)


parboiled is an open-source Java library released under an Apache License. It provides support for defining PEG parsers directly in Java source code.
parboiled is commonly used as an alternative for regular expressions or parser generators, especially for smaller and medium-size applications.
Apart from providing the constructs for grammar definition parboiled implements a complete recursive descent parser with support for abstract syntax tree construction, parse error reporting and parse error recovery.

Example

Since parsing with parboiled does not require a separate lexing phase and there is no special syntax to learn for grammar definition parboiled makes it comparatively easy to build custom parsers quickly.
Consider this the following classic “calculator” example, with these rules in a simple pseudo notation
With parboiled this rule description can be translated directly into the following Java code:

import org.parboiled.BaseParser;
public class CalculatorParser extends BaseParser

The class defines the parser rules for the language, which could be used to parse actual input with code such as this:

String input = "1+2";
CalculatorParser parser = Parboiled.createParser;
ParsingResult result = ReportingParseRunner.run;
String parseTreePrintOut = ParseTreeUtils.printNodeTree;
System.out.println;

OWIKI.org. Text is available under the Creative Commons Attribution-ShareAlike License.