Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This yacc calculator I created runs correctly, but I\'m having trouble adding a

ID: 3679114 • Letter: T

Question

This yacc calculator I created runs correctly, but I'm having trouble adding a sqrt function in it. Is there any way to put a square root function in it like it I did -, +, / and *. I also want the calculator to work on negative numbers(it currently only work on positive ones).

%{

#include <stdio.h>

#include <ctype.h>

#include <math.h>

%}

%token NUMBER

%%

command : exp {printf("%d ",$1);}

;

exp : exp '+' term {$$ = $1 + $3;}

| exp "-" term {$$ = $1 - $3;}

| exp '^' term {$$ = pow($1,$3);}

|'(' exp ')' term {$$ = $2;}

| term {$$ = $1;}

;

term : term '*' factor {$$ = $1 * $3;}

| term '/' factor {$$ = $1 / $3;}

| factor {$$ = $1;}

;

factor : NUMBER {$$ = $1;}

| '(' exp ')' {$$ = $2;}

%%

main()

{ return yyparse();

}

int yylex(void)

{ int c;

while((c = getchar()) == ' ');

if ( isdigit(c) ) {

ungetc(c, stdin);

scanf("%d",&yylval);

return(NUMBER);

}

if ( c == ' ') return 0;

return(c);

}

int yyerror(char * s)

{ fprintf(stderr,"%s ",s);

return 0;

}

Explanation / Answer

#include <stdio.h>

#include <ctype.h>

#include <math.h>

%}

%token NUMBER

%%

command : exp {printf("%d ",$1);}

;

exp : exp '+' term {$$ = $1 + $3;}

| exp "-" term {$$ = $1 - $3;}

| exp '^' term {$$ = pow($1,$3);}

|'(' exp ')' term {$$ = $2;}

| term {$$ = $1;}

;

term : term '*' factor {$$ = $1 * $3;}

| term '/' factor {$$ = $1 / $3;}

| factor {$$ = $1;}

;

factor : NUMBER {$$ = $1;}

| '(' exp ')' {$$ = $2;}

%%

main()

{ return yyparse();

}

int yylex(void)

{ int c;

while((c = getchar()) == ' ');

if ( isdigit(c) ) {

ungetc(c, stdin);

scanf("%d",&yylval);

return(NUMBER);

}

if ( c == ' ') return 0;

return(c);

}

int yyerror(char * s)

{ fprintf(stderr,"%s ",s);

return 0;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Lex file

%{
       #include
       #include”y.tab.h”    //for left,right,up & down
%}
%%

[0-9]+|[0-9]*.[0-9]+ {
                       yylval.p = atof(yytext);
                       return num;        //return nonterminal
                       }

sin {return SIN;}    //return token SIN to YACC
cos {return COS;}    //return token COS to YACC
tan return TAN;        //return token TAN to YACC
log return LOG;        //return token LOG to YACC
sqrt return SQRT;    //return token SQRT to YACC
[ ];
      return 0;
.       return yytext[0];

%%

%{
       #include
       #include
%}

%union               //to define possible symbol types
{ double p;}
%token

num
%token SIN COS TAN LOG SQRT

/*Defining the Precedence and Associativity*/

%left ‘+’,’-‘           //lowest precedence
%left ‘*’,’/’           //highest precedenc
%nonassoc uminu           //no associativity
%type

exp           //Sets the type for non – terminal

%%

/* for storing the answer */
ss: exp {printf(“=%g ”,$1);}

/* for binary arithmatic operators */
exp :    exp’+’exp      { $$=$1+$3; }
       |exp’-‘exp      { $$=$1-$3; }
       |exp’*’exp      { $$=$1*$3; }
       |exp’/’exp      {
                               if($3==0)
                               {
                                       printf(“Divide By Zero”);
                                       exit(0);
                               }
                               else $$=$1/$3;
                       }
       |’-‘exp         {$$=-$2;}
       |'(‘exp’)’      {$$=$2;}
       |SIN'(‘exp’)’   {$$=sin($3);}
       |COS'(‘exp’)’   {$$=cos($3);}
       |TAN'(‘exp’)’   {$$=tan($3);}
       |LOG'(‘exp’)’   {$$=log($3);}
       |SQRT'(‘exp’)’ {$$=sqrt($3);}
       |num;
%%

/* extern FILE *yyin; */
main()
{
       do
       {
               yyparse();   /* repeatedly tries to parse the                sentence until the i/p runs out */
       }while(1);

}

yyerror(s)           /* used to print the error message when an                error is parsing of i/p */

char *s;
{
       printf(“ERROR”);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote