Skip to content

Latest commit

 

History

History
34 lines (22 loc) · 950 Bytes

README.md

File metadata and controls

34 lines (22 loc) · 950 Bytes

Building a Calculator Using a Visitor

Use ANTLR to generate the parser for the grammar Expr.g4. Generate D code (-Dlanguage=D) in the source directory (-o src). And generate a visitor (-visitor) instead of a listener (-no-listener).

antlr4 -Dlanguage=D -o src -visitor -no-listener Expr.g4

You need src/main.d and src/calc.d to set up the parser and to provide a visitor implementation that evaluates arithmetic expressions.

Use dub to run, build, or test the application. For example:

dub run -- test.expr
echo '1 + 2 * 3' | dub run

Or build the application and then run it:

dub build
./expr test.expr
echo '1 + 2 * 3' | ./expr

If you want to see all the ANTLR debug output, build the application like this:

dub build --build=antlr-debug

Build a unittest runner and let it run:

dub test