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) 249<br />

}<br />

}<br />

return sequence(<br />

);<br />

factor(),<br />

zeroOrMore(<br />

public Rule factor() {<br />

}<br />

)<br />

return firstOf(<br />

);<br />

sequence(<br />

)<br />

number(),<br />

public Rule number() {<br />

}<br />

firstOf('*', '/'),<br />

factor()<br />

sequence('(', expression(), ')')<br />

return oneOrMore(charRange('0', '9'));<br />

The class defines the parser rules for the language (yet without any actions), which could be used to parse actual<br />

input with code such as this:<br />

String input = "1+2";<br />

CalculatorParser parser =<br />

Parboiled.createParser(CalculatorParser.class);<br />

ParsingResult result = ReportingParseRunner.run(parser.expression(),<br />

input);<br />

String parseTreePrintOut = ParseTreeUtils.printNodeTree(result);<br />

System.out.println(parseTreePrintOut);<br />

See also<br />

• Parsing expression grammars<br />

• Regular expressions<br />

• ANTLR<br />

• JavaCC<br />

External links<br />

• Official parboiled web site [1]<br />

• Example of a complete Java 5 parser written with parboiled [2]

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

Saved successfully!

Ooh no, something went wrong!