Docsity

Compiler Construction Project: Calculator using Flex and Yaac, Assignments of Compiler Construction

Partial preview of the text

Download Compiler Construction Project: Calculator using Flex and Yaac and more Assignments Compiler Construction in PDF only on Docsity!

COMPILER CONSTRUCTION

PROJECT

BCSM-F16 (481-036-007-367)

Section: BSCS (8D)

Submitted To: MAM MARYAM

 PROJECT Title: Calculator using flex and

yaac

 Department Computer Science and

Information Technology Superior

University, Lahore.

 INTRODUCTION

Rolf Haworth wrote the calculator in early 1996. A fully featured scientific calculator with proper operator precedence is implemented, including trigonometric functions and logarithms.in this implementation we have used flex and yaac to implement this calculator using c language. Although it is not fully scientific but it will perform major scientific tasks which are required by a user

 SCOPE OF THE PROJECT

Calculator has a wide scope of applications, it is useful in many fields like science, technology, accounting, marketing, education, finances, , etc. Their accessibility depends on the user understanding basic mathematical concepts such as addition, subtraction, multiplication, division, etc. Scientific calculators require a more advanced level of understanding from the user for their scientific functions. This Calculator project has vide scope in the field of mathematics’ as in math students and also in daily life people require a calculator to perform math or calculation tasks hence this will be beneficial for those students and people

 REGULAR Expression

[0-9]+.?|[0-9]*.[0-9]

Where as cos ,sin ,tan and others are functions and to perform these functions math.h library has been used DFA

 CFG

program →statement | program statement statement → exp | id = exp | clear id | list | quit | exit exp→ term | exp + term | exp - term term→ power | term * power | term / power power → factor | factor ** power

factor →id | number | ( exp ) | sqrt ( exp )

 LEX Code

#include <math.h> #include "y.tab.h" %} %% [0-9]+.?|[0-9]*.[0-9]+ { yylval.p = atof(yytext); return num;} log {return LOG;} sin {return SIN;} cos {return COS;} tan {return TAN;} sqrt {return SQRT;} [\t]; \n return 0;

. return yytext[0]; %%

%union {double p;} %token

num %token SIN COS TAN LOG %left '+' '-' %left '' '/' %nonassoc uminu %type

exp %% ss: expression '\n' { printf(" = %lf\n", $2); } ; expr: term { $$ = $1; } | expr '+' term { $$ = $1 + $3; } | expr '-' term { $$ = $1 - $3; } ; term: factor { $$ = $1; } | term '' factor { $$ = $1 * $3; } | term '/' factor { $$ = $1 / $3; } ; factor: Integer { $$ = $1; } | Float { $$ = $1;}

| group { $$ = $1; } ; power: factor { $$ = $1; } | factor ** power { $$ **$1;} ; factor :id { $$ = $1; } | number | ( exp ) | sqrt ( exp ) ; group: '(' expression ')' { $$ = $2; } ; { if ($3==0) { printf("Divided By Zero"); } else $$ = $1 / $3; } |'-'exp { $$ = -$2;} |SIN'('exp')' { $$ = sin($3);} |COS'('exp')' { $$ = cos($3);} |TAN'('exp')' { $$ = tan($3);} |LOG'('exp')' { $$ = log($3);} |SQRT'('exp')' { $$ = sqrt($3);}

 RESULTS AND CONCLUSION

After completing this project now the understanding of first three phases of compiler which are also front end phases has increased and building this term project has improve the understanding level which will help us in next phases and also in future when we learn the next phases of compiler. hence