Vous êtes sur la page 1sur 1

1) create file with below content with ."y" extension /* declarations */ %{ #include <ctype.h> #include <stdio.

h> void yyerror (char *s); #define YYSTYPE double %} %token NUMBER %left '+' '-' %left '*' '/' %right UMINUS %% /* translation rules */ lines : lines expr '\n' { printf("%g\n", $2); } | lines '\n' | ; expr : expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | expr '*' expr { $$ = $1 * $3; } | expr '/' expr { $$ = $1 / $3; } | '(' expr ')' { $$ = $2; } | '-' expr %prec UMINUS { $$ = - $2; } | NUMBER ; %% yylex() { int c; while ( ( c = getchar() ) == ' ' ); if ( (c == '.') || (isdigit(c)) ) { ungetc(c, stdin); scanf("%lf", &yylval); return NUMBER; } return c; } void yyerror (char *s) { ; } 2) run below commands in terminal yacc translate1.y gcc y.tab.c -ly -ll -o translate1 ./translate1

Vous aimerez peut-être aussi