parser.ypp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. // -----------------------------------------------------------------------------
  5. // Bison Configuration
  6. // -----------------------------------------------------------------------------
  7. %require "3.2"
  8. %language "c++"
  9. // We don't need a separate header for Bison locations.
  10. %define api.location.file none
  11. // Use a type-safe C++ variant for semantic values
  12. %define api.value.type variant
  13. // Have Bison generate the functions ‘make_TEXT’ and ‘make_NUMBER’, but also
  14. // ‘make_YYEOF’, for the end of input.
  15. %define api.token.constructor
  16. // Make parse error messages more detailed
  17. %define parse.error verbose
  18. // Enable support for parser debugging
  19. %define parse.trace true
  20. //
  21. // Parameters to the parser and lexer
  22. //
  23. // Parameters to the parser are stored therein as protected data members, and
  24. // thus available to its methods.
  25. // "out" parameter passed to the parser, where the AST is written.
  26. %parse-param {std::optional<Carbon::AST>& parsed_program}
  27. // "inout" parameter passed to both the parser and the lexer.
  28. %param {Carbon::ParseAndLexContext& context}
  29. // No shift-reduce conflicts are expected.
  30. %expect 0
  31. // -----------------------------------------------------------------------------
  32. %code top {
  33. #include <algorithm>
  34. #include <cstdarg>
  35. #include <cstdio>
  36. #include <cstdlib>
  37. #include <iostream>
  38. #include <list>
  39. #include <vector>
  40. #include "executable_semantics/syntax/syntax_helpers.h"
  41. #include "executable_semantics/syntax/parse_and_lex_context.h"
  42. } // %code top
  43. %code requires {
  44. #include <optional>
  45. #include "executable_semantics/ast/abstract_syntax_tree.h"
  46. #include "executable_semantics/ast/declaration.h"
  47. #include "executable_semantics/ast/expression.h"
  48. #include "executable_semantics/ast/function_definition.h"
  49. #include "executable_semantics/ast/pattern.h"
  50. #include "executable_semantics/common/arena.h"
  51. #include "executable_semantics/syntax/paren_contents.h"
  52. namespace Carbon {
  53. class ParseAndLexContext;
  54. } // namespace Carbon
  55. } // %code requires
  56. %code {
  57. extern int yylineno;
  58. void yy::parser::error(const location_type&, const std::string& message) {
  59. context.PrintDiagnostic(message, yylineno);
  60. }
  61. } // %code
  62. %token <int> integer_literal
  63. %token <std::string> identifier
  64. %type <std::string> designator
  65. %type <Carbon::Declaration> declaration
  66. %type <Carbon::FunctionDefinition> function_declaration
  67. %type <Carbon::FunctionDefinition> function_definition
  68. %type <std::list<Carbon::Declaration>> declaration_list
  69. %type <const Carbon::Statement*> statement
  70. %type <const Carbon::Statement*> if_statement
  71. %type <const Carbon::Statement*> optional_else
  72. %type <std::pair<const Carbon::Expression*, bool>> return_expression
  73. %type <const Carbon::Statement*> block
  74. %type <const Carbon::Statement*> statement_list
  75. %type <const Carbon::Expression*> expression
  76. %type <Carbon::GenericBinding> generic_binding
  77. %type <std::vector<Carbon::GenericBinding>> deduced_params
  78. %type <std::vector<Carbon::GenericBinding>> deduced_param_list
  79. %type <const Carbon::Pattern*> pattern
  80. %type <const Carbon::Pattern*> non_expression_pattern
  81. %type <std::pair<const Carbon::Expression*, bool>> return_type
  82. %type <const Carbon::Expression*> paren_expression
  83. %type <const Carbon::Expression*> tuple
  84. %type <std::optional<std::string>> binding_lhs
  85. %type <const Carbon::BindingPattern*> variable_declaration
  86. %type <Carbon::Member*> member
  87. %type <std::list<Carbon::Member*>> member_list
  88. %type <Carbon::ParenContents<Carbon::Expression>::Element> paren_expression_element
  89. %type <Carbon::ParenContents<Carbon::Expression>> paren_expression_base
  90. %type <Carbon::ParenContents<Carbon::Expression>> paren_expression_contents
  91. %type <const Carbon::Pattern*> paren_pattern
  92. %type <const Carbon::TuplePattern*> tuple_pattern
  93. %type <const Carbon::TuplePattern*> maybe_empty_tuple_pattern
  94. %type <Carbon::ParenContents<Carbon::Pattern>> paren_pattern_base
  95. %type <Carbon::ParenContents<Carbon::Pattern>::Element> paren_pattern_element
  96. %type <Carbon::ParenContents<Carbon::Pattern>> paren_pattern_contents
  97. %type <std::pair<std::string, const Carbon::Expression*>> alternative
  98. %type <std::list<std::pair<std::string, const Carbon::Expression*>>> alternative_list
  99. %type <std::pair<const Carbon::Pattern*, const Carbon::Statement*>*> clause
  100. %type <std::list<std::pair<const Carbon::Pattern*, const Carbon::Statement*>>*> clause_list
  101. %token END_OF_FILE 0
  102. %token AND
  103. %token OR
  104. %token NOT
  105. %token INT
  106. %token BOOL
  107. %token TYPE
  108. %token FN
  109. %token FNTY
  110. %token ARROW "->"
  111. %token FNARROW "-> in return type"
  112. %token VAR
  113. %token EQUAL_EQUAL
  114. %token IF
  115. %token ELSE
  116. %token WHILE
  117. %token CONTINUATION_TYPE
  118. %token CONTINUATION
  119. %token RUN
  120. %token AWAIT
  121. %token BREAK
  122. %token CONTINUE
  123. %token RETURN
  124. %token TRUE
  125. %token FALSE
  126. %token STRUCT
  127. %token CHOICE
  128. %token MATCH
  129. %token CASE
  130. %token DBLARROW "=>"
  131. %token DEFAULT
  132. %token AUTO
  133. %token UNDERSCORE
  134. %token
  135. EQUAL "="
  136. MINUS "-"
  137. PLUS "+"
  138. // The lexer determines the arity and fixity of each `*` based on whitespace
  139. // and adjacent tokens. UNARY_STAR indicates that the operator is unary but
  140. // could be either prefix or postfix.
  141. UNARY_STAR "unary *"
  142. PREFIX_STAR "prefix *"
  143. POSTFIX_STAR "postfix *"
  144. BINARY_STAR "binary *"
  145. SLASH "/"
  146. LEFT_PARENTHESIS "("
  147. RIGHT_PARENTHESIS ")"
  148. LEFT_CURLY_BRACE "{"
  149. RIGHT_CURLY_BRACE "}"
  150. LEFT_SQUARE_BRACKET "["
  151. RIGHT_SQUARE_BRACKET "]"
  152. PERIOD "."
  153. COMMA ","
  154. SEMICOLON ";"
  155. COLON_BANG ":!"
  156. COLON ":"
  157. ;
  158. %precedence FNARROW
  159. %precedence "{" "}"
  160. %precedence ":!" ":" "," DBLARROW
  161. %left OR AND
  162. %nonassoc EQUAL_EQUAL
  163. %left "+" "-"
  164. %left BINARY_STAR
  165. %precedence NOT UNARY_MINUS PREFIX_STAR
  166. // We need to give the `UNARY_STAR` token a precedence, rather than overriding
  167. // the precedence of the `expression UNARY_STAR` rule below, because bison
  168. // compares the precedence of the final token (for a shift) to the precedence
  169. // of the other rule (for a reduce) when attempting to resolve a shift-reduce
  170. // conflict. See https://stackoverflow.com/a/26188429/1041090. When UNARY_STAR
  171. // is the final token of a rule, it must be a postfix usage, so we give it the
  172. // same precedence as POSTFIX_STAR.
  173. %precedence POSTFIX_STAR UNARY_STAR
  174. %left "." ARROW
  175. %precedence "(" ")" "[" "]"
  176. %start input
  177. %locations
  178. %%
  179. input: declaration_list
  180. { parsed_program = $1; }
  181. ;
  182. expression:
  183. identifier
  184. { $$ = Carbon::Expression::MakeIdentifierExpression(yylineno, $1); }
  185. | expression designator
  186. { $$ = Carbon::Expression::MakeFieldAccessExpression(yylineno, $1, $2); }
  187. | expression "[" expression "]"
  188. { $$ = Carbon::Expression::MakeIndexExpression(yylineno, $1, $3); }
  189. | integer_literal
  190. { $$ = Carbon::Expression::MakeIntLiteral(yylineno, $1); }
  191. | TRUE
  192. { $$ = Carbon::Expression::MakeBoolLiteral(yylineno, true); }
  193. | FALSE
  194. { $$ = Carbon::Expression::MakeBoolLiteral(yylineno, false); }
  195. | INT
  196. { $$ = Carbon::Expression::MakeIntTypeLiteral(yylineno); }
  197. | BOOL
  198. { $$ = Carbon::Expression::MakeBoolTypeLiteral(yylineno); }
  199. | TYPE
  200. { $$ = Carbon::Expression::MakeTypeTypeLiteral(yylineno); }
  201. | CONTINUATION_TYPE
  202. { $$ = Carbon::Expression::MakeContinuationTypeLiteral(yylineno); }
  203. | paren_expression { $$ = $1; }
  204. | expression EQUAL_EQUAL expression
  205. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  206. yylineno, Carbon::Operator::Eq, {$1, $3}); }
  207. | expression "+" expression
  208. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  209. yylineno, Carbon::Operator::Add, {$1, $3}); }
  210. | expression "-" expression
  211. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  212. yylineno, Carbon::Operator::Sub, {$1, $3}); }
  213. | expression BINARY_STAR expression
  214. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  215. yylineno, Carbon::Operator::Mul, {$1, $3}); }
  216. | expression AND expression
  217. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  218. yylineno, Carbon::Operator::And, {$1, $3}); }
  219. | expression OR expression
  220. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  221. yylineno, Carbon::Operator::Or, {$1, $3}); }
  222. | NOT expression
  223. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  224. yylineno, Carbon::Operator::Not, {$2}); }
  225. | "-" expression %prec UNARY_MINUS
  226. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  227. yylineno, Carbon::Operator::Neg, {$2}); }
  228. | PREFIX_STAR expression
  229. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  230. yylineno, Carbon::Operator::Deref, {$2}); }
  231. | UNARY_STAR expression %prec PREFIX_STAR
  232. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  233. yylineno, Carbon::Operator::Deref, {$2}); }
  234. | expression tuple
  235. { $$ = Carbon::Expression::MakeCallExpression(yylineno, $1, $2); }
  236. | expression POSTFIX_STAR
  237. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  238. yylineno, Carbon::Operator::Ptr, {$1}); }
  239. | expression UNARY_STAR
  240. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  241. yylineno, Carbon::Operator::Ptr, {$1}); }
  242. | FNTY tuple return_type
  243. { $$ = Carbon::Expression::MakeFunctionTypeLiteral(
  244. yylineno, $2, $3.first, $3.second); }
  245. ;
  246. designator: "." identifier { $$ = $2; }
  247. ;
  248. paren_expression: paren_expression_base
  249. { $$ = Carbon::ExpressionFromParenContents(yylineno, $1); }
  250. ;
  251. tuple: paren_expression_base
  252. { $$ = Carbon::TupleExpressionFromParenContents(yylineno, $1); }
  253. ;
  254. paren_expression_element:
  255. expression
  256. { $$ = {.name = std::nullopt, .term = $1}; }
  257. | designator "=" expression
  258. { $$ = {.name = $1, .term = $3}; }
  259. ;
  260. paren_expression_base:
  261. "(" ")"
  262. { $$ = {.elements = {}, .has_trailing_comma = false}; }
  263. | "(" paren_expression_contents ")"
  264. { $$ = $2; }
  265. | "(" paren_expression_contents "," ")"
  266. {
  267. $$ = $2;
  268. $$.has_trailing_comma = true;
  269. }
  270. ;
  271. paren_expression_contents:
  272. paren_expression_element
  273. { $$ = {.elements = {$1}, .has_trailing_comma = false}; }
  274. | paren_expression_contents "," paren_expression_element
  275. {
  276. $$ = $1;
  277. $$.elements.push_back($3);
  278. }
  279. ;
  280. // In many cases, using `pattern` recursively will result in ambiguities.
  281. // When that happens, it's necessary to factor out two separate productions,
  282. // one for when the sub-pattern is an expression, and one for when it is not.
  283. // To facilitate this, non-terminals besides `pattern` whose names contain
  284. // `pattern` are structured to be disjoint from `expression`, unless otherwise
  285. // specified.
  286. pattern:
  287. non_expression_pattern
  288. { $$ = $1; }
  289. | expression
  290. { $$ = Carbon::global_arena->New<Carbon::ExpressionPattern>($1); }
  291. ;
  292. non_expression_pattern:
  293. AUTO
  294. { $$ = Carbon::global_arena->New<Carbon::AutoPattern>(yylineno); }
  295. | binding_lhs ":" pattern
  296. { $$ = Carbon::global_arena->New<Carbon::BindingPattern>(yylineno, $1, $3); }
  297. | paren_pattern
  298. { $$ = $1; }
  299. | expression tuple_pattern
  300. { $$ = Carbon::global_arena->New<Carbon::AlternativePattern>(yylineno, $1, $2); }
  301. ;
  302. binding_lhs:
  303. identifier { $$ = $1; }
  304. | UNDERSCORE { $$ = std::nullopt; }
  305. ;
  306. paren_pattern: paren_pattern_base
  307. { $$ = Carbon::PatternFromParenContents(yylineno, $1); }
  308. ;
  309. paren_pattern_base:
  310. "(" paren_pattern_contents ")"
  311. { $$ = $2; }
  312. | "(" paren_pattern_contents "," ")"
  313. {
  314. $$ = $2;
  315. $$.has_trailing_comma = true;
  316. }
  317. ;
  318. // paren_pattern is analogous to paren_expression, but in order to avoid
  319. // ambiguities, it must be disjoint from paren_expression, meaning it must
  320. // contain at least one non_expression_pattern. The structure of this rule
  321. // is very different from the corresponding expression rule because is has to
  322. // enforce that requirement.
  323. paren_pattern_contents:
  324. paren_pattern_element
  325. { $$ = {.elements = {$1}, .has_trailing_comma = false }; }
  326. | paren_expression_contents "," paren_pattern_element
  327. {
  328. $$ = Carbon::ParenExpressionToParenPattern($1);
  329. $$.elements.push_back($3);
  330. }
  331. | paren_pattern_contents "," paren_expression_element
  332. {
  333. $$ = $1;
  334. $$.elements.push_back({.name = $3.name, .term = Carbon::global_arena->New<Carbon::ExpressionPattern>($3.term)});
  335. }
  336. | paren_pattern_contents "," paren_pattern_element
  337. {
  338. $$ = $1;
  339. $$.elements.push_back($3);
  340. }
  341. ;
  342. paren_pattern_element:
  343. non_expression_pattern
  344. { $$ = {.name = std::nullopt, .term = $1}; }
  345. | designator "=" non_expression_pattern
  346. { $$ = {.name = $1, .term = $3}; }
  347. ;
  348. tuple_pattern: paren_pattern_base
  349. { $$ = Carbon::TuplePatternFromParenContents(yylineno, $1); }
  350. ;
  351. // Unlike most `pattern` nonterminals, this one overlaps with `expression`,
  352. // so it should be used only when prior context (such as an introducer)
  353. // rules out the possibility of an `expression` at this point.
  354. maybe_empty_tuple_pattern:
  355. "(" ")"
  356. { $$ = Carbon::global_arena->New<Carbon::TuplePattern>(yylineno, std::vector<Carbon::TuplePattern::Field>()); }
  357. | tuple_pattern
  358. { $$ = $1; }
  359. ;
  360. clause:
  361. CASE pattern DBLARROW statement
  362. { $$ = Carbon::global_arena->New<std::pair<const Carbon::Pattern*, const Carbon::Statement*>>($2, $4); }
  363. | DEFAULT DBLARROW statement
  364. {
  365. auto vp = Carbon::global_arena->New<Carbon::BindingPattern>(
  366. yylineno, std::nullopt, Carbon::global_arena->New<Carbon::AutoPattern>(yylineno));
  367. $$ = Carbon::global_arena->New<std::pair<const Carbon::Pattern*, const Carbon::Statement*>>(vp, $3);
  368. }
  369. ;
  370. clause_list:
  371. // Empty
  372. {
  373. $$ = Carbon::global_arena->New<std::list<
  374. std::pair<const Carbon::Pattern*, const Carbon::Statement*>>>();
  375. }
  376. | clause clause_list
  377. { $$ = $2; $$->push_front(*$1); }
  378. ;
  379. statement:
  380. expression "=" expression ";"
  381. { $$ = Carbon::Statement::MakeAssign(yylineno, $1, $3); }
  382. | VAR pattern "=" expression ";"
  383. { $$ = Carbon::Statement::MakeVariableDefinition(yylineno, $2, $4); }
  384. | expression ";"
  385. { $$ = Carbon::Statement::MakeExpressionStatement(yylineno, $1); }
  386. | if_statement
  387. { $$ = $1; }
  388. | WHILE "(" expression ")" block
  389. { $$ = Carbon::Statement::MakeWhile(yylineno, $3, $5); }
  390. | BREAK ";"
  391. { $$ = Carbon::Statement::MakeBreak(yylineno); }
  392. | CONTINUE ";"
  393. { $$ = Carbon::Statement::MakeContinue(yylineno); }
  394. | RETURN return_expression ";"
  395. { $$ = Carbon::Statement::MakeReturn(yylineno, $2.first, $2.second); }
  396. | block
  397. { $$ = $1; }
  398. | MATCH "(" expression ")" "{" clause_list "}"
  399. { $$ = Carbon::Statement::MakeMatch(yylineno, $3, $6); }
  400. | CONTINUATION identifier statement
  401. { $$ = Carbon::Statement::MakeContinuation(yylineno, $2, $3); }
  402. | RUN expression ";"
  403. { $$ = Carbon::Statement::MakeRun(yylineno, $2); }
  404. | AWAIT ";"
  405. { $$ = Carbon::Statement::MakeAwait(yylineno); }
  406. ;
  407. if_statement:
  408. IF "(" expression ")" block optional_else
  409. { $$ = Carbon::Statement::MakeIf(yylineno, $3, $5, $6); }
  410. ;
  411. optional_else:
  412. // Empty
  413. { $$ = 0; }
  414. | ELSE if_statement
  415. { $$ = $2; }
  416. | ELSE block
  417. { $$ = $2; }
  418. ;
  419. return_expression:
  420. // Empty
  421. { $$ = {Carbon::Expression::MakeTupleLiteral(yylineno, {}), true}; }
  422. | expression
  423. { $$ = {$1, false}; }
  424. ;
  425. statement_list:
  426. // Empty
  427. { $$ = 0; }
  428. | statement statement_list
  429. { $$ = Carbon::Statement::MakeSequence(yylineno, $1, $2); }
  430. ;
  431. block:
  432. "{" statement_list "}"
  433. { $$ = Carbon::Statement::MakeBlock(yylineno, $2); }
  434. ;
  435. return_type:
  436. // Empty
  437. { $$ = {Carbon::Expression::MakeTupleLiteral(yylineno, {}), true}; }
  438. | ARROW expression %prec FNARROW
  439. { $$ = {$2, false}; }
  440. ;
  441. generic_binding:
  442. identifier ":!" expression
  443. {
  444. $$ = Carbon::GenericBinding({.name = std::move($1), .type = $3});
  445. }
  446. ;
  447. deduced_param_list:
  448. // Empty
  449. { $$ = std::vector<Carbon::GenericBinding>(); }
  450. | generic_binding
  451. {
  452. $$ = std::vector<Carbon::GenericBinding>();
  453. $$.push_back($1);
  454. }
  455. | generic_binding "," deduced_param_list
  456. {
  457. $$ = $3;
  458. $$.push_back($1);
  459. }
  460. ;
  461. deduced_params:
  462. // Empty
  463. { $$ = std::vector<Carbon::GenericBinding>(); }
  464. | "[" deduced_param_list "]"
  465. { $$ = $2; }
  466. ;
  467. function_definition:
  468. FN identifier deduced_params maybe_empty_tuple_pattern return_type block
  469. {
  470. $$ = Carbon::FunctionDefinition(
  471. yylineno, $2, $3, $4,
  472. Carbon::global_arena->New<Carbon::ExpressionPattern>($5.first),
  473. $5.second, $6);
  474. }
  475. | FN identifier deduced_params maybe_empty_tuple_pattern DBLARROW expression ";"
  476. {
  477. $$ = Carbon::FunctionDefinition(
  478. yylineno, $2, $3, $4,
  479. Carbon::global_arena->New<Carbon::AutoPattern>(yylineno), true,
  480. Carbon::Statement::MakeReturn(yylineno, $6, false));
  481. }
  482. ;
  483. function_declaration:
  484. FN identifier deduced_params maybe_empty_tuple_pattern return_type ";"
  485. {
  486. $$ = Carbon::FunctionDefinition(
  487. yylineno, $2, $3, $4,
  488. Carbon::global_arena->New<Carbon::ExpressionPattern>($5.first),
  489. $5.second, nullptr); }
  490. ;
  491. variable_declaration: identifier ":" pattern
  492. { $$ = Carbon::global_arena->New<Carbon::BindingPattern>(yylineno, $1, $3); }
  493. ;
  494. member: VAR variable_declaration ";"
  495. { $$ = Carbon::Member::MakeFieldMember(yylineno, $2); }
  496. ;
  497. member_list:
  498. // Empty
  499. { $$ = std::list<Carbon::Member*>(); }
  500. | member member_list
  501. { $$ = $2; $$.push_front($1); }
  502. ;
  503. alternative:
  504. identifier tuple
  505. { $$ = std::pair<std::string, const Carbon::Expression*>($1, $2); }
  506. | identifier
  507. {
  508. $$ = std::pair<std::string, const Carbon::Expression*>(
  509. $1, Carbon::Expression::MakeTupleLiteral(yylineno, {}));
  510. }
  511. ;
  512. alternative_list:
  513. // Empty
  514. { $$ = std::list<std::pair<std::string, const Carbon::Expression*>>(); }
  515. | alternative
  516. {
  517. $$ = std::list<std::pair<std::string, const Carbon::Expression*>>();
  518. $$.push_front($1);
  519. }
  520. | alternative "," alternative_list
  521. { $$ = std::move($3); $$.push_front($1); }
  522. ;
  523. declaration:
  524. function_definition
  525. { $$ = Carbon::Declaration::MakeFunctionDeclaration(std::move($1)); }
  526. | function_declaration
  527. { $$ = Carbon::Declaration::MakeFunctionDeclaration(std::move($1)); }
  528. | STRUCT identifier "{" member_list "}"
  529. {
  530. $$ = Carbon::Declaration::MakeStructDeclaration(yylineno, $2, $4);
  531. }
  532. | CHOICE identifier "{" alternative_list "}"
  533. {
  534. $$ = Carbon::Declaration::MakeChoiceDeclaration(yylineno, $2, $4);
  535. }
  536. | VAR variable_declaration "=" expression ";"
  537. {
  538. $$ = Carbon::Declaration::MakeVariableDeclaration(yylineno, $2, $4);
  539. }
  540. ;
  541. declaration_list:
  542. // Empty
  543. { $$ = std::list<Carbon::Declaration>(); }
  544. | declaration declaration_list
  545. {
  546. $$ = $2;
  547. $$.push_front($1);
  548. }
  549. ;
  550. %%