flex/bisonを使用して小さなインタプリタを作成しました。Flex/Bisonの文字列を出力するインタプリタを作成する方法
これはちょうど数字を印刷することができますが、どのように文字列プリントを追加できるのか知りたいですか?
レクサー:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "y.tab.h"
%}
%%
<INITIAL>[s|S][h|H][o|O][w|W] {return show;}
<INITIAL>[0-9a-zA-z]+ {yylval.num=atoi(yytext);return string;}
<INITIAL>[\-\+\=\;\*\/] {return yytext[0];}
%%
int yywrap (void) {return 1;}
YACC:
%{
void yyerror(char *s);
#include <stdio.h>
#include <stdlib.h>
%}
%union {int num;}
%start line
%token show
%token <num> number
%type <num> line exp term
%%
line : show exp ';' {printf("showing : %d\n",$2);}
| line show exp ';' {printf("showing : %d\n",$3);}
;
exp : term {$$ = $1;}
| exp '+' term {$$ = $1 + $3;}
| exp '-' term {$$ = $1 - $3;}
| exp '*' term {$$ = $1 * $3;}
| exp '/' term {$$ = $1/$3;}
;
term : number {$$ = $1;}
%%
int main (void)
{
return yyparse();
}
void yyerror (char *s)
{
printf("-%s at %s !\n",s);
}
試験データ:
show 5;
show 5+5;
show 5*2-5+1;
私はレクサーに文字列のコードを追加します:
<INITIAL>\" {BEGIN(STRING);}
<STRING>\" {BEGIN(INITIAL);}
今どのようにcontent of in <STRING>
から使用するには?
通訳を完了するのに手伝ってもらえますか?
私は通訳に、この例を追加する必要があります。
show "hello erfan";//hello erfan
show "hello ".5;//hello 5
私を助けてください。
同じ(物理的な)行に複数の文字列がある入力でテストしましたか? – rici
@rici - ああそう!貪欲な試合....よく目撃された。 –
@rici固定... :-) –