| 1 |
This is a sample interactive calculator built using ML-Yacc and ML-Lex. |
| 2 |
|
| 3 |
The calculator is defined by the files |
| 4 |
|
| 5 |
calc.lex (* defines lexer *) |
| 6 |
calc.grm (* defines grammar *) |
| 7 |
calc.sml (* defines driver function, Calc.parse *) |
| 8 |
calc.mlb (* ML Basis file *) |
| 9 |
|
| 10 |
To compile this example, type the following commands |
| 11 |
|
| 12 |
mllex calc.lex |
| 13 |
mlyacc calc.grm |
| 14 |
mlton calc.mlb |
| 15 |
|
| 16 |
in this directory. They will invoke ml-lex and ml-yacc to process the |
| 17 |
lexer specification calc.lex and the grammar specification calc.grm |
| 18 |
respectively. Then it will compile the resulting SML source files |
| 19 |
|
| 20 |
calc.lex.sml |
| 21 |
calc.grm.sig |
| 22 |
calc.grm.sml |
| 23 |
|
| 24 |
and the calc.sml file containing the driver code. |
| 25 |
|
| 26 |
The end result of compiling these files is an executable file named |
| 27 |
"calc", that is based on the structure Calc containing a top-level |
| 28 |
driver function named parse. |
| 29 |
|
| 30 |
Calc.parse : unit -> unit |
| 31 |
|
| 32 |
The calculator can be invoked by applying Calc.parse to the unit value. |
| 33 |
|
| 34 |
- Calc.parse(); |
| 35 |
1+3; |
| 36 |
result = 4 |
| 37 |
|
| 38 |
The calculator reads a sequence of expressions from the standard input |
| 39 |
and prints the value of each expression after reading the expression. |
| 40 |
Expressions must be separated by semicolons. An expression is not |
| 41 |
evaluated until the semicolon is encountered. The calculator |
| 42 |
terminates when an end-of-file is encountered. There is no attempt to |
| 43 |
fix input errors: a lexical error will cause exception LexError to be |
| 44 |
raised, while a syntax error will cause ParseError to be raised. |
| 45 |
|
| 46 |
NOTE: The ML Basis file calc.mlb mentions the ml-yacc library |
| 47 |
(mlyacc-lib.mlb). MLton's search path should be configured so that this |
| 48 |
library will be found. This should normally be the case if MLton is |
| 49 |
properly installed. |