fork download
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int num1 = -1, num2 = -1;
  6. %}
  7.  
  8. %%
  9.  
  10. [0-9]+ {
  11. if (num1 == -1) {
  12. num1 = atoi(yytext);
  13. } else {
  14. num2 = atoi(yytext);
  15. }
  16. }
  17.  
  18. "+" {
  19. ; // This simply matches the plus sign, no action needed
  20. }
  21.  
  22. \n {
  23. if (num1 != -1 && num2 != -1) {
  24. printf("Sum: %d\n", num1 + num2);
  25. num1 = -1;
  26. num2 = -1;
  27. } else {
  28. printf("Error: Two numbers required for addition.\n");
  29. }
  30. }
  31.  
  32. . { ; }
  33.  
  34. %%
  35.  
  36. int main() {
  37. yylex();
  38. return 0;
  39. }
  40.  
Success #stdin #stdout #stderr 0.03s 6864KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/7tMbhn/prog:2:1: Syntax error: Operator expected
ERROR: /home/7tMbhn/prog:39:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit