parser.ypp 18 KB

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