0
votes

The program generated from my bison/flex files works fine for the first input(the first run of yyparse()), but throws an error when I try and run it a second time.

for example:

chris@chris-Inspiron-1525:~/4850/hw$ ./calc
HORN 3+4*6-11+33*2 ?
=   82
HORN 1+1 ?
error: syntax error

Basically, commands exist between 'HORN' and '?'

Here's my .y file:

%{ 
#include <stdlib.h>
#include <stdio.h>
void yyerror(const char *str);


%}

%union 
{
    int ival; 
    float fval;
    char* word;
}
%start line
%type <word> wexp
%type <ival> iexp
%type <fval> fexp

%left PLUS MINUS
%left STAR DIV MOD
%left POW
%token GT
%token LT
%token ASGN
%token LP
%token RP
%token LB
%token RB
%token NOT
%token GTEQ
%token LTEQ
%token EQTO
%token NOTEQ
%token HORN
%token QMARK
%token <word> WORD
%token <fval> FLOAT
%token <ival> INTEGER


%%

line    : HORN wexp QMARK   {   printf("=\t%s\n", $2);  }
        | HORN iexp QMARK   {   printf("=\t%d\n", $2);  }
        | HORN fexp QMARK   {   printf("=\t%f\n", $2);  }
        ;

iexp    : iexp MINUS iexp   { $$ = $1 - $3;             }
        | iexp PLUS iexp    { $$ = $1 + $3;             }
        | iexp STAR iexp    { $$ = $1 * $3;             }
        | iexp DIV iexp     { $$ = $1 / $3;             }
        | INTEGER           { $$ = $1;                  }
        ;

fexp    : FLOAT             { $$ = $1;                  }
        ;

wexp    : WORD              { $$ = $1;                  }
        ;
%%


int main(){
     yyparse();
}

void yyerror(const char *str)
{
        printf("error: %s\n",str);
}

Thanks for any input!

1
syntax need for newline.BLUEPIXY
@BLUEPIXY: I have that accounted for in my .l flex file as the regex [ \t\n] {;}Chris Phillips
You should use flex-lexer tag instead of flex. I have edited this for you.Robin van den Bogaard

1 Answers

1
votes

Your yacc grammar only declare one line. When you have finished one line any following input is a syntax error.

The current way to allow as many lines as you need is to add as first rule something like :

lines : line
      | lines line
      ;

Provided you correctly ignore end of line in the lex part ...