-
Notifications
You must be signed in to change notification settings - Fork 155
Simple Java Example
To give you a feel for what grammar specification looks like with parboiled for Java consider 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]+
A parboiled parser definition, complete and in ready-to-compile Java code would look like this:
@BuildParseTree class CalculatorParser extends BaseParser<Object> {
Rule Expression() { return Sequence( Term(), ZeroOrMore(AnyOf("+-"), Term()) ); }
Rule Term() { return Sequence( Factor(), ZeroOrMore(AnyOf("*/"), Factor()) ); }
Rule Factor() { return FirstOf( Number(), Sequence('(', Expression(), ')') ); }
Rule Number() { return OneOrMore(CharRange('0', '9')); } }
As you can see, the rule description from above translates directly into readable Java code (which is formatted here for readability, not minimization of line count). The class defines the parser rules (yet without any actions), which can be used to parse actual input like this:
(1) String input = "1+2";
(2) CalculatorParser parser = Parboiled.createParser(CalculatorParser.class);
(3) ParsingResult<?> result = new ReportingParseRunner(parser.Expression()).run(input);
(4) String parseTreePrintOut = ParseTreeUtils.printNodeTree(result);
(5) System.out.println(parseTreePrintOut);
Line 2 creates a parser instance whose rule creating methods can then be used with the various ParseRunners to perform an actual parsing run and create a ParsingResult object (line 3). Apart from the information whether the input was successfully matched the ParsingResult object contains the root of the parse tree for the expression (if parse tree building is enabled), the result value (which is null in this parser without actions) and a list of parse errors. A quick way for understanding how your parser digested the input is to print the created parse tree with ParseTreeUtils.printNodeTree as shown in line 4 and 5.
In general parboiled was designed to make your rule specification as clean and readable as possible under the constraints of the Java syntax. For more examples (including actions, AST construction, etc.) please see the Java Examples.
- Introduction
- ... Motivation
- ... Features
- ... Simple Java Example
- ... Simple Scala Example
- ... RegEx vs. parboiled vs. Parser Generators
- ... Projects using parboiled
- Installation
- Concepts
- ... The Big Picture
- ... The Rule Tree
- ... The Value Stack
- ... The Parse Tree
- ... AST Construction
- ... Parse Error Handling
- parboiled for Java
- ... Rule Construction in Java
- ... Parser Action Expressions
- ... Working with the Value Stack
- ... Action Variables
- ... Parser Extension in Detail
- ... Style Guide
- ... Java Examples
- ...... ABC Grammar
- ...... Calculators
- ...... Time Parser
- ...... Java Parser
- ...... Markdown processor
- parboiled for Scala
- ... Rule Construction in Scala
- ... Parser Actions in Scala
- ... Parser Testing in Scala
- ... Scala Examples
- ...... Simple Calculator
- ...... JSON Parser
- Advanced Topics
- ... Handling Whitespace
- ... Parsing Performance Tuning
- ... Indentation Based Grammars
- ... The ProfilingParseRunner
- ... Grammar and Parser Debugging
- ... Thread Safety
- Building parboiled
- parboiled-core Javadoc API
- parboiled-java Javadoc API
- parboiled-scala Scaladoc API
- Change Log
- Patch Policy