Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- //YACC with LEX
- #include<stdio.h>
- #include<stdlib.h>
- int yylex(void);
- void yyerror(char*);
- %}
- %start S
- %token L R NUM
- %left LOGICAL_AND LOGICAL_OR LOGICAL_NOT LOGICAL_EXOR
- %left REL_EQ REL_GE REL_LT REL_GT REL_LE
- %left BIT_AND BIT_OR BIT_EXOR
- %left SHIFT_LEFT SHIFT_RIGHT
- %%
- S : E { $$ = $1; printf("\n Result: %d", $$); }
- E : E LOGICAL_AND E { $$ = $1&&$3; printf("\n %d", $$);}
- | E LOGICAL_OR E { $$ = $1||$3; printf("\n %d", $$);}
- | LOGICAL_NOT E { $$ = ~$2; printf("\n %d", $$);}
- | E LOGICAL_EXOR E { $$ = $1^$3; printf("\n %d", $$);}
- | E BIT_AND E { $$ = $1&$3; printf("\n %d", $$);}
- | E BIT_OR E { $$ = $1|$3; printf("\n %d", $$);}
- | E BIT_EXOR E { $$ = $1^$3; printf("\n %d", $$);}
- | E REL_EQ E { $$ = $1==$3; printf("\n %d", $$);}
- | E REL_GE E { $$ = $1>=$3; printf("\n %d", $$);}
- | E REL_LE E { $$ = $1<=$3; printf("\n %d", $$);}
- | E REL_GT E { $$ = $1>$3; printf("\n %d", $$);}
- | E REL_LT E { $$ = $1<$3; printf("\n %d", $$);}
- | E SHIFT_LEFT E { $$ = $1<<$3; printf("\n %d", $$);}
- | E SHIFT_RIGHT E { $$ = $1>>$3; printf("\n %d", $$);}
- | L E R {$$ = $2;}
- | NUM {$$ = $1;}
- %%
- int main()
- {
- return (yyparse());
- return 0;
- }
- void yyerror(char *S)
- {
- fprintf(stderr, "%s\n", S);
- }
- yywrap()
- {
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement