File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments