parser.ypp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. // The following shift-reduce conflicts are expected; any others should be
  30. // treated as errors:
  31. // - The "dangling else" ambiguity: `if (b) if (c) x = 1; else x = 2;`
  32. // could parse as either `if (b) { if (c) x = 1; else x = 2;}` or
  33. // `if (b) { if (c) x = 1; } else x = 2;`. Following C++, we want Carbon
  34. // to choose the first option. Resolving this by restructuring the grammar
  35. // would make it harder to read, and resolving it by assigning precedence to
  36. // `if` and `else` could mask other ambiguities, especially if we allow
  37. // `if`/`else` in expressions, so we allow Bison to resolve it through its
  38. // default behavior of preferring to shift rather than reduce.
  39. %expect 1
  40. // -----------------------------------------------------------------------------
  41. %code top {
  42. #include <algorithm>
  43. #include <cstdarg>
  44. #include <cstdio>
  45. #include <cstdlib>
  46. #include <iostream>
  47. #include <list>
  48. #include "executable_semantics/syntax/syntax_helpers.h"
  49. #include "executable_semantics/syntax/parse_and_lex_context.h"
  50. } // %code top
  51. %code requires {
  52. #include <optional>
  53. #include "executable_semantics/ast/abstract_syntax_tree.h"
  54. #include "executable_semantics/ast/declaration.h"
  55. #include "executable_semantics/ast/function_definition.h"
  56. #include "executable_semantics/syntax/paren_contents.h"
  57. namespace Carbon {
  58. class ParseAndLexContext;
  59. } // namespace Carbon
  60. } // %code requires
  61. %code {
  62. extern int yylineno;
  63. void yy::parser::error(const location_type&, const std::string& message) {
  64. context.PrintDiagnostic(message, yylineno);
  65. }
  66. } // %code
  67. %token <int> integer_literal
  68. %token <char*> identifier
  69. %type <char*> designator
  70. %type <Carbon::Declaration> declaration
  71. %type <Carbon::FunctionDefinition> function_declaration
  72. %type <Carbon::FunctionDefinition> function_definition
  73. %type <std::list<Carbon::Declaration>> declaration_list
  74. %type <const Carbon::Statement*> statement
  75. %type <const Carbon::Statement*> optional_else
  76. %type <const Carbon::Statement*> statement_list
  77. %type <const Carbon::Expression*> expression
  78. %type <Carbon::GenericBinding> generic_binding
  79. %type <std::vector<Carbon::GenericBinding>> deduced_params
  80. %type <std::vector<Carbon::GenericBinding>> deduced_param_list
  81. %type <const Carbon::Expression*> pattern
  82. %type <const Carbon::Expression*> return_type
  83. %type <const Carbon::Expression*> paren_expression
  84. %type <const Carbon::Expression*> tuple
  85. %type <std::optional<std::string>> binding_lhs
  86. %type <Carbon::Member*> variable_declaration
  87. %type <Carbon::Member*> member
  88. %type <std::list<Carbon::Member*>> member_list
  89. %type <Carbon::FieldInitializer> field_initializer
  90. %type <Carbon::ParenContents> paren_contents
  91. %type <std::vector<Carbon::FieldInitializer>> paren_contents_without_trailing_comma
  92. %type <std::pair<std::string, const Carbon::Expression*>> alternative
  93. %type <std::list<std::pair<std::string, const Carbon::Expression*>>> alternative_list
  94. %type <std::pair<const Carbon::Expression*, const Carbon::Statement*>*> clause
  95. %type <std::list<std::pair<const Carbon::Expression*, const Carbon::Statement*>>*> clause_list
  96. %token END_OF_FILE 0
  97. %token AND
  98. %token OR
  99. %token NOT
  100. %token INT
  101. %token BOOL
  102. %token TYPE
  103. %token FN
  104. %token FNTY
  105. %token ARROW "->"
  106. %token FNARROW "-> in return type"
  107. %token VAR
  108. %token EQUAL_EQUAL
  109. %token IF
  110. %token ELSE
  111. %token WHILE
  112. %token CONTINUATION_TYPE
  113. %token CONTINUATION
  114. %token RUN
  115. %token AWAIT
  116. %token BREAK
  117. %token CONTINUE
  118. %token RETURN
  119. %token TRUE
  120. %token FALSE
  121. %token STRUCT
  122. %token CHOICE
  123. %token MATCH
  124. %token CASE
  125. %token DBLARROW "=>"
  126. %token DEFAULT
  127. %token AUTO
  128. %token UNDERSCORE
  129. %token
  130. EQUAL "="
  131. MINUS "-"
  132. PLUS "+"
  133. // The lexer determines the arity and fixity of each `*` based on whitespace
  134. // and adjacent tokens. UNARY_STAR indicates that the operator is unary but
  135. // could be either prefix or postfix.
  136. UNARY_STAR "unary *"
  137. PREFIX_STAR "prefix *"
  138. POSTFIX_STAR "postfix *"
  139. BINARY_STAR "binary *"
  140. SLASH "/"
  141. LEFT_PARENTHESIS "("
  142. RIGHT_PARENTHESIS ")"
  143. LEFT_CURLY_BRACE "{"
  144. RIGHT_CURLY_BRACE "}"
  145. LEFT_SQUARE_BRACKET "["
  146. RIGHT_SQUARE_BRACKET "]"
  147. PERIOD "."
  148. COMMA ","
  149. SEMICOLON ";"
  150. COLON_BANG ":!"
  151. COLON ":"
  152. ;
  153. %precedence FNARROW
  154. %precedence "{" "}"
  155. %precedence ":!" ":" "," DBLARROW
  156. %left OR AND
  157. %nonassoc EQUAL_EQUAL
  158. %left "+" "-"
  159. %left BINARY_STAR
  160. %precedence NOT UNARY_MINUS PREFIX_STAR
  161. // We need to give the `UNARY_STAR` token a precedence, rather than overriding
  162. // the precedence of the `expression UNARY_STAR` rule below, because bison
  163. // compares the precedence of the final token (for a shift) to the precedence
  164. // of the other rule (for a reduce) when attempting to resolve a shift-reduce
  165. // conflict. See https://stackoverflow.com/a/26188429/1041090. When UNARY_STAR
  166. // is the final token of a rule, it must be a postfix usage, so we give it the
  167. // same precedence as POSTFIX_STAR.
  168. %precedence POSTFIX_STAR UNARY_STAR
  169. %left "." ARROW
  170. %precedence "(" ")" "[" "]"
  171. %start input
  172. %locations
  173. %%
  174. input: declaration_list
  175. { parsed_program = $1; }
  176. ;
  177. pattern:
  178. expression
  179. { $$ = $1; }
  180. ;
  181. binding_lhs:
  182. identifier { $$ = $1; }
  183. | UNDERSCORE { $$ = std::nullopt; }
  184. ;
  185. expression:
  186. identifier
  187. { $$ = Carbon::Expression::MakeIdentifierExpression(yylineno, $1); }
  188. | expression designator
  189. { $$ = Carbon::Expression::MakeFieldAccessExpression(yylineno, $1, $2); }
  190. | expression "[" expression "]"
  191. { $$ = Carbon::Expression::MakeIndexExpression(yylineno, $1, $3); }
  192. | binding_lhs ":" expression
  193. {
  194. $$ = Carbon::Expression::MakeBindingExpression(yylineno, $1, $3);
  195. }
  196. | integer_literal
  197. { $$ = Carbon::Expression::MakeIntLiteral(yylineno, $1); }
  198. | TRUE
  199. { $$ = Carbon::Expression::MakeBoolLiteral(yylineno, true); }
  200. | FALSE
  201. { $$ = Carbon::Expression::MakeBoolLiteral(yylineno, false); }
  202. | INT
  203. { $$ = Carbon::Expression::MakeIntTypeLiteral(yylineno); }
  204. | BOOL
  205. { $$ = Carbon::Expression::MakeBoolTypeLiteral(yylineno); }
  206. | TYPE
  207. { $$ = Carbon::Expression::MakeTypeTypeLiteral(yylineno); }
  208. | AUTO
  209. { $$ = Carbon::Expression::MakeAutoTypeLiteral(yylineno); }
  210. | CONTINUATION_TYPE
  211. { $$ = Carbon::Expression::MakeContinuationTypeLiteral(yylineno); }
  212. | paren_expression { $$ = $1; }
  213. | expression EQUAL_EQUAL expression
  214. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  215. yylineno, Carbon::Operator::Eq, {$1, $3}); }
  216. | expression "+" expression
  217. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  218. yylineno, Carbon::Operator::Add, {$1, $3}); }
  219. | expression "-" expression
  220. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  221. yylineno, Carbon::Operator::Sub, {$1, $3}); }
  222. | expression BINARY_STAR expression
  223. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  224. yylineno, Carbon::Operator::Mul, {$1, $3}); }
  225. | expression AND expression
  226. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  227. yylineno, Carbon::Operator::And, {$1, $3}); }
  228. | expression OR expression
  229. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  230. yylineno, Carbon::Operator::Or, {$1, $3}); }
  231. | NOT expression
  232. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  233. yylineno, Carbon::Operator::Not, {$2}); }
  234. | "-" expression %prec UNARY_MINUS
  235. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  236. yylineno, Carbon::Operator::Neg, {$2}); }
  237. | PREFIX_STAR expression
  238. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  239. yylineno, Carbon::Operator::Deref, {$2}); }
  240. | UNARY_STAR expression %prec PREFIX_STAR
  241. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  242. yylineno, Carbon::Operator::Deref, {$2}); }
  243. | expression tuple
  244. { $$ = Carbon::Expression::MakeCallExpression(yylineno, $1, $2); }
  245. | expression POSTFIX_STAR
  246. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  247. yylineno, Carbon::Operator::Ptr, {$1}); }
  248. | expression UNARY_STAR
  249. { $$ = Carbon::Expression::MakePrimitiveOperatorExpression(
  250. yylineno, Carbon::Operator::Ptr, {$1}); }
  251. | FNTY tuple return_type
  252. { $$ = Carbon::Expression::MakeFunctionTypeLiteral(yylineno, $2, $3); }
  253. ;
  254. designator: "." identifier { $$ = $2; }
  255. ;
  256. paren_expression: "(" paren_contents ")"
  257. { $$ = $2.AsExpression(yylineno); }
  258. ;
  259. tuple: "(" paren_contents ")"
  260. { $$ = $2.AsTuple(yylineno); }
  261. ;
  262. field_initializer:
  263. pattern
  264. { $$ = Carbon::FieldInitializer({"", $1}); }
  265. | designator "=" pattern
  266. { $$ = Carbon::FieldInitializer({$1, $3}); }
  267. ;
  268. paren_contents:
  269. // Empty
  270. { $$ = Carbon::ParenContents(); }
  271. | paren_contents_without_trailing_comma
  272. {
  273. $$ = Carbon::ParenContents($1,
  274. Carbon::ParenContents::HasTrailingComma::No);
  275. }
  276. | paren_contents_without_trailing_comma ","
  277. {
  278. $$ = Carbon::ParenContents($1,
  279. Carbon::ParenContents::HasTrailingComma::Yes);
  280. }
  281. ;
  282. paren_contents_without_trailing_comma:
  283. field_initializer
  284. { $$ = {$1}; }
  285. | paren_contents_without_trailing_comma "," field_initializer
  286. {
  287. $$ = $1;
  288. $$.push_back($3);
  289. }
  290. ;
  291. clause:
  292. CASE pattern DBLARROW statement
  293. { $$ = new std::pair<const Carbon::Expression*, const Carbon::Statement*>($2, $4); }
  294. | DEFAULT DBLARROW statement
  295. {
  296. auto vp = Carbon::Expression::MakeBindingExpression(
  297. yylineno, "_", Carbon::Expression::MakeAutoTypeLiteral(yylineno));
  298. $$ = new std::pair<const Carbon::Expression*, const Carbon::Statement*>(vp, $3);
  299. }
  300. ;
  301. clause_list:
  302. // Empty
  303. {
  304. $$ = new std::list<std::pair<const Carbon::Expression*, const Carbon::Statement*>>();
  305. }
  306. | clause clause_list
  307. { $$ = $2; $$->push_front(*$1); }
  308. ;
  309. statement:
  310. expression "=" expression ";"
  311. { $$ = Carbon::Statement::MakeAssign(yylineno, $1, $3); }
  312. | VAR pattern "=" expression ";"
  313. { $$ = Carbon::Statement::MakeVariableDefinition(yylineno, $2, $4); }
  314. | expression ";"
  315. { $$ = Carbon::Statement::MakeExpressionStatement(yylineno, $1); }
  316. | IF "(" expression ")" statement optional_else
  317. { $$ = Carbon::Statement::MakeIf(yylineno, $3, $5, $6); }
  318. | WHILE "(" expression ")" statement
  319. { $$ = Carbon::Statement::MakeWhile(yylineno, $3, $5); }
  320. | BREAK ";"
  321. { $$ = Carbon::Statement::MakeBreak(yylineno); }
  322. | CONTINUE ";"
  323. { $$ = Carbon::Statement::MakeContinue(yylineno); }
  324. | RETURN expression ";"
  325. { $$ = Carbon::Statement::MakeReturn(yylineno, $2); }
  326. | "{" statement_list "}"
  327. { $$ = Carbon::Statement::MakeBlock(yylineno, $2); }
  328. | MATCH "(" expression ")" "{" clause_list "}"
  329. { $$ = Carbon::Statement::MakeMatch(yylineno, $3, $6); }
  330. | CONTINUATION identifier statement
  331. { $$ = Carbon::Statement::MakeContinuation(yylineno, $2, $3); }
  332. | RUN expression ";"
  333. { $$ = Carbon::Statement::MakeRun(yylineno, $2); }
  334. | AWAIT ";"
  335. { $$ = Carbon::Statement::MakeAwait(yylineno); }
  336. ;
  337. optional_else:
  338. // Empty
  339. { $$ = 0; }
  340. | ELSE statement { $$ = $2; }
  341. ;
  342. statement_list:
  343. // Empty
  344. { $$ = 0; }
  345. | statement statement_list
  346. { $$ = Carbon::Statement::MakeSequence(yylineno, $1, $2); }
  347. ;
  348. return_type:
  349. // Empty
  350. { $$ = Carbon::Expression::MakeTupleLiteral(yylineno, {}); }
  351. | ARROW expression %prec FNARROW
  352. { $$ = $2; }
  353. ;
  354. generic_binding:
  355. identifier ":!" expression
  356. {
  357. $$ = Carbon::GenericBinding({.name = std::move($1), .type = $3});
  358. }
  359. ;
  360. deduced_param_list:
  361. // Empty
  362. { $$ = std::vector<Carbon::GenericBinding>(); }
  363. | generic_binding
  364. {
  365. $$ = std::vector<Carbon::GenericBinding>();
  366. $$.push_back($1);
  367. }
  368. | generic_binding "," deduced_param_list
  369. {
  370. $$ = $3;
  371. $$.push_back($1);
  372. }
  373. ;
  374. deduced_params:
  375. // Empty
  376. { $$ = std::vector<Carbon::GenericBinding>(); }
  377. | "[" deduced_param_list "]"
  378. { $$ = $2; }
  379. ;
  380. function_definition:
  381. FN identifier deduced_params tuple return_type "{" statement_list "}"
  382. { $$ = Carbon::FunctionDefinition(yylineno, $2, $3, $4, $5, $7); }
  383. | FN identifier deduced_params tuple DBLARROW expression ";"
  384. {
  385. $$ = Carbon::FunctionDefinition(
  386. yylineno, $2, $3, $4,
  387. Carbon::Expression::MakeAutoTypeLiteral(yylineno),
  388. Carbon::Statement::MakeReturn(yylineno, $6));
  389. }
  390. ;
  391. function_declaration:
  392. FN identifier deduced_params tuple return_type ";"
  393. { $$ = Carbon::FunctionDefinition(yylineno, $2, $3, $4, $5, 0); }
  394. ;
  395. variable_declaration: identifier ":" expression
  396. { $$ = Carbon::Member::MakeFieldMember(yylineno, $1, $3); }
  397. ;
  398. member: VAR variable_declaration ";"
  399. { $$ = $2; }
  400. ;
  401. member_list:
  402. // Empty
  403. { $$ = std::list<Carbon::Member*>(); }
  404. | member member_list
  405. { $$ = $2; $$.push_front($1); }
  406. ;
  407. alternative:
  408. identifier tuple
  409. { $$ = std::pair<std::string, const Carbon::Expression*>($1, $2); }
  410. | identifier
  411. {
  412. $$ = std::pair<std::string, const Carbon::Expression*>(
  413. $1, Carbon::Expression::MakeTupleLiteral(yylineno, {}));
  414. }
  415. ;
  416. alternative_list:
  417. // Empty
  418. { $$ = std::list<std::pair<std::string, const Carbon::Expression*>>(); }
  419. | alternative
  420. {
  421. $$ = std::list<std::pair<std::string, const Carbon::Expression*>>();
  422. $$.push_front($1);
  423. }
  424. | alternative "," alternative_list
  425. { $$ = std::move($3); $$.push_front($1); }
  426. ;
  427. declaration:
  428. function_definition
  429. { $$ = Carbon::Declaration::MakeFunctionDeclaration(std::move($1)); }
  430. | function_declaration
  431. { $$ = Carbon::Declaration::MakeFunctionDeclaration(std::move($1)); }
  432. | STRUCT identifier "{" member_list "}"
  433. {
  434. $$ = Carbon::Declaration::MakeStructDeclaration(yylineno, $2, $4);
  435. }
  436. | CHOICE identifier "{" alternative_list "}"
  437. {
  438. $$ = Carbon::Declaration::MakeChoiceDeclaration(yylineno, $2, $4);
  439. }
  440. | VAR variable_declaration "=" expression ";"
  441. {
  442. $$ = Carbon::Declaration::MakeVariableDeclaration(
  443. yylineno, $2->GetFieldMember().name, $2->GetFieldMember().type, $4);
  444. }
  445. ;
  446. declaration_list:
  447. // Empty
  448. { $$ = std::list<Carbon::Declaration>(); }
  449. | declaration declaration_list
  450. {
  451. $$ = $2;
  452. $$.push_front($1);
  453. }
  454. ;
  455. %%