私は .gertrude esolang に基づいて簡単な電卓を書いています。私がやろうとしているのは、比率が操作のインデックス(+ - / *)または数値であるかどうかを確認し、送信するよりも、比率(n / mの形式)を含むテキストファイルをフレックスで解析することですバイソンへの正しいトークン。コードのコンパイル時にはエラーは発生しませんが、プログラムの実行時に、すべての種類の入力 (2 + 8 である必要がある 1/2 14/10 1/8 など) に対して、セグメンテーション エラー コア ダンプが返されます。
こちら gertrude.l
%{
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "gertrude.tab.h"
void yyerror(char *);
int FrazioneToDecimale(char *str1){
int num, den;
unsigned tot;
char *token;
char *deli;
const char del = '/';
*deli = del;
token = strtok (str1, deli);
num = atoi(token);
token = strtok (NULL, deli);
den = atoi(token);
tot = 1 / (num/den);
return tot;
}
%}
%%
/* ratio */
"14/10" {
yylval.sval = '+';
return SOMMA;
}
"11/7" {
yylval.sval = '-';
return SOTTRAZIONE;
}
"6/16" {
yylval.sval = '*';
return MOLTIPLICAZIONE;
}
"5/8" {
yylval.sval = '/';
return DIVISIONE;
}
[0-9]+"/"[0-9]+ {
//yylval = *yytext ;
yylval.ival = FrazioneToDecimale(yytext);
return NUMERO;
}
[ \t] ;
[ \n] { return EOL; };
%%
int yywrap(void) {
return 0;
}
ここで gertrude.y
%{
#include <stdio.h>
#include <string.h>
%}
%union {
int ival;
char sval;
}
%type <ival> exp fattore termine
%token <ival> NUMERO
%token <sval> SOMMA SOTTRAZIONE MOLTIPLICAZIONE DIVISIONE
%token EOL
%%
istruzione:
| istruzione exp EOL { printf("= %d\n", $2); }
;
exp: fattore
| exp SOMMA fattore { $$ = $1 + $3; }
| exp SOTTRAZIONE fattore { $$ = $1 - $3; }
;
fattore: termine
| fattore MOLTIPLICAZIONE termine { $$ = $1 * $3; }
| fattore DIVISIONE termine { $$ = $1 / $3; }
;
termine: NUMERO { $$ = $1; }
;
%%
int main(void) {
yyparse();
}
yyerror(char *s) {
fprintf(stderr, "error: %s\n\n", s);
}
どんな種類のアドバイスでも事前に感謝します!