9. Program in YACC to evaluate an expression (simple calculator program for addition and subtraction, multiplication, division)

 

Program in YACC to evaluate an expression (simple calculator program for addition and subtraction, multiplication, division)

/*Program in YACC to evaluate an expression (simple calculator program for addition and
subtraction, multiplication, division).*/
File Name : calc10.y
%{
 #include<stdio.h>
int yylex(void);
int yyerror(char *);
%}

%token NAME NUMBER

%%
statement : NAME '=' expression
 | expression { printf("=%d\n",$1);}
 ;
expression:expression'+' NUMBER{$$ = $1+$3;}
 |expression '-' NUMBER {$$ = $1-$3;}
 |expression '*' NUMBER {$$ = $1*$3;}
 |expression '/' NUMBER { if ($3!=0){ $$ = $1/$3; }else { printf("Error: divide by Zero"); } }
 |NUMBER {$$=$1;}
 ;

%%
int main()
{
yyparse();
return 0;
}
int yyerror(char *s)
{
 printf("%s",s);
}

File Name : calc10.l
%{
 #include "y.tab.h"
 extern int yylval;
%}

%%
[0-9]+ { yylval=atoi(yytext);return NUMBER;}
\n  {return 0;}
.   {return yytext[0];}
%%

// Output of the Above Program.
Program in YACC to evaluate an expression (simple calculator program for addition and subtraction, multiplication, division)
Program in YACC to evaluate an expression (simple calculator program for addition and
subtraction, multiplication, division)

Comments

  1. Struggling with coding concepts, debugging, or complex coursework? Professional academic guidance can help you understand programming fundamentals, improve problem-solving skills, and approach challenging projects with confidence. Students seeking programming assignment help can benefit from clear explanations, practical examples, and structured guidance across languages, algorithms, and data structures. Choose reliable support that focuses on learning, originality, and timely assistance to strengthen your programming knowledge and complete academic tasks more effectively.

    ReplyDelete

Post a Comment

Popular posts from this blog

Compiler Design

7. Lex specification program that generates a C program which takes a string “abcd” and prints the pattern

10. Program in YACC to recognize the string „abbb^n, „ab^n „a^n of the langauge (a^n b^n , n>=1)