Skip to content

azartech/Flex-Bison-Calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

23 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Flex Bison Calculator

β€’ 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.

CFG

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 ")}

Usage

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!

Example

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

About

Creating a calculator using Flex and Bison

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors