https://github.com/tripodsan/antlr4_js_calculator
Simple command line calculator built with antrl4 and nodejs.
https://github.com/tripodsan/antlr4_js_calculator
antlr4 javascript nodejs tutorial
Last synced: 2 months ago
JSON representation
Simple command line calculator built with antrl4 and nodejs.
- Host: GitHub
- URL: https://github.com/tripodsan/antlr4_js_calculator
- Owner: tripodsan
- License: apache-2.0
- Created: 2018-02-14T23:47:25.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-15T02:36:45.000Z (over 8 years ago)
- Last Synced: 2025-03-28T06:41:27.237Z (over 1 year ago)
- Topics: antlr4, javascript, nodejs, tutorial
- Language: JavaScript
- Homepage:
- Size: 17.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Antlr4 Javascript Calculator
Example project with [Tutorial](tutorial.md) that shows how to quickly build a command line calculator with antrl4 and nodejs.
The code here is based on the Java example provided by Lucas on [Stackoverflow](https://stackoverflow.com/a/29996191/3229985).
In particular, the grammar is copied as is.
## Quick Start
### Prerequisite: install antlr4
1. Follow the installation instructions on the [Antlr4 - Get Started](https://github.com/antlr/antlr4/blob/master/doc/getting-started.md#installation) or
the official [site](http://www.antlr.org/).
2. make sure that an antlr4 works:
```bash
$ antlr4
ANTLR Parser Generator Version 4.7.1
...
```
### Build
The build process invokes antlr4 and generates the JavaScript _classes_ that are used
to parse the expressions.
Using Yarn:
```bash
$ yarn install
$ yarn build
```
Using npm:
```bash
$ npm install
$ npm run build
```
### Running
without arguments, the calculator runs as REPL (read-eval-print-loop)
```
$ node src/main.js
Simple calculator.
> 10 * (3+4) / 10
: 10 * (3+4) / 10 = 7
> _
```
with arguments, the calculator just prints the result of the expression given:
```
$ node src/main.js "4*(3+7)+2"
42
```