parser.ypp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. %code top {
  5. #include <algorithm>
  6. #include <cstdarg>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <iostream>
  10. #include <list>
  11. #include "executable_semantics/syntax/syntax_helpers.h"
  12. }
  13. %code requires {
  14. #include <optional>
  15. #include "executable_semantics/ast/declaration.h"
  16. #include "executable_semantics/ast/field_list.h"
  17. #include "executable_semantics/ast/function_definition.h"
  18. namespace Carbon {
  19. // A representation of the parsed program.
  20. using AST = std::list<Carbon::Declaration>*;
  21. }
  22. }
  23. %code {
  24. extern int yylineno;
  25. extern int yylex();
  26. void yyerror(std::optional<Carbon::AST> const& parsedProgram, char* error) {
  27. Carbon::PrintSyntaxError(error, yylineno);
  28. }
  29. }
  30. %parse-param {std::optional<Carbon::AST>& parsedProgram}
  31. %union {
  32. char* str;
  33. int num;
  34. Carbon::Expression* expression;
  35. std::list<std::pair<std::string, Carbon::Expression*>>* field_types;
  36. Carbon::Statement* statement;
  37. Carbon::Statement* statement_list;
  38. Carbon::FunctionDefinition* function_definition;
  39. Carbon::Declaration* declaration;
  40. std::list<Carbon::Declaration>* declaration_list;
  41. Carbon::Member* member;
  42. std::list<Carbon::Member*>* member_list;
  43. Carbon::FieldList* field_list;
  44. std::pair<std::string, Carbon::Expression*>* alternative;
  45. std::list<std::pair<std::string, Carbon::Expression*>>* alternative_list;
  46. std::pair<Carbon::Expression*, Carbon::Statement*>* clause;
  47. std::list<std::pair<Carbon::Expression*, Carbon::Statement*>>* clause_list;
  48. Carbon::Expression* fun_type;
  49. };
  50. %token <num> integer_literal
  51. %token <str> identifier
  52. %type <str> designator
  53. %type <declaration> declaration
  54. %type <function_definition> function_declaration
  55. %type <function_definition> function_definition
  56. %type <declaration_list> declaration_list
  57. %type <statement> statement
  58. %type <statement> optional_else
  59. %type <statement_list> statement_list
  60. %type <expression> expression
  61. %type <expression> pattern
  62. %type <expression> return_type
  63. %type <expression> paren_expression
  64. %type <expression> tuple
  65. %type <member> member
  66. %type <member_list> member_list
  67. %type <field_list> field
  68. %type <field_list> field_list
  69. %type <alternative> alternative
  70. %type <alternative_list> alternative_list
  71. %type <clause> clause
  72. %type <clause_list> clause_list
  73. %token AND
  74. %token OR
  75. %token NOT
  76. %token INT
  77. %token BOOL
  78. %token TYPE
  79. %token FN
  80. %token FNTY
  81. %token ARROW
  82. %token VAR
  83. %token EQUAL
  84. %token IF
  85. %token ELSE
  86. %token WHILE
  87. %token BREAK
  88. %token CONTINUE
  89. %token RETURN
  90. %token TRUE
  91. %token FALSE
  92. %token STRUCT
  93. %token CHOICE
  94. %token MATCH
  95. %token CASE
  96. %token DBLARROW
  97. %token DEFAULT
  98. %token AUTO
  99. %nonassoc '{' '}'
  100. %nonassoc ':' ',' DBLARROW
  101. %left OR AND
  102. %nonassoc EQUAL NOT
  103. %left '+' '-'
  104. %left '.' ARROW
  105. %nonassoc '(' ')' '[' ']'
  106. %start input
  107. %locations
  108. %%
  109. input: declaration_list
  110. { parsedProgram = $1; }
  111. ;
  112. pattern:
  113. expression
  114. { $$ = $1; }
  115. ;
  116. expression:
  117. identifier
  118. { $$ = Carbon::MakeVar(yylineno, $1); }
  119. | expression designator
  120. { $$ = Carbon::MakeGetField(yylineno, $1, $2); }
  121. | expression '[' expression ']'
  122. { $$ = Carbon::MakeIndex(yylineno, $1, $3); }
  123. | expression ':' identifier
  124. { $$ = Carbon::MakeVarPat(yylineno, $3, $1); }
  125. | integer_literal
  126. { $$ = Carbon::MakeInt(yylineno, $1); }
  127. | TRUE
  128. { $$ = Carbon::MakeBool(yylineno, true); }
  129. | FALSE
  130. { $$ = Carbon::MakeBool(yylineno, false); }
  131. | INT
  132. { $$ = Carbon::MakeIntType(yylineno); }
  133. | BOOL
  134. { $$ = Carbon::MakeBoolType(yylineno); }
  135. | TYPE
  136. { $$ = Carbon::MakeTypeType(yylineno); }
  137. | AUTO
  138. { $$ = Carbon::MakeAutoType(yylineno); }
  139. | paren_expression { $$ = $1; }
  140. | expression EQUAL expression
  141. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Eq, $1, $3); }
  142. | expression '+' expression
  143. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Add, $1, $3); }
  144. | expression '-' expression
  145. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Sub, $1, $3); }
  146. | expression AND expression
  147. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::And, $1, $3); }
  148. | expression OR expression
  149. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Or, $1, $3); }
  150. | NOT expression
  151. { $$ = Carbon::MakeUnOp(yylineno, Carbon::Operator::Not, $2); }
  152. | '-' expression
  153. { $$ = Carbon::MakeUnOp(yylineno, Carbon::Operator::Neg, $2); }
  154. | expression tuple
  155. { $$ = Carbon::MakeCall(yylineno, $1, $2); }
  156. | FNTY tuple return_type
  157. { $$ = Carbon::MakeFunType(yylineno, $2, $3); }
  158. ;
  159. designator: '.' identifier { $$ = $2; }
  160. ;
  161. paren_expression: '(' field_list ')'
  162. {
  163. if ($2->fields->size() == 1 &&
  164. $2->fields->front().first == "" &&
  165. !$2->has_explicit_comma) {
  166. $$ = $2->fields->front().second;
  167. } else {
  168. auto vec = new std::vector<std::pair<std::string,Carbon::Expression*>>(
  169. $2->fields->begin(), $2->fields->end());
  170. $$ = Carbon::MakeTuple(yylineno, vec);
  171. }
  172. }
  173. ;
  174. tuple: '(' field_list ')'
  175. {
  176. auto vec = new std::vector<std::pair<std::string,Carbon::Expression*>>(
  177. $2->fields->begin(), $2->fields->end());
  178. $$ = Carbon::MakeTuple(yylineno, vec);
  179. }
  180. field:
  181. pattern
  182. {
  183. auto fields =
  184. new std::list<std::pair<std::string, Carbon::Expression*>>();
  185. fields->push_back(std::make_pair("", $1));
  186. $$ = Carbon::MakeFieldList(fields);
  187. }
  188. | designator '=' pattern
  189. {
  190. auto fields =
  191. new std::list<std::pair<std::string, Carbon::Expression*>>();
  192. fields->push_back(std::make_pair($1, $3));
  193. $$ = Carbon::MakeFieldList(fields);
  194. }
  195. ;
  196. field_list:
  197. // Empty
  198. {
  199. $$ = Carbon::MakeFieldList(
  200. new std::list<std::pair<std::string, Carbon::Expression*>>());
  201. }
  202. | field
  203. { $$ = $1; }
  204. | field ',' field_list
  205. { $$ = Carbon::MakeConsField($1, $3); }
  206. ;
  207. clause:
  208. CASE pattern DBLARROW statement
  209. { $$ = new std::pair<Carbon::Expression*, Carbon::Statement*>($2, $4); }
  210. | DEFAULT DBLARROW statement
  211. {
  212. auto vp = Carbon::MakeVarPat(yylineno, "_",
  213. Carbon::MakeAutoType(yylineno));
  214. $$ = new std::pair<Carbon::Expression*, Carbon::Statement*>(vp, $3);
  215. }
  216. ;
  217. clause_list:
  218. // Empty
  219. {
  220. $$ = new std::list<std::pair<Carbon::Expression*, Carbon::Statement*>>();
  221. }
  222. | clause clause_list
  223. { $$ = $2; $$->push_front(*$1); }
  224. ;
  225. statement:
  226. expression '=' expression ';'
  227. { $$ = Carbon::MakeAssign(yylineno, $1, $3); }
  228. | VAR pattern '=' expression ';'
  229. { $$ = Carbon::MakeVarDef(yylineno, $2, $4); }
  230. | expression ';'
  231. { $$ = Carbon::MakeExpStmt(yylineno, $1); }
  232. | IF '(' expression ')' statement optional_else
  233. { $$ = Carbon::MakeIf(yylineno, $3, $5, $6); }
  234. | WHILE '(' expression ')' statement
  235. { $$ = Carbon::MakeWhile(yylineno, $3, $5); }
  236. | BREAK ';'
  237. { $$ = Carbon::MakeBreak(yylineno); }
  238. | CONTINUE ';'
  239. { $$ = Carbon::MakeContinue(yylineno); }
  240. | RETURN expression ';'
  241. { $$ = Carbon::MakeReturn(yylineno, $2); }
  242. | '{' statement_list '}'
  243. { $$ = Carbon::MakeBlock(yylineno, $2); }
  244. | MATCH '(' expression ')' '{' clause_list '}'
  245. { $$ = Carbon::MakeMatch(yylineno, $3, $6); }
  246. ;
  247. optional_else:
  248. // Empty
  249. { $$ = 0; }
  250. | ELSE statement { $$ = $2; }
  251. ;
  252. statement_list:
  253. // Empty
  254. { $$ = 0; }
  255. | statement statement_list
  256. { $$ = Carbon::MakeSeq(yylineno, $1, $2); }
  257. ;
  258. return_type:
  259. // Empty
  260. {
  261. $$ = Carbon::MakeTuple(
  262. yylineno,
  263. new std::vector<std::pair<std::string, Carbon::Expression*>>());
  264. }
  265. | ARROW expression
  266. { $$ = $2; }
  267. ;
  268. function_definition:
  269. FN identifier tuple return_type '{' statement_list '}'
  270. { $$ = MakeFunDef(yylineno, $2, $4, $3, $6); }
  271. | FN identifier tuple DBLARROW expression ';'
  272. {
  273. $$ = Carbon::MakeFunDef(yylineno, $2, Carbon::MakeAutoType(yylineno), $3,
  274. Carbon::MakeReturn(yylineno, $5));
  275. }
  276. ;
  277. function_declaration:
  278. FN identifier tuple return_type ';'
  279. { $$ = MakeFunDef(yylineno, $2, $4, $3, 0); }
  280. ;
  281. member:
  282. VAR expression ':' identifier ';'
  283. { $$ = MakeField(yylineno, $4, $2); }
  284. ;
  285. member_list:
  286. // Empty
  287. { $$ = new std::list<Carbon::Member*>(); }
  288. | member member_list
  289. { $$ = $2; $$->push_front($1); }
  290. ;
  291. alternative:
  292. identifier tuple
  293. { $$ = new std::pair<std::string, Carbon::Expression*>($1, $2); }
  294. | identifier
  295. {
  296. $$ = new std::pair<std::string, Carbon::Expression*>(
  297. $1, Carbon::MakeTuple(
  298. yylineno,
  299. new std::vector<std::pair<std::string, Carbon::Expression*>>()));
  300. }
  301. ;
  302. alternative_list:
  303. // Empty
  304. { $$ = new std::list<std::pair<std::string, Carbon::Expression*>>(); }
  305. | alternative
  306. {
  307. $$ = new std::list<std::pair<std::string, Carbon::Expression*>>();
  308. $$->push_front(*$1);
  309. }
  310. | alternative ',' alternative_list
  311. { $$ = $3; $$->push_front(*$1); }
  312. ;
  313. declaration:
  314. function_definition
  315. { $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
  316. | function_declaration
  317. { $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
  318. | STRUCT identifier '{' member_list '}'
  319. {
  320. $$ = new Carbon::Declaration(
  321. Carbon::StructDeclaration{yylineno, $2, $4});
  322. }
  323. | CHOICE identifier '{' alternative_list '}'
  324. {
  325. $$ = new Carbon::Declaration(
  326. Carbon::ChoiceDeclaration{yylineno, $2, std::list(*$4)});
  327. }
  328. ;
  329. declaration_list:
  330. // Empty
  331. { $$ = new std::list<Carbon::Declaration>(); }
  332. | declaration declaration_list
  333. {
  334. $$ = $2;
  335. $$->push_front(*$1);
  336. }
  337. ;
  338. %%