Guest User

Untitled

a guest
Feb 1st, 2012
65
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. expr = expr + ident // r1
  2. | ident // r2
  3.  
  4. | ident | + | $ | expr |
  5. 0 | s1 | | | 2 |
  6. 1 | r2 | r2 | r2| |
  7. 2 | | s3 |acc| |
  8. 3 | s4 | | | 2 |
  9. 4 | r1 | r1 | r1| |
  10.  
  11. struct result {
  12. symbol nonterm;
  13. int tokens_to_reduce;
  14. }
  15.  
  16. result state0(token* stream) {
  17. result res;
  18. switch(stream[0]) {
  19. case ident:
  20. res = state1(stream+1);
  21. break;
  22. default: throw error();
  23. }
  24. while(true) {
  25. if(res.tokens_to_reduce == 0) {
  26. switch(res.nonterm) {
  27. case expr:
  28. res = state2(stream);
  29. break;
  30. default: throw error();
  31. }
  32. }
  33. else return { res.non_term, res.tokens_to_reduce-1 };
  34. }
  35. }
  36.  
  37. result state1(token* stream) {
  38. reduce2(); // needs arguments
  39. return { expr, 0 };
  40. }
  41.  
  42. result state2(token* stream) {
  43. result res;
  44. switch(stream[0]) {
  45. case plus:
  46. res = state3(stream+1);
  47. break;
  48. case end:
  49. return accept(); // needs some work
  50. default: throw error();
  51. }
  52. }
  53.  
  54. result state3(token* stream) {
  55. result res;
  56. switch(stream[0]) {
  57. case ident:
  58. res = state4(stream+1);
  59. break;
  60. default: throw error();
  61. }
  62. while(true) {
  63. if(res.tokens_to_reduce == 0) {
  64. switch(res.nonterm) {
  65. case expr:
  66. res = state2(stream);
  67. break;
  68. default: throw error();
  69. }
  70. }
  71. else return { res.non_term, res.tokens_to_reduce-1 };
  72. }
  73. }
  74.  
  75. result state4(token* stream) {
  76. reduce1(); // needs arguments
  77. return { expr, 1 };
  78. }
RAW Paste Data