fork download
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4. int operators_count = 0, operands_count = 0, valid = 1, top = -1, l = 0, j = 0;
  5. char operands[10][10], operators[10][10], stack[100];
  6. %}
  7. %%
  8. "(" {
  9. top++;
  10. stack[top] = '(';
  11. }
  12. "{" {
  13. top++;
  14. stack[top] = '{';
  15. }
  16. "[" {
  17. top++;
  18. stack[top] = '[';
  19. }
  20. ")" {
  21. if (stack[top] != '(') {
  22. valid = 0;
  23. }
  24. else if(operands_count>0 && (operands_count-operators_count)!=1){
  25. valid=0;
  26. }
  27. else{
  28. top--;
  29. operands_count=1;
  30. operators_count=0;
  31. }
  32. }
  33. "}" {
  34. if (stack[top] != '{') {
  35. valid = 0;
  36. }
  37. else if(operands_count>0 && (operands_count-operators_count)!=1){
  38. valid=0;
  39. }
  40. else{
  41. top--;
  42. operands_count=1;
  43. operators_count=0;
  44. }
  45. }
  46. "]" {
  47. if (stack[top] != '[') {
  48. valid = 0;
  49. }
  50. else if(operands_count>0 && (operands_count-operators_count)!=1){
  51. valid=0;
  52. }
  53. else{
  54. top--;
  55. operands_count=1;
  56. operators_count=0;
  57. }
  58.  
  59. }
  60. "+"|"-"|"*"|"/" {
  61. operators_count++;
  62. strcpy(operators[l], yytext);
  63. l++;
  64. }
  65. [0-9]+|[a-zA-Z][a-zA-Z0-9_]* {
  66. operands_count++;
  67. strcpy(operands[j], yytext);
  68. j++;
  69. }
  70. %%
  71.  
  72.  
  73. int yywrap()
  74. {
  75. return 1;
  76. }
  77. int main()
  78. {
  79. int k;
  80. printf("Enter the arithmetic expression: ");
  81. yylex();
  82.  
  83. if (valid == 1 && top == -1) {
  84. printf("\nValid Expression\n");
  85. }
  86. else
  87. printf("\nInvalid Expression\n");
  88.  
  89. return 0;
  90. }
Success #stdin #stdout #stderr 0.02s 6844KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/bkqHXU/prog:90:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit