tp-langages-formels/syntaxique/expr.y

43 lines
455 B
Plaintext
Raw Normal View History

2020-05-15 23:51:08 +00:00
%{
#include <stdio.h>
int yylex();
void yyerror(char *s)
{
fflush(stdout);
fprintf(stderr, "%s\n", s);
}
%}
%union {
int i;
}
%token <i> INT
%type <i> E
%left '+'
%left '*'
2020-05-15 23:51:08 +00:00
%%
S : E { printf("result=%d\n",$1); }
E : E '*' E { $$ = $1*$3; }
| E '+' E { $$ = $1+$3; }
| '(' E ')' { $$ = $2; }
| INT
%%
#include "exprlex.c"
int main (int argc, char **argv)
{
if (argc > 1) yyin = fopen(argv[1],"r");
yyparse();
}