fork download
  1.  
  2.  
  3. However, Repl.it doesn't support the `flex` command by default. As an alternative, you can use [Ideone](https://i...content-available-to-author-only...e.com/), which supports Lex.
  4.  
  5.  
  6. Here's your updated code for Ideone:
  7.  
  8.  
  9. ```c
  10. %option noyywrap
  11.  
  12. %{
  13. #include <stdio.h>
  14. %}
  15.  
  16. %%
  17.  
  18. [id][a-zA-Z_][a-zA-Z0-9_]* {printf("%s is an identifier\n", yytext);}
  19.  
  20. for|if|else|int|float|double|char|do|while {printf("%s is a keyword\n", yytext);}
  21.  
  22. [0-9]+("[.][0-9]+")?([eE][+-]?[0-9]+)? {printf("%s is a number\n", yytext);}
  23.  
  24. \n ;
  25.  
  26. . {printf("Invalid character: %c\n", *yytext);}
  27.  
  28. %%
  29.  
  30. int main() {
  31. yylex();
  32. printf("\n\n");
  33. return 0;
  34. }
Success #stdin #stdout #stderr 0.02s 6848KB
stdin
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/* Définition des tokens */
enum TOKEN { FIN=0, PV, IF, EGAL, ASSIGN, OP, PARG, PARD, ID, NUM };

/* Pour stocker les attributs */
enum CODEOPERATION { PLUS, MOINS };

union {
    char *nom;       /* Pour ID */
    int valeur;      /* Pour NUM */
    enum CODEOPERATION cop; /* Pour OP */
} yylval;

/* Prototype de la fonction d'erreur */
void erreur_lexicale();
%}

%option yylineno

/* Définitions régulières */
blancs [ \t\n]+
lettre [A-Za-z]
chiffre [0-9]
ident {lettre}({lettre}|{chiffre}|_)*

%%
/* Ignorer espaces et commentaires */
{blancs} ;

\{[^}]*\} ;       /* commentaire entre { et } */

/* Règles lexicales */
if              { return IF; }
";"             { return PV; }
"=="            { return EGAL; }
":="            { return ASSIGN; }
[+-]            {
                    if (yytext[0] == '+') yylval.cop = PLUS;
                    else yylval.cop = MOINS;
                    return OP;
                 }
"("             { return PARG; }
")"             { return PARD; }

{chiffre}+      {
                    yylval.valeur = atoi(yytext);
                    return NUM;
                }

{ident}         {
                    yylval.nom = (char *)malloc(strlen(yytext)+1);
                    strcpy(yylval.nom, yytext);
                    return ID;
                }

/* Caractère illégal */
.               { erreur_lexicale(); }

%%

int yywrap() {
    return 1;
}

/* Fonction d'erreur */
void erreur_lexicale() {
    printf("Ligne %d : erreur lexicale, '%c' caractère illégal.\n", yylineno, yytext[0]);
    exit(1);
}

/* Fonction principale */
int main(int argc, char **argv) {
    enum TOKEN tc;

    if (argc == 2) {
        if ((yyin = fopen(argv[1], "r")) == NULL) {
            printf("Impossible d'ouvrir le fichier <%s>.\n", argv[1]);
            exit(3);
        }
    } else {
        printf("Usage : %s [nom_du_fichier]\n", argv[0]);
        exit(2);
    }

    while ((tc = yylex())) {
        switch(tc) {
            case PV:    printf("<PV, >\n"); break;
            case IF:    printf("<IF, >\n"); break;
            case EGAL:  printf("<EGAL, >\n"); break;
            case ASSIGN:printf("<ASSIGN, >\n"); break;
            case OP:    printf("<OP, %s>\n", yylval.cop==PLUS?"PLUS":"MOINS"); break;
            case PARG:  printf("<PARG, >\n"); break;
            case PARD:  printf("<PARD, >\n"); break;
            case ID:    printf("<ID, %s>\n", yylval.nom); break;
            case NUM:   printf("<NUM, %d>\n", yylval.valeur); break;
        }
    }

    printf("<FIN, >\n");
    return 0;
}
stdout
Standard output is empty
stderr
ERROR: /home/SZSyx6/prog:3:1: Syntax error: End of file in quoted codes
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ?