Search code examples
bisonyacclex

BISON - How to enter a complete input?


How can I input a full statement for example x = 2 instead of doing like:

input: x *enter*
input: = *enter*
input: 2 *enter* 

?

Currently I manage to enter inputs on BISON but separately instead of just one statement. I get outputs from single inputs but when I try to enter all in one statement there is no output.

y file:

 %{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void yyerror(char *s);
int yylex(void);
int yydebug = 1;

%}




%union { char * stringValue;
        int integer;
        double real;
        char * string;

        char expresion;
 }

%start program

%token ASSIGNMENT
%token IF
%token THEN
%token END
%token PRINT
%token READ
%token REM
%token LOGICAL
%token ARITHMETIC
%token COMPARISON
%token ERRORTOKEN

%token <integer> INTNUMBER
%token <real> REALNUMBER
%token <stringValue> STRING

%token <string> INTTOKEN REALTOKEN STRINGTOKEN



%%

program:        /* empty */
                |
                program exp {;}
                ;

line:           stmt
                ;
exp:          INTTOKEN {printf("Token");}
            | INTNUMBER {printf("Number");}
            | ASSIGNMENT      {printf("Sign");}
            | INTTOKEN ASSIGNMENT INTNUMBER {printf("Assignment");}
            ;
stmt:       exp
            ;

%%

void yyerror (char *s) {fprintf (stderr, "%s\n", s);}


int main (void) {

  yydebug = 1;
  return yyparse();

}

Lex file:

    %{
#include "y.tab.h"
#include <stdio.h>
#include <string.h>

%}

%%


=                                   return ASSIGNMENT;
if|IF                       return IF;
then|THEN               return THEN;
end\.|END\.             return END;
print|PRINT             return PRINT;
read|READ                   return READ;
rem|REM                     return REM;
\.gt\.                      return COMPARISON;
\.eq\.                      return COMPARISON;
\.lt\.                      return COMPARISON;
\.ge\.                      return COMPARISON;
\.le\.                      return COMPARISON;
\.ne\.                      return COMPARISON;
\.and\.                   return LOGICAL;
\.or\.                      return LOGICAL;
\.not\.                     return LOGICAL;
\.add\.                     return ARITHMETIC;
\.mul\.                     return ARITHMETIC;
\.div\.                     return ARITHMETIC;
\.sub\.                     return ARITHMETIC;
\".*\"                      { yylval.stringValue = yytext; return STRING; }
[0-9]+                      { yylval.integer = atoi(yytext); return INTNUMBER; }
[0-9]+\.[0-9]+          { yylval.real = atoi(yytext); return REALNUMBER;}
[a-fA-F][a-zA-Z]*       { yylval.string = yytext; return INTTOKEN; }
[g-nG-N]+[a-zA-Z]*      { yylval.string = yytext; return REALTOKEN; }
[o-zO-Z]+[a-zA-Z]*      {   yylval.string = yytext; return STRINGTOKEN; }
.*                                  ;
%%

int yywrap(void) {
    return -1;
}

Solution

  • Changed the .* to . and fixed. Really messy mistake of my part.