I created a small interpreter using flex/bison.
This just can print a number, but I want to know how can add a string print?
lexer :
%{
#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 );
}
test data :
show 5;
show 5+5;
show 5*2-5+1;
I want add string code to the lexer :
<INITIAL>\" {BEGIN(STRING);}
<STRING>\" {BEGIN(INITIAL);}
Now how to use from content of in <STRING>
?
Can you help me to complete my interpreter?
I need to add this examples to my interpreter :
show "hello erfan";//hello erfan
show "hello ".5;//hello 5
Please help me.