Anda di halaman 1dari 15

COMMAND LINE

CALCULATOR

BY SHOAIB SHAIKH
SHIPRA MISHRA
SIMRAN PAINTAL
List Of Contents

Objective
OBJECTIVE

There is no decent command line calculator available on linux for example, we cannot just
type, solve 5+8 or 33/5 and get the solution. So, we want to use linux terminal to do
mathematical calculations using command line in lex and yacc.
We wish to create an interface of any program in linx like bc. We want a calculator that
can handle floating point numbers and is able to specify the post decimal point precision,
we seek.
Command line calculators provide us with many benefits that are unavailable and hard to
achieve or are incomplete.
Command line Calculator
Advantages

SCALABILITY : Command line tools offer much better opportunities to deal with
scalability problems easily. They are better at organizing output into groups so the tasks that
require the same user responds are all lumped together.

SCRIPTABILITY : The power of scripting of command line tools are very high. They
ensure uniformity and of scripting interfaces and availability of automation capabilities for
more pieces of software that would be very scriptable.

SIMPLE DESIGN : Simplicity is the key to security. Considering security benefits of simple
design, we just need to understand the relationships between security, complexity and the GUI
environment.
Contd.

STABALITY : GUI applications need to constant change to fir into new evolving environments
over time. Command line calculators dont tend to suffer the problem of evolving circumstances. If
a new feature must be added, it rarely ever requires shuffling the old features and they can be
accessed the same way as they were before the upgrade.

SIMPLE INTERFACE : The calculator has a straightforward obvious design to perform relatively
complex tasks. It doesnt require anyone to add features to the cat utility itself such as:
Features for reading for reading level estimation
PDF export
Spell grammar checking
Command line Calculators Limitations

The bc command line only offers integer results. We already know that 5/2=2.5, but bc
shows it as 2. That doesnt make it any useful for high level calculations.
Another similar calculator is the desktop calculator(dc) but it seems fundamentally flawed
because it provides and entire new notation to figure out simple mathematical problems.
Like:
$dc 1+1

Could not open file 1

dc: stack empty

$dc e 1+1

Could not open file +


LEX AND YACC(At a glance)

What is lex?

The main job of a lexical analyzer (scanner) is to break up an input stream into
more usable elements (tokens)
a = b + c * d;
ID ASSIGN ID PLUS ID MULT ID SEMI

Lex is an utility to help you rapidly generate your scanners


Compilation Sequence
Lex v.s. Yacc

Lex
Lex generates C code for a lexical analyzer, or scanner
Lex uses patterns that match strings in the input and converts the strings to
tokens

Yacc
Yacc generates C code for syntax analyzer, or parser.
Yacc uses grammar rules that allow it to analyze tokens from Lex and create
a syntax tree.
Source Code
LEX CODE:
%{
#include "y.tab.h
#include <ctype.h>%}%%"print"
{return print;}"exit"
{return exit_command;}[a-zA-Z]
{yylval.id = yytext[0]; return identifier;}
[0-9]+
{yylval.num = atoi(yytext); return number;
}
[ \t\n] ;
[-+=;]
{return yytext[0];}.
{ECHO; yyerror("Unexpected character");}
%%int yywrap(void)
{return 1;}
YACC CODE:
%{
void yyerror(char *s);
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int symbols[52];
int symbolVal(char symbol);
void updateSymbolVal(char symbol, int val);
%}

%union {int num; char id;}


%start line
%token print
%token exit_command
%token <num> number
%token <id> identifier
%type <num> line exp term
%type <id> assignment
%%

/* descriptions of expected inputs corresponding actions in C*/

line : assignment ';' {;}


| exit_command ';' {exit(EXIT_SUCCESS);}
| print exp ';' {printf("Printing %d\n", $2);}
| line assignment ';' {;}
| line print exp ';' {printf("Printing %d\n", $3);}
| line exit_command ';' {exit(EXIT_SUCCESS);}
;

assignment : identifier '=' exp { updateSymbolVal($1, $3);}


;

exp : term {$$ = $1;}


| exp '+' term {$$ = $1 + $3;}
| exp '-' term {$$ = $1 - $3;}
;
term : number {$$ = $1;}
| identifier {$$ = symbolVal($1);}
;

%%

/* C Code*/

int computeSymbolIndex(char token)


{
int idx = -1;
if(islower(token)){
idx = token - 'a' + 26;
}
else if(isupper(token)){
idx = token - 'A';
}
return idx;
}
/* Returns value of a given symbol */
int symbolVal(char symbol)
{
int bucker = computeSymbolIndex(symbol);
return symbols[bucker];
}
/* Updates the value of given symbol */
void updateSymbolVal(char symbol, int val)
{
int bucker = computeSymbolIndex(symbol);
symbols[bucker] = val;
}
int main(void)
{
/* Init symbol table */
int i;
for(i=0; i<52; i++)
{symbols[i] = 0;}
return yyparse();
}
void yyerror(char *s) {fprintf(stderr, "%s\n", s);}
Conclusion

You can do calculations similar to the other command line calculators mentioned above.
These were some of the command line calculator utilities you can use on the Linux
terminal.