Display message after user input in flex

I have the following code snippet that I wrote in flex. I need to display this message:

{printf("\n%-20s%-30s%-10s\n", "Lexeme", "Unite lexicale", "Indice");}

The first thing that happened after user input , I tried to find a solution, but nothing worked.

%{
int i=1;
%}
lettre [a-zA-Z]+
nombre_entier (\+|\-)?[0-9]+
nombre_reel (\+|\-)?[0-9]+\.[0-9]+((e|E)(\-|\+)?[0-9]+)?
id {lettre}({lettre}|[0-9])*
%%
\$              { exit(0);}
[ \t]+          {/*ignorer*/}
\n              {i=1;}
ENTIER|REEL     {printf("%-20s%-30s%-10d\n",yytext, "Mot_cle", i++);
                 printf("-----------------------------------------------------\n");}
{id}            {printf("%-20s%-30s%-10d\n",yytext, "ID", i++);
                 printf("------------------------------------------------------\n");}
{nombre_entier} {printf("%-20s%-30s%-10d\n",yytext, "nombre entier", i++);
                 printf("------------------------------------------------------\n");}
{nombre_reel}   {printf("%-20s%-30s%-10d\n",yytext, "nombre reel", i++);
                 printf("------------------------------------------------------\n");}
\(              {printf("%-20s%-30s%-10d\n",yytext, "parenthese ouvrante", i++);
                 printf("------------------------------------------------------\n");}
")"             {printf("%-20s%-30s%-10d\n",yytext, "parenthese fermante", i++);
                 printf("------------------------------------------------------\n");}
"+"|"-"|"*"|"/" {printf("%-20s%-30s%-10d\n",yytext, "operateur arithmetique", i++);
                 printf("------------------------------------------------------\n");}
"="             {printf("%-20s%-30s%-10d\n",yytext, "operateur d'affectation", i++);
                 printf("------------------------------------------------------\n");}
","             {printf("%-20s%-30s%-10d\n",yytext, "Virgule", i++);
                 printf("------------------------------------------------------\n");}
";"             {printf("%-20s%-30s%-10d\n",yytext, "Point virgule", i++);
                 printf("------------------------------------------------------\n");}
.               {printf("%-20s%-30s%-10d\n",yytext, "caractere inconnu", i++);
                 printf("------------------------------------------------------\n");}
%%
int main(){
    printf("Entrez le texte a analyser : \n");
    yylex();
    return 0;
}
int yywrap(){
    return 1;
}

Please, help.

+4
source share
2 answers

A cleaner solution is to use the scanner in the flexintended way, which is to consistently return lexical tokens to your caller, one token per call.

, - , . (, , #define s), , , . , yacc bison, .

- " " . , , . yytext ( ), yytext flex - , , yytext , , , . , yylex.

:

: tokens.h

enum Token {
  T_FIN = 0,
  T_MOTCLE,
  T_ID,
  T_ENTIER,
  T_REEL,
  T_OUVRANTE,
  T_FERMANTE,
  T_OPERATEUR,
  T_AFFECT,
  T_VIRGULE,
  T_POINT_VIRGULE,
  T_INCONNU
};

const char* decrire(int jeton);

- enum . , . - . , 0 , .

: tokens.c

#include <stdio.h>
#include "tokens.h"

static const char* descriptions = {
  "Fin d'entree",
  "Mot_cle",
  "ID",
  "Nombre entier",
  "Nombre reel",
  "Parenthese ouvrante",
  "Parenthese fermante"
  "Operateur arithmetique",
  "Operateur d'affectation",
  "Virgule",
  "Point virgule",
  "Caractere inconnu"
};

const char* decrire(int jeton) {
  if (jeton >= 0 && jeton <= T_INCONNU)
    return descriptions[jeton];
  else
    return "???";  /* This indicates a bug somewhere */
}

, :

int main() {
  puts("Entrez le texte a analyser : ");
  int jeton = yylex();
  printf("\n%-20s%-30s%-10s\n", "Lexeme", "Unite lexicale", "Indice");
  puts("-----------------------------------------------------");
  for (int i = 1; jeton; jeton = yylex();) {
    printf("%-20s%-30s%-10d\n", yytext, decrire(jeton), token_count++);
    puts("------------------------------------------------------");
  }
  return 0;
}

, :

tokens.l

%{
  #include "tokens.h"
  int token_count;
%}

%options noinput nounput noyywrap nodefault

%%
"$"                     { return T_FIN; }
[ \t]+                  { /*ignorer*/ }
\n                      { token_count = 1; }
ENTIER|REEL             { return T_MOTCLE; }
[[:alpha:]][[:alnum:]]* { return T_ID; }
[+-]?[[:digit:]]*       { return T_ENTIER; }
[+-]?[[:digit:]]+\.[[:digit:]]+([eE][+-]?[[:digit:]]*)? {
                          return T_REEL; }
"("                     { return T_OUVRANTE; }
")"                     { return T_FERMANTE; }
[-+*/]                  { return T_OPERATEUR; }
"="                     { return T_AFFECT; }
","                     { return T_VIRGULE; }
";"                     { return T_POINT_VIRGULE; }
.                       { return T_INCONNU; }

( ) ; yylloc ( ) , .

+2

@DYZ

%{
int i=1,j=0;
#define YY_INPUT(buf,result,max_size) \
    { \
    int c = getchar(); \
    result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \
    if(j++ == 0) \
        { \
        printf("\n%-20s%-30s%-10s\n", "Lexeme", "Unite lexicale", "Indice"); \
        printf("-----------------------------------------------------\n"); \
        } \
    }
%}
0

Source: https://habr.com/ru/post/1668321/


All Articles