Lex program to implement a simple Calculator Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. Luckily there is freely available software to assist in these functions. While they will not do everything for you, they will enable faster implementation of the basic functions. Lex and Yacc are the most commonly used packages with Lex managing the token recognition and Yacc handling the syntax.
This Interpreter includes 2 files to run a flex file and a yacc file.
- Open the terminal and go to the folder where these files are
- Write the the command lex bc.l to compile the lex file
- Then write yacc term1.y to compile the yacc file , this will generate y.tab.c
- Then compile y.tab.c using c compiler to generate a.out by writing the command cc y.tab.c -ly -ll -lm
- Run the a.out file using ./a.out command
- Your calculator is ready
- You can use different functions/operators given in manual
- If u write something other than functions as specified in manual it will give syntax error
- In order to get out of the calculator type ctrl+C .
Scientific calculator using lex and yaccFunctions and Instructions :-
- log() :- Logarithmic function used for calculating log value
- pi :- Writing pi will give out the value of pi
- sin() :- Sin function used for calculating sin value
- cos() :- Cos function used for calculating cos value
- tan() :- Tan function used for calculating tan value
- and :- It will give logical AND of 2 numbers
- sinh() :- Sinh function used for return the sin hypotenuse value
- cosh() :- Cosh function used for returning the cos hypotenuse value
- tanh() :- Tanh function used for returning the tan hypotenuse value
- & :- It used to return bitwise AND of 2 numbers
- xor :- It is used to return XOR of 2 numbers
- or :- It is used to return OR of 2 numbers
- ceil :- It gives nearest integer greater then or equal to the number
- floor :- It gives nearest integer smaller then or equal to the number
- abs() :- Returns the absolute value of the number
- '<<' :- Left shifts the bits of expression to left of operator by number specified to the right of operator
- '>>' :- Right shifts the bits of expression to left of operator by number specified to the right of operator
- '++' :- Increment the number by 1 by writing ++ after number
- '--' :- Decrement the number by 1 by writing – after number
- '+' :- Used for adding 2 numbers
- '-' :- it is used for subtracting 2 numbers
- '|' :- It gives bitwise OR of 2 numbers;
- '%' :- It return the quotient when dividing 2 numbers
- '*' :- it used for multiplying 2 numbers
- '^' :- Returns the number given by left number to the operator to the power of number to the right of operator
- sqrt() :- It will return the square root value
- '/' :- It return the approximate number when 2 numbers are divided
- '&&' :- Returns logical AND of 2 numbers
- “||” :- Gives logical OR of 2 numbers
- asin() :- asin function used for calculating inverse sin value
- acos() :- acos function used for calculating inverse cos value
- atan() :- atan function used for calculating inverse tan value
- factorial() :- It will calculate the factorial of the given number
- bin_dec() :- Give a number in 1s and 0s and the function will return the decimal value
All the values in trigonometric functions which are inserted or returned are in radians .If the user wants to calculate other than this and if it gives input in the manner other than above specified it will give syntax error . In case of input given is out of range in inverse functions it will print out “nan” i.e. it is out of range .In case of bin_dec function if u give integer other than 0 and 1 it will return the same value.
A simple calculator using lex and yacc . A grammer that allows calculation with a new Lex specification that recognizes english words:Such that you can now do : Example 5 MINUS 22 PLUS 24 TIMES 54 MULTIPLY 515 DIVIDE 3
pas.l - is the lex filepas.y - i the yacc file
CAVEAT: Operator precedence yot yet handled! (keeping it simple for those new to LEX AND YACC) warning: 16 shift/reduce conflicts
Requirements : compatible versions of the cygwin distributions lex/yacc or Flex or Bison
![Write A Lex Program For Basic Desktop Calculator Using Yacc Write A Lex Program For Basic Desktop Calculator Using Yacc](/uploads/1/2/5/7/125744141/922030721.jpg)
Usage:Commands to create our compiler, pas.exe, are listed below:lex pas.l # create lex.yy.cyacc –d pas.y # create y.tab.h, y.tab.ccc lex.yy.c y.tab.c –o pas.exe # compile/link
Acceptable tokens:[0-9]+ { yylval = atoi(yytext);return INTEGER;}[-+/n] return yytext;PLUS return '+';MINUS return '-';TIMES return '';MULTIPLY return '';DIVIDE return '/';[ t] ; /* skip whitespace */