fork download
  1. %{
  2. int COMMENT = 0; // Flag to track if inside a comment
  3. %}
  4.  
  5. identifier [a-zA-Z][a-zA-Z0-9]* // Regular expression for identifiers
  6.  
  7. %%
  8.  
  9. #.* {
  10. // Matches preprocessor directives
  11. printf("\n%s is a preprocessor directive", yytext);
  12. }
  13.  
  14. int |
  15. char |
  16. double |
  17. while |
  18. for |
  19. struct |
  20. typedef |
  21. do |
  22. if |
  23. break |
  24. continue |
  25. void |
  26. switch |
  27. return |
  28. else |
  29. goto {
  30. // Matches keywords
  31. printf("\n\t%s is a keyword", yytext);
  32. }
  33.  
  34. "/*" {
  35.   // Start of a comment
  36.   COMMENT = 1;
  37.   printf("\n\t%s is a COMMENT", yytext);
  38. }
  39.  
  40. "*/" {
  41. // End of a comment
  42. COMMENT = 0;
  43. printf("\n\t%s is a COMMENT END", yytext);
  44. }
  45.  
  46. {identifier}\( {
  47. // Function definition (identifier followed by '(')
  48. if (!COMMENT) {
  49. printf("\nFUNCTION \n\t%s", yytext);
  50. }
  51. }
  52.  
  53. \{ {
  54. // Block starts
  55. if (!COMMENT) {
  56. printf("\n BLOCK BEGINS");
  57. }
  58. }
  59.  
  60. \} {
  61. // Block ends
  62. if (!COMMENT) {
  63. printf("BLOCK ENDS ");
  64. }
  65. }
  66.  
  67. {identifier}(\[[0-9]*\])? {
  68. // Identifiers, with optional array index
  69. if (!COMMENT) {
  70. printf("\n %s IDENTIFIER", yytext);
  71. }
  72. }
  73.  
  74. \"[^\"]*\" {
  75. // Strings, allows escape sequences (not yet fully handling all escape sequences)
  76. if (!COMMENT) {
  77. printf("\n\t %s is a STRING", yytext);
  78. }
  79. }
  80.  
  81. [0-9]+ {
  82. // Numbers
  83. if (!COMMENT) {
  84. printf("\n %s is a NUMBER ", yytext);
  85. }
  86. }
  87.  
  88. \)(\:)? {
  89. // Matches ')' followed by an optional ':'
  90. if (!COMMENT) {
  91. printf("\n\t");
  92. }
  93. ECHO; // Print the token
  94. printf("\n");
  95. }
  96.  
  97. \( {
  98. // Matches opening '('
  99. ECHO;
  100. }
  101.  
  102. = {
  103. // Assignment operator '='
  104. if (!COMMENT) {
  105. printf("\n\t %s is an ASSIGNMENT OPERATOR", yytext);
  106. }
  107. }
  108.  
  109. \<= |
  110. \>= |
  111. \< |
  112. == |
  113. \> |
  114. != {
  115. // Relational operators
  116. if (!COMMENT) {
  117. printf("\n\t%s is a RELATIONAL OPERATOR", yytext);
  118. }
  119. }
  120.  
  121. %%
  122.  
  123. int main(int argc, char **argv) {
  124. FILE *file;
  125. file = fopen("var.c", "r");
  126.  
  127. if (!file) {
  128. printf("could not open the file");
  129. exit(0);
  130. }
  131.  
  132. yyin = file; // Use file input
  133. yylex(); // Start lexical analysis
  134. printf("\n");
  135. return(0);
  136. }
  137.  
  138. int yywrap() {
  139. return(1); // End of input
  140. }
  141.  
Success #stdin #stdout #stderr 0.04s 6868KB
stdin
#include <stdio.h>
int main() {
    int x = 10;
    if (x > 5) {
        printf("x is greater than 5");
    }
}
stdout
Standard output is empty
stderr
ERROR: /home/TpFlt1/prog:141:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Can't ignore goal at this port
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: Can't ignore goal at this port
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ?    Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? ERROR: Unknown option (h for help)
   Exception: (3) program ? EOF: exit