Scientific Calculator using LEX & YACC tools

Lohith Briznath
3 min readMay 5, 2021

--

Introduction

If you work on a compiler project or develop a program to check the validity of an input language, you might want to use the system tool yacc . yacc generates parsers, programs that analyze input to insure that it is syntactically correct. lex and yacc often work well together for developing compilers.As noted, a program uses the lex- generated scanner by repeatedly calling the function yylex(). This name is convenient because a yacc-generated parser calls its lexical analyzer with this name.To use lex to create the lexical analyzer for a compiler, end each lex action with the statement return token, where token is a defined term with an integer value.The integer value of the token returned indicates to the parser what the lexical analyzer has found. The parser, called yyparse() by yacc, then resumes control and makes another call to the lexical analyzer to get another token.In a compiler, the different values of the token indicate what, if any, reserved word of the language has been found or whether an identifier, constant, arithmetic operator, or relational operator has been found. In the latter cases, the analyzer must also specify the exact value of the token: what the identifier is, whether the constant is, say, 9 or 888, whether the operator is + or *, and whether the relational operator is = or >.

Objective

Lex tool helps write programs whose control flow is directed by instances of regular expressions in the input stream. It is well suited for editor-scripts type transformations and for segmenting input in preparation for a parsing routine. Lex tool source is the table of regular expressions and corresponding program fragments. The table is translated to a program which reads an input stream, copying it to an output stream and partitioning the input into strings which match the given expressions. On the other hand YACC tool receives input of the user grammar. Starting from this grammar it generates the C source code for the parser. YACC invokes Lex to scan the source code and uses the tokens returned by Lex to build a syntax tree. With the help of YACC and Lex tool one can write their own compiler.

Problem Statement

YACC and Lex program to implement a Calculator and recognize a valid Arithmetic expression. A calculator is the basic and most important use of daily students. Keeping this in mind we have done this project.

Scope of Investigation

  • We have implemented scientific calculator which can perform operations which are listed below:-
  • Addition/Subtraction/Division/Multiplication
  • Trigonometric functions –sine,cos,tan
  • Logarithmic functions-log, natural logarithm(ln)
  • Expression can be enclosed with round brackets

Download the code from here: https://github.com/lohith-create/Scientific-Calculator-using-LEX-YACC-files

Demo :

Special Thanks To

We owe our deep gratitude to our mentor Dr. Anbunathan R, Founder and CEO, DigiTran solutions, Bangalore, Adjunct Professor, who took a keen interest in our Software and guided us all along, till the completion of our project work by providing all the necessary information for developing a good system.

We extend our sincere thanks to Dr. Sanjay Chitnis, Chairman, Department of Computer Science & Engineering who continuously helped us throughout the project, and without his guidance, this project would have been an uphill task

--

--

Lohith Briznath
Lohith Briznath

No responses yet