fork download
  1. %{
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6.  
  7. #define MAX_TOKEN_LENGTH 100
  8.  
  9. // Token types
  10. typedef enum {
  11. MOT_CLE,
  12. INT,
  13. IDENTIFICATEUR,
  14. OPERATEUR,
  15. SEPARATEUR,
  16. CHAINE_CARACTERE,
  17. ERROR
  18. } TokenType;
  19.  
  20. // Keywords list
  21. const char* keywords[] = {"si", "sino", "is", "tq", "qt", "rpt", "jsq", "lre", "ecr", "vrai", "faux"};
  22. int num_keywords = sizeof(keywords) / sizeof(keywords[0]);
  23.  
  24. // Check if a token is a keyword
  25. bool is_keyword(const char* token) {
  26. for (int i = 0; i < num_keywords; i++) {
  27. if (strcasecmp(token, keywords[i]) == 0) {
  28. return true;
  29. }
  30. }
  31. return false;
  32. }
  33.  
  34. // Write token to output file
  35. void process_token(TokenType type, int line_number, const char* token) {
  36. switch (type) {
  37. case MOT_CLE:
  38. fprintf(yyout, "Mot_Cle %d %s\n", line_number, token);
  39. break;
  40. case INT:
  41. fprintf(yyout, "Int %d %s\n", line_number, token);
  42. break;
  43. case FLOAT:
  44. fprintf(yyout, "Float %d %s\n", line_number, token);
  45. break;
  46. case IDENTIFICATEUR:
  47. fprintf(yyout, "Identificateur %d %s\n", line_number, token);
  48. break;
  49. case OPERATEUR:
  50. fprintf(yyout, "Operateur %d %s\n", line_number, token);
  51. break;
  52. case SEPARATEUR:
  53. fprintf(yyout, "Separateur %d %s\n", line_number, token);
  54. break;
  55. case CHAINE_CARACTERE:
  56. fprintf(yyout, "Chaine_Caracteres %d %s\n", line_number, token);
  57. break;
  58. case ERROR:
  59. fprintf(yyout, "ERROR %d %s\n", line_number, token);
  60. break;
  61. }
  62. }
  63.  
  64. int line_number = 1;
  65. %}
  66.  
  67. %%
  68.  
  69. "//".* { /* Skip single-line comments */
  70. // Do nothing
  71. }
  72.  
  73. "/*" { fprintf(yyout, "ERROR %d Début de commentaire multiligne\n", line_number);
  74.   bool in_multiline_comment = true;
  75.   while (in_multiline_comment) {
  76.   int token = yylex();
  77.   if (token == 0) {
  78.   fprintf(yyout, "ERROR %d Fin de fichier dans un commentaire\n", line_number);
  79.   return;
  80.   }
  81.   if (yytext[0] == '*' && yytext[1] == '/') {
  82.   in_multiline_comment = false;
  83.   fprintf(yyout, "ERROR %d Fin de commentaire multiligne\n", line_number);
  84.   }
  85.   }
  86.   }
  87.  
  88. "\""[^"\\]*(\\.[^"\\]*)*"\" { /* Handle string literals */
  89. yytext[yyleng - 1] = '\0'; // Remove trailing quote
  90. process_token(CHAINE_CARACTERE, line_number, yytext + 1); // Skip leading quote
  91. }
  92.  
  93. [0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)? { /* Handle integers and floats */
  94. process_token(FLOAT, line_number, yytext);
  95. }
  96.  
  97. [a-zA-Z][a-zA-Z0-9]* { /* Handle identifiers and keywords */
  98. if (is_keyword(yytext)) {
  99. process_token(MOT_CLE, line_number, yytext);
  100. } else {
  101. process_token(IDENTIFICATEUR, line_number, yytext);
  102. }
  103. }
  104.  
  105. [+\-*/=<>!&|:] { /* Handle operators */
  106. if (strcmp(yytext, "+") == 0 || strcmp(yytext, "-") == 0 ||
  107. strcmp(yytext, "*") == 0 || strcmp(yytext, "/") == 0 ||
  108. strcmp(yytext, "=") == 0 || strcmp(yytext, "<") == 0 ||
  109. strcmp(yytext, ">") == 0 || strcmp(yytext, "!") == 0 ||
  110. strcmp(yytext, "&") == 0 || strcmp(yytext, "|") == 0 ||
  111. strcmp(yytext, ":") == 0) {
  112. process_token(OPERATEUR, line_number, yytext);
  113. } else if (strcmp(yytext, "==") == 0 || strcmp(yytext, "!=") == 0 ||
  114. strcmp(yytext, "<=") == 0 || strcmp(yytext, ">=") == 0 ||
  115. strcmp(yytext, "&&") == 0 || strcmp(yytext, "||") == 0) {
  116. process_token(OPERATEUR, line_number, yytext);
  117. }
  118. }
  119.  
  120. [;,()] { /* Handle separators */
  121. process_token(SEPARATEUR, line_number, yytext);
  122. }
  123.  
  124. [ \t\r]+ { /* Skip whitespace */
  125. // Do nothing
  126. }
  127.  
  128. \n { /* Increment line number */
  129. line_number++;
  130. }
  131.  
  132. . { /* Handle invalid characters */
  133. process_token(ERROR, line_number, yytext);
  134. }
  135.  
  136. %%
  137.  
  138. int main(int argc, char** argv) {
  139. if (argc > 1) {
  140. yyin = fopen(argv[1], "r");
  141. if (!yyin) {
  142. perror("Error opening input file");
  143. return 1;
  144. }
  145. }
  146.  
  147. yyout = fopen("output.txt", "w");
  148. if (!yyout) {
  149. perror("Error opening output file");
  150. if (yyin) fclose(yyin);
  151. return 1;
  152. }
  153.  
  154. yylex();
  155.  
  156. if (yyin) fclose(yyin);
  157. if (yyout) fclose(yyout);
  158.  
  159. return 0;
  160. }
Success #stdin #stdout #stderr 0.03s 6852KB
stdin
int x = 10;


float y = 3.14; // Initialisation de y
char str[] = "Ceci est une chaîne de caractères avec un saut de ligne \n et un tabulation \t";

/* Ceci est un
   commentaire multiligne */
stdout
Standard output is empty
stderr
ERROR: /home/xy8H8b/prog:161:0: Syntax error: end_of_file_in_quasi_quotation
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? [Illegal port specification]
   Exception: (3) program ? EOF: exit