parser.ypp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. }
  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. }
  60. }
  61. %code {
  62. extern int yylineno;
  63. void yy::parser::error(
  64. const location_type&, const std::string& message)
  65. {
  66. context.PrintDiagnostic(message, yylineno);
  67. }
  68. }
  69. %token <int> integer_literal
  70. %token <char*> identifier
  71. %type <char*> designator
  72. %type <Carbon::Declaration*> declaration
  73. %type <Carbon::FunctionDefinition*> function_declaration
  74. %type <Carbon::FunctionDefinition*> function_definition
  75. %type <std::list<Carbon::Declaration>*> declaration_list
  76. %type <const Carbon::Statement*> statement
  77. %type <const Carbon::Statement*> optional_else
  78. %type <const Carbon::Statement*> statement_list
  79. %type <const Carbon::Expression*> expression
  80. %type <const Carbon::Expression*> pattern
  81. %type <const Carbon::Expression*> return_type
  82. %type <const Carbon::Expression*> paren_expression
  83. %type <const Carbon::Expression*> tuple
  84. %type <Carbon::Member*> variable_declaration
  85. %type <Carbon::Member*> member
  86. %type <std::list<Carbon::Member*>*> member_list
  87. %type <Carbon::FieldInitializer> field_initializer
  88. %type <Carbon::ParenContents> paren_contents
  89. %type <std::vector<Carbon::FieldInitializer>> paren_contents_without_trailing_comma
  90. %type <std::pair<std::string, const Carbon::Expression*>*> alternative
  91. %type <std::list<std::pair<std::string, const Carbon::Expression*>>*> alternative_list
  92. %type <std::pair<const Carbon::Expression*, const Carbon::Statement*>*> clause
  93. %type <std::list<std::pair<const Carbon::Expression*, const Carbon::Statement*>>*> clause_list
  94. %token END_OF_FILE 0
  95. %token AND
  96. %token OR
  97. %token NOT
  98. %token INT
  99. %token BOOL
  100. %token TYPE
  101. %token FN
  102. %token FNTY
  103. %token ARROW "->"
  104. %token FNARROW "-> in return type"
  105. %token VAR
  106. %token EQUAL_EQUAL
  107. %token IF
  108. %token ELSE
  109. %token WHILE
  110. %token CONTINUATION_TYPE
  111. %token CONTINUATION
  112. %token RUN
  113. %token AWAIT
  114. %token BREAK
  115. %token CONTINUE
  116. %token RETURN
  117. %token TRUE
  118. %token FALSE
  119. %token STRUCT
  120. %token CHOICE
  121. %token MATCH
  122. %token CASE
  123. %token DBLARROW "=>"
  124. %token DEFAULT
  125. %token AUTO
  126. %token
  127. EQUAL "="
  128. MINUS "-"
  129. PLUS "+"
  130. // The lexer determines the arity and fixity of each `*` based on whitespace
  131. // and adjacent tokens. UNARY_STAR indicates that the operator is unary but
  132. // could be either prefix or postfix.
  133. UNARY_STAR "unary *"
  134. PREFIX_STAR "prefix *"
  135. POSTFIX_STAR "postfix *"
  136. BINARY_STAR "binary *"
  137. SLASH "/"
  138. LEFT_PARENTHESIS "("
  139. RIGHT_PARENTHESIS ")"
  140. LEFT_CURLY_BRACE "{"
  141. RIGHT_CURLY_BRACE "}"
  142. LEFT_SQUARE_BRACKET "["
  143. RIGHT_SQUARE_BRACKET "]"
  144. PERIOD "."
  145. COMMA ","
  146. SEMICOLON ";"
  147. COLON ":"
  148. ;
  149. %precedence FNARROW
  150. %precedence "{" "}"
  151. %precedence ":" "," DBLARROW
  152. %left OR AND
  153. %nonassoc EQUAL_EQUAL
  154. %left "+" "-"
  155. %left BINARY_STAR
  156. %precedence NOT UNARY_MINUS PREFIX_STAR
  157. // We need to give the `UNARY_STAR` token a precedence, rather than overriding
  158. // the precedence of the `expression UNARY_STAR` rule below, because bison
  159. // compares the precedence of the final token (for a shift) to the precedence
  160. // of the other rule (for a reduce) when attempting to resolve a shift-reduce
  161. // conflict. See https://stackoverflow.com/a/26188429/1041090. When UNARY_STAR
  162. // is the final token of a rule, it must be a postfix usage, so we give it the
  163. // same precedence as POSTFIX_STAR.
  164. %precedence POSTFIX_STAR UNARY_STAR
  165. %left "." ARROW
  166. %precedence "(" ")" "[" "]"
  167. %start input
  168. %locations
  169. %%
  170. input: declaration_list
  171. { parsed_program = $1; }
  172. ;
  173. pattern:
  174. expression
  175. { $$ = $1; }
  176. ;
  177. expression:
  178. identifier
  179. { $$ = Carbon::Expression::MakeVar(yylineno, $1); }
  180. | expression designator
  181. { $$ = Carbon::Expression::MakeGetField(yylineno, $1, $2); }
  182. | expression "[" expression "]"
  183. { $$ = Carbon::Expression::MakeIndex(yylineno, *$1, *$3); }
  184. | identifier ":" expression
  185. { $$ = Carbon::Expression::MakeVarPat(yylineno, $1, *$3); }
  186. | integer_literal
  187. { $$ = Carbon::Expression::MakeInt(yylineno, $1); }
  188. | TRUE
  189. { $$ = Carbon::Expression::MakeBool(yylineno, true); }
  190. | FALSE
  191. { $$ = Carbon::Expression::MakeBool(yylineno, false); }
  192. | INT
  193. { $$ = Carbon::Expression::MakeIntType(yylineno); }
  194. | BOOL
  195. { $$ = Carbon::Expression::MakeBoolType(yylineno); }
  196. | TYPE
  197. { $$ = Carbon::Expression::MakeTypeType(yylineno); }
  198. | AUTO
  199. { $$ = Carbon::Expression::MakeAutoType(yylineno); }
  200. | CONTINUATION_TYPE
  201. { $$ = Carbon::Expression::MakeContinuationType(yylineno); }
  202. | paren_expression { $$ = $1; }
  203. | expression EQUAL_EQUAL expression
  204. { $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Eq, *$1, *$3); }
  205. | expression "+" expression
  206. { $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Add, *$1, *$3); }
  207. | expression "-" expression
  208. { $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Sub, *$1, *$3); }
  209. | expression BINARY_STAR expression
  210. { $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Mul, *$1, *$3); }
  211. | expression AND expression
  212. { $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::And, *$1, *$3); }
  213. | expression OR expression
  214. { $$ = Carbon::Expression::MakeBinOp(yylineno, Carbon::Operator::Or, *$1, *$3); }
  215. | NOT expression
  216. { $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Not, *$2); }
  217. | "-" expression %prec UNARY_MINUS
  218. { $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Neg, *$2); }
  219. | PREFIX_STAR expression
  220. { $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Deref, *$2); }
  221. | UNARY_STAR expression %prec PREFIX_STAR
  222. { $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Deref, *$2); }
  223. | expression tuple
  224. { $$ = Carbon::Expression::MakeCall(yylineno, *$1, *$2); }
  225. | expression POSTFIX_STAR
  226. { $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Ptr, *$1); }
  227. | expression UNARY_STAR
  228. { $$ = Carbon::Expression::MakeUnOp(yylineno, Carbon::Operator::Ptr, *$1); }
  229. | FNTY tuple return_type
  230. { $$ = Carbon::Expression::MakeFunType(yylineno, *$2, *$3); }
  231. ;
  232. designator: "." identifier { $$ = $2; }
  233. ;
  234. paren_expression: "(" paren_contents ")"
  235. { $$ = $2.AsExpression(yylineno); }
  236. ;
  237. tuple: "(" paren_contents ")"
  238. { $$ = $2.AsTuple(yylineno); }
  239. ;
  240. field_initializer:
  241. pattern
  242. { $$ = Carbon::FieldInitializer({"", *$1}); }
  243. | designator "=" pattern
  244. { $$ = Carbon::FieldInitializer({$1, *$3}); }
  245. ;
  246. paren_contents:
  247. // Empty
  248. { $$ = Carbon::ParenContents(); }
  249. | paren_contents_without_trailing_comma
  250. {
  251. $$ = Carbon::ParenContents($1,
  252. Carbon::ParenContents::HasTrailingComma::No);
  253. }
  254. | paren_contents_without_trailing_comma ","
  255. {
  256. $$ = Carbon::ParenContents($1,
  257. Carbon::ParenContents::HasTrailingComma::Yes);
  258. }
  259. ;
  260. paren_contents_without_trailing_comma:
  261. field_initializer
  262. { $$ = {$1}; }
  263. | paren_contents_without_trailing_comma "," field_initializer
  264. {
  265. $$ = $1;
  266. $$.push_back($3);
  267. }
  268. ;
  269. clause:
  270. CASE pattern DBLARROW statement
  271. { $$ = new std::pair<const Carbon::Expression*, const Carbon::Statement*>($2, $4); }
  272. | DEFAULT DBLARROW statement
  273. {
  274. auto vp = Carbon::Expression::MakeVarPat(yylineno, "_",
  275. *Carbon::Expression::MakeAutoType(yylineno));
  276. $$ = new std::pair<const Carbon::Expression*, const Carbon::Statement*>(vp, $3);
  277. }
  278. ;
  279. clause_list:
  280. // Empty
  281. {
  282. $$ = new std::list<std::pair<const Carbon::Expression*, const Carbon::Statement*>>();
  283. }
  284. | clause clause_list
  285. { $$ = $2; $$->push_front(*$1); }
  286. ;
  287. statement:
  288. expression "=" expression ";"
  289. { $$ = Carbon::Statement::MakeAssign(yylineno, $1, $3); }
  290. | VAR pattern "=" expression ";"
  291. { $$ = Carbon::Statement::MakeVarDef(yylineno, $2, $4); }
  292. | expression ";"
  293. { $$ = Carbon::Statement::MakeExpStmt(yylineno, $1); }
  294. | IF "(" expression ")" statement optional_else
  295. { $$ = Carbon::Statement::MakeIf(yylineno, $3, $5, $6); }
  296. | WHILE "(" expression ")" statement
  297. { $$ = Carbon::Statement::MakeWhile(yylineno, $3, $5); }
  298. | BREAK ";"
  299. { $$ = Carbon::Statement::MakeBreak(yylineno); }
  300. | CONTINUE ";"
  301. { $$ = Carbon::Statement::MakeContinue(yylineno); }
  302. | RETURN expression ";"
  303. { $$ = Carbon::Statement::MakeReturn(yylineno, $2); }
  304. | "{" statement_list "}"
  305. { $$ = Carbon::Statement::MakeBlock(yylineno, $2); }
  306. | MATCH "(" expression ")" "{" clause_list "}"
  307. { $$ = Carbon::Statement::MakeMatch(yylineno, $3, $6); }
  308. | CONTINUATION identifier statement
  309. { $$ = Carbon::Statement::MakeContinuation(yylineno, $2, $3); }
  310. | RUN expression ";"
  311. { $$ = Carbon::Statement::MakeRun(yylineno, $2); }
  312. | AWAIT ";"
  313. { $$ = Carbon::Statement::MakeAwait(yylineno); }
  314. ;
  315. optional_else:
  316. // Empty
  317. { $$ = 0; }
  318. | ELSE statement { $$ = $2; }
  319. ;
  320. statement_list:
  321. // Empty
  322. { $$ = 0; }
  323. | statement statement_list
  324. { $$ = Carbon::Statement::MakeSeq(yylineno, $1, $2); }
  325. ;
  326. return_type:
  327. // Empty
  328. { $$ = Carbon::Expression::MakeTuple(yylineno, {}); }
  329. | ARROW expression %prec FNARROW
  330. { $$ = $2; }
  331. ;
  332. function_definition:
  333. FN identifier tuple return_type "{" statement_list "}"
  334. { $$ = MakeFunDef(yylineno, $2, $4, $3, $6); }
  335. | FN identifier tuple DBLARROW expression ";"
  336. {
  337. $$ = Carbon::MakeFunDef(yylineno, $2, Carbon::Expression::MakeAutoType(yylineno), $3,
  338. Carbon::Statement::MakeReturn(yylineno, $5));
  339. }
  340. ;
  341. function_declaration:
  342. FN identifier tuple return_type ";"
  343. { $$ = MakeFunDef(yylineno, $2, $4, $3, 0); }
  344. ;
  345. variable_declaration: identifier ":" expression
  346. { $$ = MakeField(yylineno, $1, $3); }
  347. ;
  348. member: VAR variable_declaration ";"
  349. { $$ = $2; }
  350. ;
  351. member_list:
  352. // Empty
  353. { $$ = new std::list<Carbon::Member*>(); }
  354. | member member_list
  355. { $$ = $2; $$->push_front($1); }
  356. ;
  357. alternative:
  358. identifier tuple
  359. { $$ = new std::pair<std::string, const Carbon::Expression*>($1, $2); }
  360. | identifier
  361. {
  362. $$ = new std::pair<std::string, const Carbon::Expression*>(
  363. $1, Carbon::Expression::MakeTuple(yylineno, {}));
  364. }
  365. ;
  366. alternative_list:
  367. // Empty
  368. { $$ = new std::list<std::pair<std::string, const Carbon::Expression*>>(); }
  369. | alternative
  370. {
  371. $$ = new std::list<std::pair<std::string, const Carbon::Expression*>>();
  372. $$->push_front(*$1);
  373. }
  374. | alternative "," alternative_list
  375. { $$ = $3; $$->push_front(*$1); }
  376. ;
  377. declaration:
  378. function_definition
  379. { $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
  380. | function_declaration
  381. { $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
  382. | STRUCT identifier "{" member_list "}"
  383. {
  384. $$ = new Carbon::Declaration(
  385. Carbon::StructDeclaration{yylineno, $2, $4});
  386. }
  387. | CHOICE identifier "{" alternative_list "}"
  388. {
  389. $$ = new Carbon::Declaration(
  390. Carbon::ChoiceDeclaration{yylineno, $2, std::list(*$4)});
  391. }
  392. | VAR variable_declaration "=" expression ";"
  393. {
  394. $$ = new Carbon::Declaration(
  395. Carbon::VariableDeclaration(yylineno, *$2->u.field.name, $2->u.field.type, $4));
  396. }
  397. ;
  398. declaration_list:
  399. // Empty
  400. { $$ = new std::list<Carbon::Declaration>(); }
  401. | declaration declaration_list
  402. {
  403. $$ = $2;
  404. $$->push_front(*$1);
  405. }
  406. ;
  407. %%