1
votes

I want to make parser for simple calculator but I cant uderstand whu I get error for simple input. Flex file looks like this

%{
#include "exp.tab.h"
#include <string.h>
%}

blanks          [ \t\n]+


%%

{blanks}        { /* ignore */ }
[0-9]+           {yylval= strtol(yytext, NULL, 10);
return(NUMB);}

%%

Bison file looks like this:

%{
#include <stdio.h>
%}


%token NUMB


%left '+'


%%

exp:
NUMB                { $$ = $1;           }
| exp '+' exp        { $$ = $1 + $3;      }

%%
int yyerror(char *s) {
printf("yyerror : %s\n",s);
}

int main(void) {
yyparse();
}

For input

123 + 12

i get error message.Why is this happening?

1
Be aware when tagging. Flex is used for the Adobe/Apache Framework. Gnu-flex is used for the lexical analyzer.JeffryHouser
@Reboog711, flex-lexer is, actually...Charles
@Charles Thanks; I had no idea. I just modified the "Flex Tag Wiki" to make mention of the flex-lexer tag: stackoverflow.com/tags/flex/infoJeffryHouser

1 Answers

0
votes

Your lexer is missing a rule to match/return the '+' token. Try adding at the end:

.    { return *yytext; }  /* any other single character returns as itself */

The default lexer action if the text doesn't match any rule is to print it and skip it. So they error you get from the parser is because it gets 123 12 as the input, which causes a syntax error.