0

The code is for study bison and flex, I am new but I want to learn to handle them.

I'm trying to run this code but I don't know where the mistake is because the compiler says it is ok, but I don't know why there is the error when I test with the number +573002597643. It is a number that's valid because I tried taking care of my rules.

My code:

bison:

%{
#include <stdio.h>
#include <stdlib.h>
extern int yylex();
extern int yyparse();
extern FILE* yyin;
void yyerror(const char* s);
%}

%union {
    int ival;
    float fval;
}

%token S_COL
%token<ival> I_AUX I_REGFIJO I_REGCEL I_NUMFIJO
%token T_NEWLINE T_QUIT


%start calculation

%%

calculation: T_NEWLINE
    | S_COL I_REGCEL I_AUX I_NUMFIJO{ printf("Number phone: \n"); }
    | S_COL I_REGFIJO I_REGFIJO I_NUMFIJO{ printf("Number local phone: \n"); }
    | T_QUIT T_NEWLINE { printf("bye!\n"); exit(0); }
;



%%
int main() {
    yyin = stdin;
    do { 
        yyparse();
    } while(!feof(yyin));
    return 0;
}
void yyerror(const char* s) {
    fprintf(stderr, "Parse error: %s\n", s);
    exit(1);
}

flex file

%option noyywrap

%{
#include <stdio.h>

#define YY_DECL int yylex()

#include "tel.tab.h"

%}

%%

[ \t]   ; // ignore all whitespace
\n  {return T_NEWLINE;}
[1-9]   {return I_REGFIJO;}
[0-9]   {return I_AUX;}
[3][0-9][0-9]   {return I_REGCEL;}
[\+][5][7]  {return S_COL;}
[0-9][0-9][0-9][0-9][0-9][0-9]  {return I_NUMFIJO;}
"exit"      {return T_QUIT;}
"quit"      {return T_QUIT;}


%%
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

Note that with input that matches more than one pattern, such as 1 for

[1-9]   {return I_REGFIJO;}
[0-9]   {return I_AUX;}

(and the same length) only the first action is taken. You'll only ever see an I_AUX for a lone 0. If more than one pattern of different length is matched, the action for the longest match is taken (aka "maximum munch rule"). Other than that, I have trouble understanding what you need. Maybe you can rephrase the question to be clearer?

Jens
  • 69,818
  • 15
  • 125
  • 179