Skip to content

Instantly share code, notes, and snippets.

@dntf-studio
Created May 9, 2021 03:20
Show Gist options
  • Save dntf-studio/959e9510e2736b584bb36c27f5d3a91a to your computer and use it in GitHub Desktop.
Save dntf-studio/959e9510e2736b584bb36c27f5d3a91a to your computer and use it in GitHub Desktop.
小数あり四則演算。変数なし。
%{
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
double y;
%}
%union{
int ival;
float fval;
double dval;
}
%token LF
%token <dval> NUMBER
%token PLUS MINUS DIV MULTI
%type <dval> list expr input
%left '+' '-'
%left '*' '/'
%%
input : /* empty */
| input list
;
list : expr LF {
y = $1;
printf("r>%g\n",y);
}
expr : NUMBER
| expr '+' expr {$$ = $1 + $3;}
| expr '-' expr {$$ = $1 - $3;}
| expr '*' expr {$$ = $1 * $3;}
| expr '/' expr {$$ = $1 / $3;}
;
%%
int main(void){
//yyparse();
if(yyparse()){
printf("Err:%s",stderr);
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment