fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. // Token definitions
  7. #define LT 256
  8. #define LE 257
  9. #define EQ 258
  10. #define NE 259
  11. #define GT 260
  12. #define GE 261
  13. #define RELOP 262
  14. #define ID 263
  15. #define NUM 264
  16. #define IF 265
  17. #define THEN 266
  18. #define ELSE 267
  19.  
  20. int attribute; // To store relational operator attribute
  21. %}
  22.  
  23. delim [\t\n ]
  24. ws {delim}+
  25. letter [A-Za-z]
  26. digit [0-9]
  27. id {letter}({letter}|{digit})*
  28. num {digit}+(\.{digit}+)?(E[+-]?{digit}+)?
  29.  
  30. %%
  31.  
  32. {ws} { /* Ignore whitespace */ }
  33. if { return(IF); }
  34. then { return(THEN); }
  35. else { return(ELSE); }
  36. {id} { return(ID); }
  37. {num} { return(NUM); }
  38. "<" { attribute = LT; return(RELOP); }
  39. "<=" { attribute = LE; return(RELOP); }
  40. "<>" { attribute = NE; return(RELOP); }
  41. "=" { attribute = EQ; return(RELOP); }
  42. ">" { attribute = GT; return(RELOP); }
  43. ">=" { attribute = GE; return(RELOP); }
  44.  
  45. %%
  46.  
  47. // Default wrap function
  48. int yywrap() {
  49. return 1;
  50. }
  51.  
  52. // Main function to test the lexer
  53. int main() {
  54. int token;
  55. while ((token = yylex())) {
  56. printf("<%d,", token);
  57. switch (token) {
  58. case ID:
  59. case NUM:
  60. printf("%s>\n", yytext); // Print lexeme for ID and NUM tokens
  61. break;
  62. case RELOP:
  63. printf("%d>\n", attribute); // Print attribute for RELOP tokens
  64. break;
  65. default:
  66. printf(")\n");
  67. break;
  68. }
  69. }
  70. return 0;
  71. }
  72.  
Success #stdin #stdout #stderr 0.03s 6980KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/Kn2rKY/prog:71:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit