A Hugo incarnation of the blog.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parser.y 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. %{
  2. #include <string>
  3. #include <iostream>
  4. #include "ast.hpp"
  5. #include "definition.hpp"
  6. #include "parser.hpp"
  7. std::vector<definition_ptr> program;
  8. extern yy::parser::symbol_type yylex();
  9. %}
  10. %token PLUS
  11. %token TIMES
  12. %token MINUS
  13. %token DIVIDE
  14. %token <int> INT
  15. %token DEFN
  16. %token DATA
  17. %token CASE
  18. %token OF
  19. %token OCURLY
  20. %token CCURLY
  21. %token OPAREN
  22. %token CPAREN
  23. %token COMMA
  24. %token ARROW
  25. %token EQUAL
  26. %token <std::string> LID
  27. %token <std::string> UID
  28. %language "c++"
  29. %define api.value.type variant
  30. %define api.token.constructor
  31. %type <std::vector<std::string>> lowercaseParams uppercaseParams
  32. %type <std::vector<definition_ptr>> program definitions
  33. %type <std::vector<branch_ptr>> branches
  34. %type <std::vector<constructor_ptr>> constructors
  35. %type <ast_ptr> aAdd aMul case app appBase
  36. %type <definition_ptr> definition defn data
  37. %type <branch_ptr> branch
  38. %type <pattern_ptr> pattern
  39. %type <constructor_ptr> constructor
  40. %start program
  41. %%
  42. program
  43. : definitions { program = std::move($1); }
  44. ;
  45. definitions
  46. : definitions definition { $$ = std::move($1); $$.push_back(std::move($2)); }
  47. | definition { $$ = std::vector<definition_ptr>(); $$.push_back(std::move($1)); }
  48. ;
  49. definition
  50. : defn { $$ = std::move($1); }
  51. | data { $$ = std::move($1); }
  52. ;
  53. defn
  54. : DEFN LID lowercaseParams EQUAL OCURLY aAdd CCURLY
  55. { $$ = definition_ptr(
  56. new definition_defn(std::move($2), std::move($3), std::move($6))); }
  57. ;
  58. lowercaseParams
  59. : %empty { $$ = std::vector<std::string>(); }
  60. | lowercaseParams LID { $$ = std::move($1); $$.push_back(std::move($2)); }
  61. ;
  62. uppercaseParams
  63. : %empty { $$ = std::vector<std::string>(); }
  64. | uppercaseParams UID { $$ = std::move($1); $$.push_back(std::move($2)); }
  65. ;
  66. aAdd
  67. : aAdd PLUS aMul { $$ = ast_ptr(new ast_binop(PLUS, std::move($1), std::move($3))); }
  68. | aAdd MINUS aMul { $$ = ast_ptr(new ast_binop(MINUS, std::move($1), std::move($3))); }
  69. | aMul { $$ = std::move($1); }
  70. ;
  71. aMul
  72. : aMul TIMES app { $$ = ast_ptr(new ast_binop(TIMES, std::move($1), std::move($3))); }
  73. | aMul DIVIDE app { $$ = ast_ptr(new ast_binop(DIVIDE, std::move($1), std::move($3))); }
  74. | app { $$ = std::move($1); }
  75. ;
  76. app
  77. : app appBase { $$ = ast_ptr(new ast_app(std::move($1), std::move($2))); }
  78. | appBase { $$ = std::move($1); }
  79. ;
  80. appBase
  81. : INT { $$ = ast_ptr(new ast_int($1)); }
  82. | LID { $$ = ast_ptr(new ast_lid(std::move($1))); }
  83. | UID { $$ = ast_ptr(new ast_uid(std::move($1))); }
  84. | OPAREN aAdd CPAREN { $$ = std::move($2); }
  85. | case { $$ = std::move($1); }
  86. ;
  87. case
  88. : CASE aAdd OF OCURLY branches CCURLY
  89. { $$ = ast_ptr(new ast_case(std::move($2), std::move($5))); }
  90. ;
  91. branches
  92. : branches branch { $$ = std::move($1); $$.push_back(std::move($2)); }
  93. | branch { $$ = std::vector<branch_ptr>(); $$.push_back(std::move($1));}
  94. ;
  95. branch
  96. : pattern ARROW OCURLY aAdd CCURLY
  97. { $$ = branch_ptr(new branch(std::move($1), std::move($4))); }
  98. ;
  99. pattern
  100. : LID { $$ = pattern_ptr(new pattern_var(std::move($1))); }
  101. | UID lowercaseParams
  102. { $$ = pattern_ptr(new pattern_constr(std::move($1), std::move($2))); }
  103. ;
  104. data
  105. : DATA UID EQUAL OCURLY constructors CCURLY
  106. { $$ = definition_ptr(new definition_data(std::move($2), std::move($5))); }
  107. ;
  108. constructors
  109. : constructors COMMA constructor { $$ = std::move($1); $$.push_back(std::move($3)); }
  110. | constructor
  111. { $$ = std::vector<constructor_ptr>(); $$.push_back(std::move($1)); }
  112. ;
  113. constructor
  114. : UID uppercaseParams
  115. { $$ = constructor_ptr(new constructor(std::move($1), std::move($2))); }
  116. ;