Skip to content

Commit 281f764

Browse files
Create 1b.c
1 parent 6628880 commit 281f764

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

1b.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
%{
2+
#include "y.tab.h"
3+
extern yylval;
4+
%}
5+
%%
6+
[0-9]+ {yylval=atoi(yytext);return num;}
7+
[\+\-\*\/] {return yytext[0];}
8+
[)] {return yytext[0];}
9+
[(] {return yytext[0];}
10+
. {;}
11+
\n {return 0;}
12+
%%
13+
------------YACC Part----------
14+
%{
15+
#include<stdio.h>
16+
#include<stdlib.h>
17+
%}
18+
%token num
19+
%left '+' '-'
20+
%left '*' '/'
21+
%%
22+
input:exp {printf("%d\n",$$);exit(0);}
23+
exp:exp'+'exp {$$=$1+$3;}
24+
|exp'-'exp{$$=$1-$3;}
25+
|exp'*'exp{$$=$1*$3;}
26+
|exp'/'exp { if($3==0){printf("Divide by Zero\n");exit(0);}
27+
else
28+
$$=$1/$3;}
29+
|'('exp')'{$$=$2;}
30+
|num{$$=$1;};
31+
%%
32+
int yyerror()
33+
{
34+
printf("error");
35+
exit(0);
36+
}
37+
38+
int main()
39+
{
40+
printf("Enter an expression:\n");
41+
yyparse();
42+
}

0 commit comments

Comments
 (0)