fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int number = 0;
  5. int is_prime(int n) {
  6. if (n <= 1) return 0; // Numbers less than or equal to 1 are not prime
  7. for (int i = 2; i * i <= n; i++) {
  8. if (n % i == 0) return 0; // If divisible, not a prime number
  9. }
  10. return 1; // It's prime
  11. }
  12. %}
  13.  
  14. %%
  15. [0-9]+ { number = atoi(yytext); } // Capture the number entered
  16. .|\n { } // Ignore other characters or newlines
  17. %%
  18.  
  19. int main() {
  20. printf("Enter a number: ");
  21. yylex(); // Start Lex scanning
  22. if (is_prime(number)) {
  23. printf("%d is a prime number.\n", number);
  24. } else {
  25. printf("%d is not a prime number.\n", number);
  26. }
  27. return 0;
  28. }
  29.  
Success #stdin #stdout #stderr 0.03s 6868KB
stdin
2
stdout
Standard output is empty
stderr
ERROR: /home/ypNwoD/prog:2:1: Syntax error: End of file in quoted atom
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit