Simple Calculator

2beaucoup edited this page Sep 21, 2012 · 6 revisions
Clone this wiki locally

The Simple Calculator example lives in the org.parboiled.examples.calculators package and is a set of two example parsers show-casing a simple recognizer and an actual calculator built with parboiled for Scala.

The first one, the SimpleCalculator0 is a simple recognizer for the following basic grammar, without any parser actions:

Expression ← Term ((‘+’ / ‘-’) Term)*
Term ← Factor ((‘*’ / ‘/’) Factor)*
Factor ← Number / Parens
Number ← Digit+
Parens ← ‘(’ expression ‘)’
Digit ← [0-9]

The SimpleCalculator1 examples adds parser actions that perform the actual calculations directly on the parsers value stack, without first creating an AST for the expression.