Parboiled (Java)
It is proposed that this article be deleted because of the following concern:
If you can address this concern by improving, copyediting, sourcing, renaming, or merging the page, please edit this page and do so. You may remove this message if you improve the article or otherwise object to deletion for any reason. Although not required, you are encouraged to explain why you object to the deletion, either in your edit summary or on the talk page. If this template is removed, do not replace it. This message has remained in place for seven days, so the article may be deleted without further notice. Find sources: "Parboiled" Java – news · newspapers · books · scholar · JSTOR Timestamp: 20230104030210 03:02, 4 January 2023 (UTC) Administrators: delete |
![]() | This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these messages)
|
Developer(s) | Mathias Doenitz |
---|---|
Initial release | November 12, 2009 |
Stable release | 1.3.1
/ June 24, 2019[1] |
Repository | |
Written in | Java |
Operating system | Cross-platform |
License | Apache License 2.0 |
Website | parboiled |
parboiled is an open-source Java library released under an Apache License. It provides support for defining PEG parsers directly in Java source code.[2][3]
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
Expression ← Term (('+' / '-') Term)*
Term ← Factor (('*' / '/') Factor)*
Factor ← Number / '(' 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
- ^ "Changelog". Parboiled. June 24, 2019. Retrieved January 7, 2020.
- ^ Kumar, Jayant (2015). Apache Solr Search Patterns: Leverage the Power of Apache Solr to Power Up Your Business by Navigating Your Users to Their Data Quickly and Efficiently. Packt Publishing. p. 69. ISBN 9781783981854.
- ^ Darwin, Ian F. (2014). Java Cookbook: Solutions and Examples for Java Developers (3rd ed.). O'Reilly. p. 317. ISBN 9781449338824.
External links