fork download
  1. /* With Left Recursion
  2. E -> E+T | T
  3. T -> T*F | F
  4. F -> (E) | id
  5. */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. char input[10];
  11. int i, error;
  12. void E();
  13. void T();
  14. void Eprime();
  15. void Tprime();
  16. void F();
  17. main() {
  18. printf("\nGrammar without left recursion");
  19. printf("\n\t\t E->TE' \n\t\t E'->+TE'|e \n\t\t T->FT' ");
  20. printf("\n\t\t T'->*FT'|e \n\t\t F->(E)|i");
  21. while (1) {
  22. i = 0;
  23. error = 0;
  24. printf("Enter an arithmetic expression : "); // Eg: a+a*a
  25. gets(input);
  26. E();
  27. if (strlen(input) == i && error == 0)
  28. printf("\nAccepted..!!!\n");
  29. else {
  30. printf("\nRejected..!!!\n");
  31. exit(0);
  32. }
  33. }
  34. }
  35. void E() {
  36. T();
  37. Eprime();
  38. }
  39. void Eprime() {
  40. if (input[i] == '+') {
  41. i++;
  42. T();
  43. Eprime();
  44. }
  45. }
  46. void T() {
  47. F();
  48. Tprime();
  49. }
  50. void Tprime() {
  51. if (input[i] == '*') {
  52. i++;
  53. F();
  54. Tprime();
  55. }
  56. }
  57. void F() {
  58. if (isalnum(input[i]))
  59. i++;
  60. 15. Recursive descent parser
  61. 25
  62. else if (input[i] == '(') {
  63. i++;
  64. E();
  65. if (input[i] == ')')
  66. i++;
  67. else
  68. error = 1;
  69. }
  70. else
  71. error = 1;
  72. }
Success #stdin #stdout #stderr 0.02s 6984KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/gJ3HOF/prog:6:1: Syntax error: Operator expected
ERROR: /home/gJ3HOF/prog:72:0: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit