This folder contains examples of "calculator" programs in F# and Java that read an arithmetic expression from the command line and print the result of evaluating such expression.
F#/FsLexYacc
- CalculatorLexer.fsl: The F# lexer for arithmetic expressions
- CalculatorParser.fsp: The F# parser for arithmetic expressions
- CalculatorTypesAST.fs: F# types for AST of arithmetic expressions
- Calculator.fsx: The F# script for the calculator
Java/ANTLR4
- Calculator.g: The ANTLR4 grammar for arithmetic expressions.
- Calculator.java: The Java calculator, which uses the parser generated by ANTLR4.
This is instructions assume that you have followed the guidelines to getting started with F# and FSLexYacc and that you are using a terminal on a folder contaning the F# files mentioned above.
Invoke the lexer generator with
mono FsLexYacc.10.0.0/build/fslex/net46/fslex.exe CalculatorLexer.fsl --unicode
Invoke the parser generator with
mono FsLexYacc.10.0.0/build/fsyacc/net46/fsyacc.exe CalculatorParser.fsp --module CalculatorParser
Invoke the calculator with
fsharpi Calculator.fsx
You should be able to interact with the calculator program as follows:
Enter an arithmetic expression: 1
1.0
Enter an arithmetic expression: 1 + 2
3.0
Enter an arithmetic expression: 1 + 2 * 3
7.0
This is instructions assume that you have followed the guidelines to getting started with Java and ANLTR4 and that you are using a terminal on a folder contaning Calculator.g and Calculator.java.
Generate the visitor-based parser with
antlr4 -visitor -no-listener Calculator.g
Compile the program with
javac *.java
Run the program with
java Calculator
You should be able to interact with the calculator program as follows:
Enter an arithmetic expression: 1
1.0
Enter an arithmetic expression: 1 + 2
3.0
Enter an arithmetic expression: 1 + 2 * 3
7.0