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

According to the following context-free grammar, write a yacc file as a integer

ID: 3679163 • Letter: A

Question

According to the following context-free grammar, write a yacc file as a integer
calculator that can do +, -, *, /, and ^ with or without ( ).
command expr
expr expr + term | expr – term | term
term term * term2 | term / term2 | term2 | -factor
term2 factor ^ term2 | factor
factor ( expr ) | NUMBER
You may find a sample yacc file named calculator.y

___________________________________________

To compile your calculator.y, you type yacc calculator.y. It will create y.tab.c
file for you. You need to compile this c program using gcc.

In order to run calculator on real numbers, you may
add the following in the definition section and make other necessary changes
accordingly:
%token NUMBER
%union { double val;
char op; }
%type <val> exp term factor NUMBER
%type <op> '+' '-' '*' '/' '(' ')'
%%

----------------------------

Calculate.y

%{
#include <stdio.h>
#include <ctype.h>
%}

%token NUMBER

%%

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

exp   : exp '+' term   {$$ = $1 + $3;}
   | exp "-" term   {$$ = $1 - $3;}
   | 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