Parboiled (Java)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by DannyS712 bot (talk | contribs) at 18:18, 11 May 2020 (Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

parboiled Library
Developer(s)Mathias Doenitz
Initial releaseNovember 12, 2009; 14 years ago (2009-11-12)
Stable release
1.3.1 / June 24, 2019; 4 years ago (2019-06-24)[1]
Repository
Written inJava
Operating systemCross-platform
LicenseApache License 2.0
Websiteparboiled.org

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 (like ANTLR or JavaCC), 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

ExpressionTerm ((‘+’ / ‘-’) Term)*
TermFactor (('*' / '/') Factor)*
FactorNumber / '(' Expression ')'
Number ← [0-9]+

With parboiled this rule description can be translated directly into the following Java code:

import org.parboiled.BaseParser;

public class CalculatorParser extends BaseParser<Object> {

    public Rule Expression() {
        return Sequence(
                Term(),
                ZeroOrMore(
                        Sequence(
                                FirstOf('+', '-'),
                                Term()
                        )
                )
        );
    }

    public Rule Term() {
        return Sequence(
                Factor(),
                ZeroOrMore(
                        Sequence(
                                FirstOf('*', '/'),
                                Factor()
                        )
                )
        );
    }

    public Rule Factor() {
        return FirstOf(
                Number(),
                Sequence('(', Expression(), ')')
        );
    }

    public Rule Number() {
        return OneOrMore(CharRange('0', '9'));
    }

}

The class defines the parser rules for the language (yet without any actions), which could be used to parse actual input with code such as this:

String input = "1+2";
CalculatorParser parser = Parboiled.createParser(CalculatorParser.class);
ParsingResult<?> result = ReportingParseRunner.run(parser.expression(), input);
String parseTreePrintOut = ParseTreeUtils.printNodeTree(result); 
System.out.println(parseTreePrintOut);

See also

References

  1. ^ "Changelog". Parboiled. June 24, 2019. Retrieved January 7, 2020.

External links