parser.ypp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 <Carbon::Statement*> statement
  77. %type <Carbon::Statement*> optional_else
  78. %type <Carbon::Statement*> statement_list
  79. %type <Carbon::Expression*> expression
  80. %type <Carbon::Expression*> pattern
  81. %type <Carbon::Expression*> return_type
  82. %type <Carbon::Expression*> paren_expression
  83. %type <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, Carbon::Expression*>*> alternative
  91. %type <std::list<std::pair<std::string, Carbon::Expression*>>*> alternative_list
  92. %type <std::pair<Carbon::Expression*, Carbon::Statement*>*> clause
  93. %type <std::list<std::pair<Carbon::Expression*, 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 VAR
  105. %token EQUAL_EQUAL
  106. %token IF
  107. %token ELSE
  108. %token WHILE
  109. %token CONTINUATION_TYPE
  110. %token CONTINUATION
  111. %token RUN
  112. %token AWAIT
  113. %token BREAK
  114. %token CONTINUE
  115. %token RETURN
  116. %token TRUE
  117. %token FALSE
  118. %token STRUCT
  119. %token CHOICE
  120. %token MATCH
  121. %token CASE
  122. %token DBLARROW
  123. %token DEFAULT
  124. %token AUTO
  125. %token
  126. EQUAL "="
  127. MINUS "-"
  128. PLUS "+"
  129. STAR "*"
  130. SLASH "/"
  131. LEFT_PARENTHESIS "("
  132. RIGHT_PARENTHESIS ")"
  133. LEFT_CURLY_BRACE "{"
  134. RIGHT_CURLY_BRACE "}"
  135. LEFT_SQUARE_BRACKET "["
  136. RIGHT_SQUARE_BRACKET "]"
  137. PERIOD "."
  138. COMMA ","
  139. SEMICOLON ";"
  140. COLON ":"
  141. ;
  142. %precedence "{" "}"
  143. %precedence ":" "," DBLARROW
  144. %left OR AND
  145. %nonassoc EQUAL_EQUAL
  146. %left "+" "-"
  147. %precedence NOT UNARY_MINUS
  148. %left "." ARROW
  149. %precedence "(" ")" "[" "]"
  150. %start input
  151. %locations
  152. %%
  153. input: declaration_list
  154. { parsed_program = $1; }
  155. ;
  156. pattern:
  157. expression
  158. { $$ = $1; }
  159. ;
  160. expression:
  161. identifier
  162. { $$ = Carbon::MakeVar(yylineno, $1); }
  163. | expression designator
  164. { $$ = Carbon::MakeGetField(yylineno, $1, $2); }
  165. | expression "[" expression "]"
  166. { $$ = Carbon::MakeIndex(yylineno, $1, $3); }
  167. | expression ":" identifier
  168. { $$ = Carbon::MakeVarPat(yylineno, $3, $1); }
  169. | integer_literal
  170. { $$ = Carbon::MakeInt(yylineno, $1); }
  171. | TRUE
  172. { $$ = Carbon::MakeBool(yylineno, true); }
  173. | FALSE
  174. { $$ = Carbon::MakeBool(yylineno, false); }
  175. | INT
  176. { $$ = Carbon::MakeIntType(yylineno); }
  177. | BOOL
  178. { $$ = Carbon::MakeBoolType(yylineno); }
  179. | TYPE
  180. { $$ = Carbon::MakeTypeType(yylineno); }
  181. | AUTO
  182. { $$ = Carbon::MakeAutoType(yylineno); }
  183. | CONTINUATION_TYPE
  184. { $$ = Carbon::MakeContinuationType(yylineno); }
  185. | paren_expression { $$ = $1; }
  186. | expression EQUAL_EQUAL expression
  187. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Eq, $1, $3); }
  188. | expression "+" expression
  189. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Add, $1, $3); }
  190. | expression "-" expression
  191. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Sub, $1, $3); }
  192. | expression AND expression
  193. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::And, $1, $3); }
  194. | expression OR expression
  195. { $$ = Carbon::MakeBinOp(yylineno, Carbon::Operator::Or, $1, $3); }
  196. | NOT expression
  197. { $$ = Carbon::MakeUnOp(yylineno, Carbon::Operator::Not, $2); }
  198. | "-" expression %prec UNARY_MINUS
  199. { $$ = Carbon::MakeUnOp(yylineno, Carbon::Operator::Neg, $2); }
  200. | expression tuple
  201. { $$ = Carbon::MakeCall(yylineno, $1, $2); }
  202. | FNTY tuple return_type
  203. { $$ = Carbon::MakeFunType(yylineno, $2, $3); }
  204. ;
  205. designator: "." identifier { $$ = $2; }
  206. ;
  207. paren_expression: "(" paren_contents ")"
  208. { $$ = $2.AsExpression(yylineno); }
  209. ;
  210. tuple: "(" paren_contents ")"
  211. { $$ = $2.AsTuple(yylineno); }
  212. ;
  213. field_initializer:
  214. pattern
  215. { $$ = Carbon::FieldInitializer({"", $1}); }
  216. | designator "=" pattern
  217. { $$ = Carbon::FieldInitializer({$1, $3}); }
  218. ;
  219. paren_contents:
  220. // Empty
  221. { $$ = Carbon::ParenContents(); }
  222. | paren_contents_without_trailing_comma
  223. {
  224. $$ = Carbon::ParenContents($1,
  225. Carbon::ParenContents::HasTrailingComma::No);
  226. }
  227. | paren_contents_without_trailing_comma ","
  228. {
  229. $$ = Carbon::ParenContents($1,
  230. Carbon::ParenContents::HasTrailingComma::Yes);
  231. }
  232. ;
  233. paren_contents_without_trailing_comma:
  234. field_initializer
  235. { $$ = {$1}; }
  236. | paren_contents_without_trailing_comma "," field_initializer
  237. {
  238. $$ = $1;
  239. $$.push_back($3);
  240. }
  241. ;
  242. clause:
  243. CASE pattern DBLARROW statement
  244. { $$ = new std::pair<Carbon::Expression*, Carbon::Statement*>($2, $4); }
  245. | DEFAULT DBLARROW statement
  246. {
  247. auto vp = Carbon::MakeVarPat(yylineno, "_",
  248. Carbon::MakeAutoType(yylineno));
  249. $$ = new std::pair<Carbon::Expression*, Carbon::Statement*>(vp, $3);
  250. }
  251. ;
  252. clause_list:
  253. // Empty
  254. {
  255. $$ = new std::list<std::pair<Carbon::Expression*, Carbon::Statement*>>();
  256. }
  257. | clause clause_list
  258. { $$ = $2; $$->push_front(*$1); }
  259. ;
  260. statement:
  261. expression "=" expression ";"
  262. { $$ = Carbon::MakeAssign(yylineno, $1, $3); }
  263. | VAR pattern "=" expression ";"
  264. { $$ = Carbon::MakeVarDef(yylineno, $2, $4); }
  265. | expression ";"
  266. { $$ = Carbon::MakeExpStmt(yylineno, $1); }
  267. | IF "(" expression ")" statement optional_else
  268. { $$ = Carbon::MakeIf(yylineno, $3, $5, $6); }
  269. | WHILE "(" expression ")" statement
  270. { $$ = Carbon::MakeWhile(yylineno, $3, $5); }
  271. | BREAK ";"
  272. { $$ = Carbon::MakeBreak(yylineno); }
  273. | CONTINUE ";"
  274. { $$ = Carbon::MakeContinue(yylineno); }
  275. | RETURN expression ";"
  276. { $$ = Carbon::MakeReturn(yylineno, $2); }
  277. | "{" statement_list "}"
  278. { $$ = Carbon::MakeBlock(yylineno, $2); }
  279. | MATCH "(" expression ")" "{" clause_list "}"
  280. { $$ = Carbon::MakeMatch(yylineno, $3, $6); }
  281. | CONTINUATION identifier statement
  282. { $$ = Carbon::MakeContinuationStatement(yylineno, $2, $3); }
  283. | RUN expression ";"
  284. { $$ = Carbon::MakeRun(yylineno, $2); }
  285. | AWAIT ";"
  286. { $$ = Carbon::MakeAwait(yylineno); }
  287. ;
  288. optional_else:
  289. // Empty
  290. { $$ = 0; }
  291. | ELSE statement { $$ = $2; }
  292. ;
  293. statement_list:
  294. // Empty
  295. { $$ = 0; }
  296. | statement statement_list
  297. { $$ = Carbon::MakeSeq(yylineno, $1, $2); }
  298. ;
  299. return_type:
  300. // Empty
  301. {
  302. $$ = Carbon::MakeTuple(
  303. yylineno,
  304. new std::vector<std::pair<std::string, Carbon::Expression*>>());
  305. }
  306. | ARROW expression
  307. { $$ = $2; }
  308. ;
  309. function_definition:
  310. FN identifier tuple return_type "{" statement_list "}"
  311. { $$ = MakeFunDef(yylineno, $2, $4, $3, $6); }
  312. | FN identifier tuple DBLARROW expression ";"
  313. {
  314. $$ = Carbon::MakeFunDef(yylineno, $2, Carbon::MakeAutoType(yylineno), $3,
  315. Carbon::MakeReturn(yylineno, $5));
  316. }
  317. ;
  318. function_declaration:
  319. FN identifier tuple return_type ";"
  320. { $$ = MakeFunDef(yylineno, $2, $4, $3, 0); }
  321. ;
  322. variable_declaration: expression ":" identifier
  323. { $$ = MakeField(yylineno, $3, $1); }
  324. ;
  325. member: VAR variable_declaration ";"
  326. { $$ = $2; }
  327. ;
  328. member_list:
  329. // Empty
  330. { $$ = new std::list<Carbon::Member*>(); }
  331. | member member_list
  332. { $$ = $2; $$->push_front($1); }
  333. ;
  334. alternative:
  335. identifier tuple
  336. { $$ = new std::pair<std::string, Carbon::Expression*>($1, $2); }
  337. | identifier
  338. {
  339. $$ = new std::pair<std::string, Carbon::Expression*>(
  340. $1, Carbon::MakeTuple(
  341. yylineno,
  342. new std::vector<std::pair<std::string, Carbon::Expression*>>()));
  343. }
  344. ;
  345. alternative_list:
  346. // Empty
  347. { $$ = new std::list<std::pair<std::string, Carbon::Expression*>>(); }
  348. | alternative
  349. {
  350. $$ = new std::list<std::pair<std::string, Carbon::Expression*>>();
  351. $$->push_front(*$1);
  352. }
  353. | alternative "," alternative_list
  354. { $$ = $3; $$->push_front(*$1); }
  355. ;
  356. declaration:
  357. function_definition
  358. { $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
  359. | function_declaration
  360. { $$ = new Carbon::Declaration(Carbon::FunctionDeclaration{$1}); }
  361. | STRUCT identifier "{" member_list "}"
  362. {
  363. $$ = new Carbon::Declaration(
  364. Carbon::StructDeclaration{yylineno, $2, $4});
  365. }
  366. | CHOICE identifier "{" alternative_list "}"
  367. {
  368. $$ = new Carbon::Declaration(
  369. Carbon::ChoiceDeclaration{yylineno, $2, std::list(*$4)});
  370. }
  371. | VAR variable_declaration "=" expression ";"
  372. {
  373. $$ = new Carbon::Declaration(
  374. Carbon::VariableDeclaration(yylineno, *$2->u.field.name, $2->u.field.type, $4));
  375. }
  376. ;
  377. declaration_list:
  378. // Empty
  379. { $$ = new std::list<Carbon::Declaration>(); }
  380. | declaration declaration_list
  381. {
  382. $$ = $2;
  383. $$->push_front(*$1);
  384. }
  385. ;
  386. %%