β’ Supports the following math functions :
Β Β Β Β Β Β Β Β Β Β Β Add - Subtract - Divide - Multiply - Power - Assignment - Sin - Cos - Sqrt - Abs - Exp - Sinh - Cosh
β’ Can recognize both Integer and Float values.
β’ Can recognize scientific values as Float (e.g. 314e-2 = 3.14).
β’ Can Assign values to identifires using "=" sign.
β’ Prints out a list of used Tokens in each command.
β’ Prints out the final output of the enterd math function as a Double value.
The equivalent CFG for the grammer used in this project is as follows :
stmts β stmt stmts
stmts β Ξ΅
stmt β ππ {πΈπππ‘(lvalue + ππ. πππ₯)} = expr{πΈπππ‘("ππ π πππ")};
expr β expr + term {πΈπππ‘("πππ")}
expr β expr β term {πΈπππ‘("π π’π")}
expr β term
term β term β unary {πΈπππ‘("ππ’π")}
term β term / unary {πΈπππ‘("πππ£")}
term β unary
unary β βunary {πΈπππ‘("πππ")}
unary β pow
pow β factor ^ pow {πΈπππ‘("πππ€")}
pow β factor
factor β id {πΈπππ‘("rvalue " + πd. πππ₯)}
factor β πππ‘ {πΈπππ‘("ππ’π h " + πππ‘. πππ₯)}
factor β πππππ‘ {πΈπππ‘("ππ’π h " + πππππ‘. πππ₯)}
factor β (expr)
factor β sin(expr) {πΈπππ‘("π ππ " )}
factor β cos(expr){πΈπππ‘("πππ " )}
factor β sqrt(expr){πΈπππ‘("π πππ‘ " )}
factor β abs(expr){πΈπππ‘("πππ " )}
factor β exp(expr){πΈπππ‘("ππ₯π " )}
factor β sinh(expr){πΈπππ‘("π ππh " )}
factor β cosh(expr){πΈπππ‘("πππ h ")}
First of all, please make sure that both Flex and Bison are installed.
To use the calculator, clone the project. Open command prompt and cd to the directory. Then use the following commands. If there were warnings, ignore them.
bison -d -y calc.y
flex calc.lex
gcc -c y.tab.h y.tab.c lex.yy.c
gcc y.tab.o lex.yy.o -o $@ -lm
A Unix executable file named "-lm" will be generated. open the file and enjoy!
a=2
T_IDEN T_EQL T_INT = 2.000000
a^3
T_IDEN T_POW T_INT = 8.000000
23 + sin(sqrt(16))
T_INT T_ADD T_SIN T_PRNL T_SQRT T_PRNL T_INT T_PRNR T_PRNR = 22.243198
100 + 2e2
T_INT T_ADD T_FLOAT = 300.000000