arothuis / antlr4-calculator Goto Github PK
View Code? Open in Web Editor NEWANTLR4 calculator example and explanation
License: MIT License
ANTLR4 calculator example and explanation
License: MIT License
Things that would benefit education:
parser
package is perhaps a misnomer: it contains behaviors rather than merely parsing (evaluator
or translator
might be more accurate names)CARET
, PLUS
, MINUS
, STAR
, SLASH
, LEFT_PARENTHESIS
, RIGHT_PARENTHESIS
I tested your calculator with the inputs below:
Calculate: 1-2
-1.0
Calculate: 1-2*3
-5.0
Calculate: 1-2*3+4
-9.0
This is wrong, 1-2*3+4 = -1.0
Start program with 'nl.arothuis.antlr4calculator.ListenerMain' or 'nl.arothuis.antlr4calculator.ListenerMain'
Calculate: 4-2
4.0
Calculate: 4+-2
2.0
This may due to "-" is treated as number, but how to fix it?
Right now, the parser is quite tolerant -- a bit too tolerant, perhaps.
For educational purposes, it might be a better idea to explicitly fail on a parse error and give the user some feedback on what went wrong.
Hi,
Sorry to interrupt. Could you help me how to build this project in Intellij IDEA locally?
import nl.arothuis.antlr4calculator.core.parser.CalculatorLexer;
import nl.arothuis.antlr4calculator.core.parser.CalculatorParser;
It suggests that these two classes cannot be imported.
It might be interesting to support floating-point numbers, rather than only integers (1.23
).
We could also allow _
as a separator for more legible numbers (i.e. 1_000_000
).
This requires adding and changing token names for better disambiguation.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.