14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Parboiled (Java) 248<br />

Parboiled (Java)<br />

Developer(s) Mathias Doenitz<br />

Initial release November 12, 2009<br />

Development status Active<br />

Written in Java<br />

Operating system Cross-platform<br />

License Apache License 2.0<br />

Website http:/ / www. parboiled. org/<br />

parboiled is an open-source Java library released under an Apache License. It provides support for defining PEG<br />

parsers directly in Java source code.<br />

parboiled is commonly used as an alternative for regular expressions or parser generators (like ANTLR or JavaCC),<br />

especially for smaller and medium-size applications.<br />

Apart from providing the constructs for grammar definition parboiled implements a complete recursive descent<br />

parser with support for abstract syntax tree construction, parse error reporting and parse error recovery.<br />

Example<br />

Since parsing with parboiled does not require a separate lexing phase and there is no special syntax to learn for<br />

grammar definition parboiled makes it comparatively easy to build custom parsers quickly.<br />

Consider this the following classic “calculator” example, with these rules in a simple pseudo notation<br />

Expression ← Term ((‘+’ / ‘-’) Term)*<br />

Term ← Factor (('*' / '/') Factor)*<br />

Factor ← Number / '(' Expression ')'<br />

Number ← [0-9]+<br />

With parboiled this rule description can be translated directly into the following Java code:<br />

import org.parboiled.BaseParser;<br />

public class CalculatorParser extends BaseParser {<br />

public Rule expression() {<br />

}<br />

return sequence(<br />

);<br />

term(),<br />

zeroOrMore(<br />

public Rule term() {<br />

)<br />

sequence(<br />

)<br />

firstOf('+', '-'),<br />

term()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!