fork download
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. const char input[] = "9-5*2";
  6. int index = 0;
  7. char LookAhead = input[index];
  8.  
  9. void Match(char newChar);
  10. void Factor();
  11. void Rest_();
  12. void Rest();
  13. void Term();
  14. void Expression();
  15.  
  16.  
  17. int main(){
  18. Expression();
  19. return 0;
  20. }
  21.  
  22. void Match(char newChar){
  23. if(newChar == LookAhead){
  24. index++;
  25. LookAhead = input[index];
  26. }
  27. }
  28.  
  29. void Expression(){
  30. Term();
  31. Rest();
  32. }
  33.  
  34. void Term(){
  35. Factor();
  36. Rest_();
  37. }
  38.  
  39. void Rest(){
  40. if(LookAhead == '+'){
  41. Match('+');
  42. Term();
  43. cout << '+';
  44. Rest();
  45. }else if(LookAhead == '-'){
  46. Match('-');
  47. Term();
  48. cout << '-';
  49. Rest();
  50. }else{
  51.  
  52. }
  53. }
  54.  
  55. void Rest_(){
  56. if(LookAhead == '*'){
  57. Match('*');
  58. Factor();
  59. cout << '*';
  60. Rest_();
  61. }else if(LookAhead == '/'){
  62. Match('/');
  63. Factor();
  64. cout << '/';
  65. Rest_();
  66. }else{
  67.  
  68. }
  69. }
  70.  
  71. void Factor(){
  72. if(isdigit(LookAhead)){
  73. cout << LookAhead;
  74. Match(LookAhead);
  75. }
  76. }
Success #stdin #stdout 0s 4396KB
stdin
Standard input is empty
stdout
952*-