Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. %skeleton "lalr1.cc"
  2. %defines
  3. %define api.value.type variant
  4. %define api.token.constructor
  5. %code requires{
  6. #include "Node.hh"
  7. }
  8. %code{
  9. extern FILE yyin;
  10. unsigned int total = 0;
  11. #include <string>
  12. #define YY_DECL yy::parser::symbol_type yylex()
  13. YY_DECL;
  14. Node root;
  15. int Node::id = 0;
  16. }
  17.  
  18.  
  19. %token <std::string> SPACE
  20. %token <std::string> EQUAL
  21. %token <std::string> NUMBER
  22. %token <std::string> ADD
  23. %token <std::string> SUB
  24. %token <std::string> MULT
  25. %token <std::string> DIV
  26. %token <std::string> ELE
  27. %token <std::string> NEWLINE
  28. %token <std::string> START
  29. %token <std::string> FINISH
  30. %token <std::string> VAR
  31. %token <std::string> PRINT
  32. %token <std::string> COMMA
  33. %token <std::string> CITE
  34. %token <std::string> FOR
  35. %token <std::string> DO
  36. %token <std::string> ENDLOOP
  37. %token <std::string> READ
  38. %token <std::string> TRUE
  39. %token <std::string> FALSE
  40. %token <std::string> MOD
  41. %token <std::string> EQU
  42. %token <std::string> IF
  43. %token <std::string> THEN
  44. %token <std::string> LIST
  45. %token <std::string> LISTSTART
  46. %token <std::string> LISTEND
  47. %token <std::string> LISTRANGESTART
  48. %token <std::string> LISTRANGEEND
  49. %token <std::string> REPEAT
  50. %token <std::string> UNTIL
  51. %token <std::string> MORE
  52. %token <std::string> LESS
  53. %token <std::string> FUNCTION
  54. %token <std::string> ELSE
  55.  
  56. //binop precedence
  57. %left MORE LESS EQU COMMA
  58. %left ADD SUB
  59. %left MULT DIV MOD
  60. %left ELE
  61.  
  62. %type <Node> chunk
  63. %type <Node> exp
  64. %type <Node> optblank
  65. %type <Node> block
  66. %type <Node> stat
  67. %type <Node> prefixexp
  68. %type <Node> field
  69. %type <Node> var
  70. %type <Node> functioncall
  71. %type <Node> explist
  72. %type <Node> args
  73.  
  74.  
  75. %token END 0 "end of file"
  76. %%
  77.  
  78. block : chunk {$$ = Node("BLOCK", "");
  79. $$.children.push_back($1);
  80. root = $$;};
  81. | block NEWLINE chunk { $$ = $1;
  82. $$.children.push_back($3);
  83. root = $$;}
  84.  
  85.  
  86. chunk : { $$ = Node("CHUNK", "empty");}
  87. | stat { $$ = Node("CHUNK","has stat");
  88. $$.children.push_back($1);};
  89.  
  90.  
  91. stat : field { $$ = $1; }
  92.  
  93.  
  94. optblank : {};
  95. | optblank SPACE {};
  96. | optblank READ {};
  97.  
  98.  
  99.  
  100. var : optblank VAR optblank {$$ = Node("VAR",$2);};
  101.  
  102. explist : exp {$$ = $1;};
  103. | explist COMMA optblank exp {$$ = $1;};
  104.  
  105. exp : optblank NUMBER optblank {$$ = Node("NUMBER",$2);};
  106. | prefixexp {$$ = $1;};
  107. | functioncall {$$ = $1;};
  108. | exp MORE exp {$$ = Node("BINOP",">");
  109. $$.children.push_back($1);
  110. $$.children.push_back($3);};
  111. | exp LESS exp {$$ = Node("BINOP","<");
  112. $$.children.push_back($1);
  113. $$.children.push_back($3);};
  114. | exp EQU exp {$$ = Node("BINOP","==");
  115. $$.children.push_back($1);
  116. $$.children.push_back($3);};
  117. | exp ADD exp {$$ = Node("BINOP","+");
  118. $$.children.push_back($1);
  119. $$.children.push_back($3);};
  120. | exp SUB exp {$$ = Node("BINOP","-");
  121. $$.children.push_back($1);
  122. $$.children.push_back($3);};
  123. | exp MULT exp {$$ = Node("BINOP","*");
  124. $$.children.push_back($1);
  125. $$.children.push_back($3);};
  126. | exp DIV exp {$$ = Node("BINOP","/");
  127. $$.children.push_back($1);
  128. $$.children.push_back($3);};
  129. | exp MOD exp {$$ = Node("BINOP","%");
  130. $$.children.push_back($1);
  131. $$.children.push_back($3);};
  132. | exp ELE exp {$$ = Node("BINOP","^");
  133. $$.children.push_back($1);
  134. $$.children.push_back($3);};
  135.  
  136. args : START explist END {$$ = $2;};
  137.  
  138. prefixexp : var {$$ = $1;};
  139. | optblank START exp FINISH optblank {$$ = $3;};
  140.  
  141. functioncall : prefixexp args {$$ = $1;};
  142.  
  143.  
  144. field : exp {$$ = $1;}
  145. | var EQUAL exp {$$ = Node("Assignment", "");
  146. $$.children.push_back($1);
  147. $$.children.push_back($3);};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement