- Open terminal.
- Go to the directory containing all files.
- Run "sudo sml rational.sml".
- Type your password and press enter key.
OR
- Run "sudo sml".
- Type your password and press enter key.
- Run "CM.make "makefile.cm";".
- Run "Calc.parse();".
- Type your expression, add a semicolon at the end then press enter.
- Your expression can include:
- Binary operators: "+", "-", "*" and "/"
- Negation: "~" (Warning: Not a operator, can only be used on values)
- Integers: Use "~" instead of "-" to denote negative numbers.
- Decimals: In the fractional-normal form.(Use "~" instead of "-" to denote negative numbers)
Rational ::= "~" rational | rational
rational ::= integer | decimal | fraction
integer ::= digit | digitinteger
decimal ::= integer "." integer "(" integer ")" | "." integer "(" integer ")" | integer "." "(" integer ")"
fraction ::= integer "/" nonzerointeger
nonzerointeger ::= integer positivedigits
positivedigits ::= "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
digit ::= positivedigits | "0"
NUM ::= integer | "" integer | decimal | "" decimal
integer::= digit | digit integer
decimal::= integer "." integer "(" integer ")" | "." integer "(" integer ")" | integer "." "(" integer ")"
digit ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
(fractions are not implemented in parser as their functionality is obtained through dividing)
Start ::= Expression ";"
Expression ::= Term | Term "+" Term | Term "-" Term
Term ::= Rat | Rat "*" Rat | Rat "/" Rat
Rat ::= RAT | variable
RAT ::= Rational | "(" Expression ")"
variable ::= alphabet | alphabet integer | alphabet integer variable
alphabet ::= “A” | “B” | “C” | “D” | “E” | “F” | “G” | “H” | “I” | “J” | “K” | “L” | “M” | “N” | “O” |
“P” | “Q” | “R” | “S” | “T” | “U” | “V” | “W” | “X” | “Y” | “Z” | “a” | “b” | “c” | “d” |
“e” | “f” | “g” | “h” | “i” | “j” | “k” | “l” | “m” | “n” | “o” | “p” | “q” | “r” | “s” |
“t” | “u” | “v” | “w” | “x” | “y” | “z”
START ::= EXP ";"
EXP ::= NUM | EXP "+" EXP | EXP "-" EXP | EXP "*" EXP | EXP "/" EXP | "(" EXP ")"
(The rules for associativity and precedence are mentioned separately in the parser)
I have used the resources present in the SMLNJ Documentation of ML lex and ML yacc. Namely the base structure for the parser as present in the Rational_parser.lex, Rational_parser.grm, Rational_parser.sml files is taken from the above mentioned source.