Hello everyone,
I thought the Java guys among you might be interested in a new, open
source Java 1.5+ library supporting PEG parsing in a pretty unique way.
http://www.parboiled.org was born out of frustration over existing
parser generators with their external grammar files that lack IDE
support and are often just a little bit too hard to integrate in real-
world projects.
parboiled let's you specify your PEG grammar rules right in Java source.
Small example:
Consider the following grammar (a classical calculator example):
Expression ← Term ((‘+’ / ‘-’) Term)*
Term ← Factor ((‘*’ / ‘/’) Factor)*
Factor ← Number / ‘(’ Expression ‘)’
Number ← [0-9]+
A parboiled parser definition, complete and in ready-to-compile Java
code would look like this:
public class CalculatorParser extends BaseParser<Object,
Actions<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'));
}
}
Your rule definitions can also include direct calls to action methods
which will behave as expected, they are called at the right time
during the parsing run.
parboiled supports AST construction in a very flexible way and has a
few other, quite neat features (like proper parse error recovery,
white space gobbling and so on). It currently implements a recursive
descent parser, but could be extended to support packrat parsing as
well without too much trouble.
The source and a rather full-blown documentation currently lives at
github, which you are forwarded to from http://parboiled.org.
Maybe you like the approach and are willing to provide feedback... I'd
be happy to receive any.
Cheers,
Mathias
---
[email protected]
http://www.parboiled.org
_______________________________________________
PEG mailing list
[email protected]
https://lists.csail.mit.edu/mailman/listinfo/peg