logo

live-bootstrap

Mirror of <https://github.com/fosslinux/live-bootstrap>

parse-gram.y (28943B)


  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /* Bison Grammar Parser -*- C -*-
  3. Copyright (C) 2002-2015, 2018-2019 Free Software Foundation, Inc.
  4. This file is part of Bison, the GNU Compiler Compiler.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * This file is identical to the file parse-gram.y distributed with
  17. * GNU Bison 3.4.1, except that all %printer and %destructor
  18. * directives were removed (and this comment was added). It thus
  19. * accepts the same grammar and produce the same actions, although it
  20. * won't clean up things properly and it will have partially impaired
  21. * printing.
  22. *
  23. * It was produced by Giovanni Mascellani <gio@debian.org> on 26th
  24. * March 2020. Since the modifications are so trivial, no copyright is
  25. * claimed.
  26. */
  27. %code requires
  28. {
  29. #include "symlist.h"
  30. #include "symtab.h"
  31. }
  32. %code top
  33. {
  34. /* On column 0 to please syntax-check. */
  35. #include <config.h>
  36. }
  37. %code
  38. {
  39. #include "system.h"
  40. #include <errno.h>
  41. #include "c-ctype.h"
  42. #include "complain.h"
  43. #include "conflicts.h"
  44. #include "files.h"
  45. #include "getargs.h"
  46. #include "gram.h"
  47. #include "named-ref.h"
  48. #include "quotearg.h"
  49. #include "reader.h"
  50. #include "scan-code.h"
  51. #include "scan-gram.h"
  52. #include "vasnprintf.h"
  53. #include "xmemdup0.h"
  54. static int current_prec = 0;
  55. static location current_lhs_loc;
  56. static named_ref *current_lhs_named_ref;
  57. static symbol *current_lhs_symbol;
  58. static symbol_class current_class = unknown_sym;
  59. /** Set the new current left-hand side symbol, possibly common
  60. * to several right-hand side parts of rule.
  61. */
  62. static void current_lhs (symbol *sym, location loc, named_ref *ref);
  63. #define YYLLOC_DEFAULT(Current, Rhs, N) \
  64. (Current) = lloc_default (Rhs, N)
  65. static YYLTYPE lloc_default (YYLTYPE const *, int);
  66. #define YY_LOCATION_PRINT(File, Loc) \
  67. location_print (Loc, File)
  68. /* Strip initial '{' and final '}' (must be first and last characters).
  69. Return the result. */
  70. static char *strip_braces (char *code);
  71. /* Convert CODE by calling code_props_plain_init if PLAIN, otherwise
  72. code_props_symbol_action_init. Call
  73. gram_scanner_last_string_free to release the latest string from
  74. the scanner (should be CODE). */
  75. static char const *translate_code (char *code, location loc, bool plain);
  76. /* Convert CODE by calling code_props_plain_init after having
  77. stripped the first and last characters (expected to be '{', and
  78. '}'). Call gram_scanner_last_string_free to release the latest
  79. string from the scanner (should be CODE). */
  80. static char const *translate_code_braceless (char *code, location loc);
  81. /* Handle a %error-verbose directive. */
  82. static void handle_error_verbose (location const *loc, char const *directive);
  83. /* Handle a %file-prefix directive. */
  84. static void handle_file_prefix (location const *loc,
  85. location const *dir_loc,
  86. char const *directive, char const *value);
  87. /* Handle a %name-prefix directive. */
  88. static void handle_name_prefix (location const *loc,
  89. char const *directive, char const *value);
  90. /* Handle a %pure-parser directive. */
  91. static void handle_pure_parser (location const *loc, char const *directive);
  92. /* Handle a %require directive. */
  93. static void handle_require (location const *loc, char const *version);
  94. /* Handle a %skeleton directive. */
  95. static void handle_skeleton (location const *loc, char const *skel);
  96. /* Handle a %yacc directive. */
  97. static void handle_yacc (location const *loc, char const *directive);
  98. static void gram_error (location const *, char const *);
  99. /* A string that describes a char (e.g., 'a' -> "'a'"). */
  100. static char const *char_name (char);
  101. #define YYTYPE_INT16 int_fast16_t
  102. #define YYTYPE_INT8 int_fast8_t
  103. #define YYTYPE_UINT16 uint_fast16_t
  104. #define YYTYPE_UINT8 uint_fast8_t
  105. /* Add style to semantic values in traces. */
  106. static void tron (FILE *yyo);
  107. static void troff (FILE *yyo);
  108. }
  109. %define api.header.include {"parse-gram.h"}
  110. %define api.prefix {gram_}
  111. %define api.pure full
  112. %define api.value.type union
  113. %define locations
  114. %define parse.error verbose
  115. %define parse.lac full
  116. %define parse.trace
  117. %defines
  118. %expect 0
  119. %verbose
  120. %initial-action
  121. {
  122. /* Bison's grammar can initial empty locations, hence a default
  123. location is needed. */
  124. boundary_set (&@$.start, current_file, 1, 1, 1);
  125. boundary_set (&@$.end, current_file, 1, 1, 1);
  126. }
  127. /* Define the tokens together with their human representation. */
  128. %token GRAM_EOF 0 "end of file"
  129. %token STRING "string"
  130. %token PERCENT_TOKEN "%token"
  131. %token PERCENT_NTERM "%nterm"
  132. %token PERCENT_TYPE "%type"
  133. %token PERCENT_DESTRUCTOR "%destructor"
  134. %token PERCENT_PRINTER "%printer"
  135. %token PERCENT_LEFT "%left"
  136. %token PERCENT_RIGHT "%right"
  137. %token PERCENT_NONASSOC "%nonassoc"
  138. %token PERCENT_PRECEDENCE "%precedence"
  139. %token PERCENT_PREC "%prec"
  140. %token PERCENT_DPREC "%dprec"
  141. %token PERCENT_MERGE "%merge"
  142. /*----------------------.
  143. | Global Declarations. |
  144. `----------------------*/
  145. %token
  146. PERCENT_CODE "%code"
  147. PERCENT_DEFAULT_PREC "%default-prec"
  148. PERCENT_DEFINE "%define"
  149. PERCENT_DEFINES "%defines"
  150. PERCENT_ERROR_VERBOSE "%error-verbose"
  151. PERCENT_EXPECT "%expect"
  152. PERCENT_EXPECT_RR "%expect-rr"
  153. PERCENT_FLAG "%<flag>"
  154. PERCENT_FILE_PREFIX "%file-prefix"
  155. PERCENT_GLR_PARSER "%glr-parser"
  156. PERCENT_INITIAL_ACTION "%initial-action"
  157. PERCENT_LANGUAGE "%language"
  158. PERCENT_NAME_PREFIX "%name-prefix"
  159. PERCENT_NO_DEFAULT_PREC "%no-default-prec"
  160. PERCENT_NO_LINES "%no-lines"
  161. PERCENT_NONDETERMINISTIC_PARSER
  162. "%nondeterministic-parser"
  163. PERCENT_OUTPUT "%output"
  164. PERCENT_PURE_PARSER "%pure-parser"
  165. PERCENT_REQUIRE "%require"
  166. PERCENT_SKELETON "%skeleton"
  167. PERCENT_START "%start"
  168. PERCENT_TOKEN_TABLE "%token-table"
  169. PERCENT_VERBOSE "%verbose"
  170. PERCENT_YACC "%yacc"
  171. ;
  172. %token BRACED_CODE "{...}"
  173. %token BRACED_PREDICATE "%?{...}"
  174. %token BRACKETED_ID "[identifier]"
  175. %token CHAR "char"
  176. %token COLON ":"
  177. %token EPILOGUE "epilogue"
  178. %token EQUAL "="
  179. %token ID "identifier"
  180. %token ID_COLON "identifier:"
  181. %token PERCENT_PERCENT "%%"
  182. %token PIPE "|"
  183. %token PROLOGUE "%{...%}"
  184. %token SEMICOLON ";"
  185. %token TAG "<tag>"
  186. %token TAG_ANY "<*>"
  187. %token TAG_NONE "<>"
  188. /* Experimental feature, don't rely on it. */
  189. %code pre-printer {tron (yyo);}
  190. %code post-printer {troff (yyo);}
  191. %type <unsigned char> CHAR
  192. %type <char*> "{...}" "%?{...}" "%{...%}" EPILOGUE STRING
  193. %type <uniqstr>
  194. BRACKETED_ID ID ID_COLON
  195. PERCENT_ERROR_VERBOSE PERCENT_FILE_PREFIX PERCENT_FLAG PERCENT_NAME_PREFIX
  196. PERCENT_PURE_PARSER PERCENT_YACC
  197. TAG tag tag.opt variable
  198. %token <int> INT "integer"
  199. %type <symbol*> id id_colon string_as_id symbol token_decl token_decl_for_prec
  200. %type <assoc> precedence_declarator
  201. %type <named_ref*> named_ref.opt
  202. /*---------.
  203. | %param. |
  204. `---------*/
  205. %code requires
  206. {
  207. typedef enum
  208. {
  209. param_none = 0,
  210. param_lex = 1 << 0,
  211. param_parse = 1 << 1,
  212. param_both = param_lex | param_parse
  213. } param_type;
  214. };
  215. %code
  216. {
  217. /** Add a lex-param and/or a parse-param.
  218. *
  219. * \param type where to push this formal argument.
  220. * \param decl the formal argument. Destroyed.
  221. * \param loc the location in the source.
  222. */
  223. static void add_param (param_type type, char *decl, location loc);
  224. static param_type current_param = param_none;
  225. };
  226. %token <param_type> PERCENT_PARAM "%param";
  227. /*==========\
  228. | Grammar. |
  229. \==========*/
  230. %%
  231. input:
  232. prologue_declarations "%%" grammar epilogue.opt
  233. ;
  234. /*------------------------------------.
  235. | Declarations: before the first %%. |
  236. `------------------------------------*/
  237. prologue_declarations:
  238. %empty
  239. | prologue_declarations prologue_declaration
  240. ;
  241. prologue_declaration:
  242. grammar_declaration
  243. | "%{...%}"
  244. {
  245. muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
  246. translate_code ($1, @1, true), @1);
  247. code_scanner_last_string_free ();
  248. }
  249. | "%<flag>"
  250. {
  251. muscle_percent_define_ensure ($1, @1, true);
  252. }
  253. | "%define" variable value
  254. {
  255. muscle_percent_define_insert ($2, @$, $3.kind, $3.chars,
  256. MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
  257. }
  258. | "%defines" { defines_flag = true; }
  259. | "%defines" STRING
  260. {
  261. defines_flag = true;
  262. spec_header_file = xstrdup ($2);
  263. }
  264. | "%error-verbose" { handle_error_verbose (&@$, $1); }
  265. | "%expect" INT { expected_sr_conflicts = $2; }
  266. | "%expect-rr" INT { expected_rr_conflicts = $2; }
  267. | "%file-prefix" STRING { handle_file_prefix (&@$, &@1, $1, $2); }
  268. | "%glr-parser"
  269. {
  270. nondeterministic_parser = true;
  271. glr_parser = true;
  272. }
  273. | "%initial-action" "{...}"
  274. {
  275. muscle_code_grow ("initial_action", translate_code ($2, @2, false), @2);
  276. code_scanner_last_string_free ();
  277. }
  278. | "%language" STRING { language_argmatch ($2, grammar_prio, @1); }
  279. | "%name-prefix" STRING { handle_name_prefix (&@$, $1, $2); }
  280. | "%no-lines" { no_lines_flag = true; }
  281. | "%nondeterministic-parser" { nondeterministic_parser = true; }
  282. | "%output" STRING { spec_outfile = $2; }
  283. | "%param" { current_param = $1; } params { current_param = param_none; }
  284. | "%pure-parser" { handle_pure_parser (&@$, $1); }
  285. | "%require" STRING { handle_require (&@2, $2); }
  286. | "%skeleton" STRING { handle_skeleton (&@2, $2); }
  287. | "%token-table" { token_table_flag = true; }
  288. | "%verbose" { report_flag |= report_states; }
  289. | "%yacc" { handle_yacc (&@$, $1); }
  290. | error ";" { current_class = unknown_sym; yyerrok; }
  291. | /*FIXME: Err? What is this horror doing here? */ ";"
  292. ;
  293. params:
  294. params "{...}" { add_param (current_param, $2, @2); }
  295. | "{...}" { add_param (current_param, $1, @1); }
  296. ;
  297. /*----------------------.
  298. | grammar_declaration. |
  299. `----------------------*/
  300. grammar_declaration:
  301. symbol_declaration
  302. | "%start" symbol
  303. {
  304. grammar_start_symbol_set ($2, @2);
  305. }
  306. | code_props_type "{...}" generic_symlist
  307. {
  308. code_props code;
  309. code_props_symbol_action_init (&code, $2, @2);
  310. code_props_translate_code (&code);
  311. {
  312. for (symbol_list *list = $3; list; list = list->next)
  313. symbol_list_code_props_set (list, $1, &code);
  314. symbol_list_free ($3);
  315. }
  316. }
  317. | "%default-prec"
  318. {
  319. default_prec = true;
  320. }
  321. | "%no-default-prec"
  322. {
  323. default_prec = false;
  324. }
  325. | "%code" "{...}"
  326. {
  327. /* Do not invoke muscle_percent_code_grow here since it invokes
  328. muscle_user_name_list_grow. */
  329. muscle_code_grow ("percent_code()",
  330. translate_code_braceless ($2, @2), @2);
  331. code_scanner_last_string_free ();
  332. }
  333. | "%code" ID "{...}"
  334. {
  335. muscle_percent_code_grow ($2, @2, translate_code_braceless ($3, @3), @3);
  336. code_scanner_last_string_free ();
  337. }
  338. ;
  339. %type <code_props_type> code_props_type;
  340. code_props_type:
  341. "%destructor" { $$ = destructor; }
  342. | "%printer" { $$ = printer; }
  343. ;
  344. /*---------.
  345. | %union. |
  346. `---------*/
  347. %token PERCENT_UNION "%union";
  348. union_name:
  349. %empty {}
  350. | ID { muscle_percent_define_insert ("api.value.union.name",
  351. @1, muscle_keyword, $1,
  352. MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE); }
  353. ;
  354. grammar_declaration:
  355. "%union" union_name "{...}"
  356. {
  357. union_seen = true;
  358. muscle_code_grow ("union_members", translate_code_braceless ($3, @3), @3);
  359. code_scanner_last_string_free ();
  360. }
  361. ;
  362. %type <symbol_list*> nterm_decls symbol_decls symbol_decl.1
  363. token_decls token_decls_for_prec
  364. token_decl.1 token_decl_for_prec.1;
  365. symbol_declaration:
  366. "%nterm" { current_class = nterm_sym; } nterm_decls[syms]
  367. {
  368. current_class = unknown_sym;
  369. symbol_list_free ($syms);
  370. }
  371. | "%token" { current_class = token_sym; } token_decls[syms]
  372. {
  373. current_class = unknown_sym;
  374. symbol_list_free ($syms);
  375. }
  376. | "%type" symbol_decls[syms]
  377. {
  378. symbol_list_free ($syms);
  379. }
  380. | precedence_declarator token_decls_for_prec[syms]
  381. {
  382. ++current_prec;
  383. for (symbol_list *list = $syms; list; list = list->next)
  384. symbol_precedence_set (list->content.sym, current_prec, $1, @1);
  385. symbol_list_free ($syms);
  386. }
  387. ;
  388. precedence_declarator:
  389. "%left" { $$ = left_assoc; }
  390. | "%right" { $$ = right_assoc; }
  391. | "%nonassoc" { $$ = non_assoc; }
  392. | "%precedence" { $$ = precedence_assoc; }
  393. ;
  394. tag.opt:
  395. %empty { $$ = NULL; }
  396. | TAG { $$ = $1; }
  397. ;
  398. %type <symbol_list*> generic_symlist generic_symlist_item;
  399. generic_symlist:
  400. generic_symlist_item
  401. | generic_symlist generic_symlist_item { $$ = symbol_list_append ($1, $2); }
  402. ;
  403. generic_symlist_item:
  404. symbol { $$ = symbol_list_sym_new ($1, @1); }
  405. | tag { $$ = symbol_list_type_new ($1, @1); }
  406. ;
  407. tag:
  408. TAG
  409. | "<*>" { $$ = uniqstr_new ("*"); }
  410. | "<>" { $$ = uniqstr_new (""); }
  411. ;
  412. /*-----------------------.
  413. | nterm_decls (%nterm). |
  414. `-----------------------*/
  415. // A non empty list of possibly tagged symbols for %nterm.
  416. //
  417. // Can easily be defined like symbol_decls but restricted to ID, but
  418. // using token_decls allows to reudce the number of rules, and also to
  419. // make nicer error messages on "%nterm 'a'" or '%nterm FOO "foo"'.
  420. nterm_decls:
  421. token_decls
  422. ;
  423. /*-----------------------------------.
  424. | token_decls (%token, and %nterm). |
  425. `-----------------------------------*/
  426. // A non empty list of possibly tagged symbols for %token or %nterm.
  427. token_decls:
  428. token_decl.1[syms]
  429. {
  430. $$ = $syms;
  431. }
  432. | TAG token_decl.1[syms]
  433. {
  434. $$ = symbol_list_type_set ($syms, $TAG, @TAG);
  435. }
  436. | token_decls TAG token_decl.1[syms]
  437. {
  438. $$ = symbol_list_append ($1, symbol_list_type_set ($syms, $TAG, @TAG));
  439. }
  440. ;
  441. // One or more symbol declarations for %token or %nterm.
  442. token_decl.1:
  443. token_decl { $$ = symbol_list_sym_new ($1, @1); }
  444. | token_decl.1 token_decl { $$ = symbol_list_append ($1, symbol_list_sym_new ($2, @2)); }
  445. // One symbol declaration for %token or %nterm.
  446. token_decl:
  447. id int.opt[num] string_as_id.opt[alias]
  448. {
  449. $$ = $id;
  450. symbol_class_set ($id, current_class, @id, true);
  451. if (0 <= $num)
  452. symbol_user_token_number_set ($id, $num, @num);
  453. if ($alias)
  454. symbol_make_alias ($id, $alias, @alias);
  455. }
  456. ;
  457. %type <int> int.opt;
  458. int.opt:
  459. %empty { $$ = -1; }
  460. | INT
  461. ;
  462. /*-------------------------------------.
  463. | token_decls_for_prec (%left, etc.). |
  464. `-------------------------------------*/
  465. // A non empty list of possibly tagged tokens for precedence declaration.
  466. //
  467. // Similar to %token (token_decls), but in '%left FOO 1 "foo"', it treats
  468. // FOO and "foo" as two different symbols instead of aliasing them.
  469. token_decls_for_prec:
  470. token_decl_for_prec.1[syms]
  471. {
  472. $$ = $syms;
  473. }
  474. | TAG token_decl_for_prec.1[syms]
  475. {
  476. $$ = symbol_list_type_set ($syms, $TAG, @TAG);
  477. }
  478. | token_decls_for_prec TAG token_decl_for_prec.1[syms]
  479. {
  480. $$ = symbol_list_append ($1, symbol_list_type_set ($syms, $TAG, @TAG));
  481. }
  482. ;
  483. // One or more token declarations for precedence declaration.
  484. token_decl_for_prec.1:
  485. token_decl_for_prec
  486. { $$ = symbol_list_sym_new ($1, @1); }
  487. | token_decl_for_prec.1 token_decl_for_prec
  488. { $$ = symbol_list_append ($1, symbol_list_sym_new ($2, @2)); }
  489. // One token declaration for precedence declaration.
  490. token_decl_for_prec:
  491. id int.opt[num]
  492. {
  493. $$ = $id;
  494. symbol_class_set ($id, token_sym, @id, false);
  495. if (0 <= $num)
  496. symbol_user_token_number_set ($id, $num, @num);
  497. }
  498. | string_as_id
  499. ;
  500. /*-----------------------.
  501. | symbol_decls (%type). |
  502. `-----------------------*/
  503. // A non empty list of typed symbols.
  504. symbol_decls:
  505. symbol_decl.1[syms]
  506. {
  507. $$ = $syms;
  508. }
  509. | TAG symbol_decl.1[syms]
  510. {
  511. $$ = symbol_list_type_set ($syms, $TAG, @TAG);
  512. }
  513. | symbol_decls TAG symbol_decl.1[syms]
  514. {
  515. $$ = symbol_list_append ($1, symbol_list_type_set ($syms, $TAG, @TAG));
  516. }
  517. ;
  518. // One or more token declarations.
  519. symbol_decl.1:
  520. symbol { $$ = symbol_list_sym_new ($1, @1); }
  521. | symbol_decl.1 symbol { $$ = symbol_list_append ($1, symbol_list_sym_new ($2, @2)); }
  522. ;
  523. /*------------------------------------------.
  524. | The grammar section: between the two %%. |
  525. `------------------------------------------*/
  526. grammar:
  527. rules_or_grammar_declaration
  528. | grammar rules_or_grammar_declaration
  529. ;
  530. /* As a Bison extension, one can use the grammar declarations in the
  531. body of the grammar. */
  532. rules_or_grammar_declaration:
  533. rules
  534. | grammar_declaration ";"
  535. | error ";"
  536. {
  537. yyerrok;
  538. }
  539. ;
  540. rules:
  541. id_colon named_ref.opt { current_lhs ($1, @1, $2); } ":" rhses.1
  542. {
  543. /* Free the current lhs. */
  544. current_lhs (0, @1, 0);
  545. }
  546. ;
  547. rhses.1:
  548. rhs { grammar_current_rule_end (@rhs); }
  549. | rhses.1 "|" rhs { grammar_current_rule_end (@rhs); }
  550. | rhses.1 ";"
  551. ;
  552. %token PERCENT_EMPTY "%empty";
  553. rhs:
  554. %empty
  555. { grammar_current_rule_begin (current_lhs_symbol, current_lhs_loc,
  556. current_lhs_named_ref); }
  557. | rhs symbol named_ref.opt
  558. { grammar_current_rule_symbol_append ($2, @2, $3); }
  559. | rhs tag.opt "{...}"[act] named_ref.opt[name]
  560. { grammar_current_rule_action_append ($act, @act, $name, $[tag.opt]); }
  561. | rhs "%?{...}"
  562. { grammar_current_rule_predicate_append ($2, @2); }
  563. | rhs "%empty"
  564. { grammar_current_rule_empty_set (@2); }
  565. | rhs "%prec" symbol
  566. { grammar_current_rule_prec_set ($3, @3); }
  567. | rhs "%dprec" INT
  568. { grammar_current_rule_dprec_set ($3, @3); }
  569. | rhs "%merge" TAG
  570. { grammar_current_rule_merge_set ($3, @3); }
  571. | rhs "%expect" INT
  572. { grammar_current_rule_expect_sr ($3, @3); }
  573. | rhs "%expect-rr" INT
  574. { grammar_current_rule_expect_rr ($3, @3); }
  575. ;
  576. named_ref.opt:
  577. %empty { $$ = NULL; }
  578. | BRACKETED_ID { $$ = named_ref_new ($1, @1); }
  579. ;
  580. /*---------------------.
  581. | variable and value. |
  582. `---------------------*/
  583. variable:
  584. ID
  585. ;
  586. /* Some content or empty by default. */
  587. %code requires {
  588. #include "muscle-tab.h"
  589. typedef struct
  590. {
  591. char const *chars;
  592. muscle_kind kind;
  593. } value_type;
  594. };
  595. %type <value_type> value;
  596. value:
  597. %empty { $$.kind = muscle_keyword; $$.chars = ""; }
  598. | ID { $$.kind = muscle_keyword; $$.chars = $1; }
  599. | STRING { $$.kind = muscle_string; $$.chars = $1; }
  600. | "{...}" { $$.kind = muscle_code; $$.chars = strip_braces ($1); }
  601. ;
  602. /*--------------.
  603. | Identifiers. |
  604. `--------------*/
  605. /* Identifiers are returned as uniqstr values by the scanner.
  606. Depending on their use, we may need to make them genuine symbols. */
  607. id:
  608. ID
  609. { $$ = symbol_from_uniqstr ($1, @1); }
  610. | CHAR
  611. {
  612. if (current_class == nterm_sym)
  613. {
  614. gram_error (&@1,
  615. _("character literals cannot be nonterminals"));
  616. YYERROR;
  617. }
  618. $$ = symbol_get (char_name ($1), @1);
  619. symbol_class_set ($$, token_sym, @1, false);
  620. symbol_user_token_number_set ($$, $1, @1);
  621. }
  622. ;
  623. id_colon:
  624. ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
  625. ;
  626. symbol:
  627. id
  628. | string_as_id
  629. ;
  630. /* A string used as an ID: quote it. */
  631. string_as_id:
  632. STRING
  633. {
  634. $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
  635. symbol_class_set ($$, token_sym, @1, false);
  636. }
  637. ;
  638. %type <symbol*> string_as_id.opt;
  639. string_as_id.opt:
  640. %empty { $$ = NULL; }
  641. | string_as_id
  642. ;
  643. epilogue.opt:
  644. %empty
  645. | "%%" EPILOGUE
  646. {
  647. muscle_code_grow ("epilogue", translate_code ($2, @2, true), @2);
  648. code_scanner_last_string_free ();
  649. }
  650. ;
  651. %%
  652. /* Return the location of the left-hand side of a rule whose
  653. right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
  654. the right-hand side, and return an empty location equal to the end
  655. boundary of RHS[0] if the right-hand side is empty. */
  656. static YYLTYPE
  657. lloc_default (YYLTYPE const *rhs, int n)
  658. {
  659. YYLTYPE loc;
  660. /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
  661. The bug is fixed in 7.4.2m, but play it safe for now. */
  662. loc.start = rhs[n].end;
  663. loc.end = rhs[n].end;
  664. /* Ignore empty nonterminals the start of the right-hand side.
  665. Do not bother to ignore them at the end of the right-hand side,
  666. since empty nonterminals have the same end as their predecessors. */
  667. for (int i = 1; i <= n; i++)
  668. if (! equal_boundaries (rhs[i].start, rhs[i].end))
  669. {
  670. loc.start = rhs[i].start;
  671. break;
  672. }
  673. return loc;
  674. }
  675. static
  676. char *strip_braces (char *code)
  677. {
  678. code[strlen (code) - 1] = 0;
  679. return code + 1;
  680. }
  681. static
  682. char const *
  683. translate_code (char *code, location loc, bool plain)
  684. {
  685. code_props plain_code;
  686. if (plain)
  687. code_props_plain_init (&plain_code, code, loc);
  688. else
  689. code_props_symbol_action_init (&plain_code, code, loc);
  690. code_props_translate_code (&plain_code);
  691. gram_scanner_last_string_free ();
  692. return plain_code.code;
  693. }
  694. static
  695. char const *
  696. translate_code_braceless (char *code, location loc)
  697. {
  698. return translate_code (strip_braces (code), loc, true);
  699. }
  700. static void
  701. add_param (param_type type, char *decl, location loc)
  702. {
  703. static char const alphanum[26 + 26 + 1 + 10 + 1] =
  704. "abcdefghijklmnopqrstuvwxyz"
  705. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  706. "_"
  707. "0123456789";
  708. char const *name_start = NULL;
  709. {
  710. char *p;
  711. /* Stop on last actual character. */
  712. for (p = decl; p[1]; p++)
  713. if ((p == decl
  714. || ! memchr (alphanum, p[-1], sizeof alphanum - 1))
  715. && memchr (alphanum, p[0], sizeof alphanum - 10 - 1))
  716. name_start = p;
  717. /* Strip the surrounding '{' and '}', and any blanks just inside
  718. the braces. */
  719. --p;
  720. while (c_isspace ((unsigned char) *p))
  721. --p;
  722. p[1] = '\0';
  723. ++decl;
  724. while (c_isspace ((unsigned char) *decl))
  725. ++decl;
  726. }
  727. if (! name_start)
  728. complain (&loc, complaint, _("missing identifier in parameter declaration"));
  729. else
  730. {
  731. char *name = xmemdup0 (name_start, strspn (name_start, alphanum));
  732. if (type & param_lex)
  733. muscle_pair_list_grow ("lex_param", decl, name);
  734. if (type & param_parse)
  735. muscle_pair_list_grow ("parse_param", decl, name);
  736. free (name);
  737. }
  738. gram_scanner_last_string_free ();
  739. }
  740. static void
  741. handle_error_verbose (location const *loc, char const *directive)
  742. {
  743. bison_directive (loc, directive);
  744. muscle_percent_define_insert (directive, *loc, muscle_keyword, "",
  745. MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
  746. }
  747. static void
  748. handle_file_prefix (location const *loc,
  749. location const *dir_loc,
  750. char const *directive, char const *value)
  751. {
  752. bison_directive (loc, directive);
  753. bool warned = false;
  754. if (location_empty (spec_file_prefix_loc))
  755. {
  756. spec_file_prefix_loc = *loc;
  757. spec_file_prefix = value;
  758. }
  759. else
  760. {
  761. duplicate_directive (directive, spec_file_prefix_loc, *loc);
  762. warned = true;
  763. }
  764. if (!warned
  765. && STRNEQ (directive, "%file-prefix"))
  766. deprecated_directive (dir_loc, directive, "%file-prefix");
  767. }
  768. static void
  769. handle_name_prefix (location const *loc,
  770. char const *directive, char const *value)
  771. {
  772. bison_directive (loc, directive);
  773. char buf1[1024];
  774. size_t len1 = sizeof (buf1);
  775. char *old = asnprintf (buf1, &len1, "%s\"%s\"", directive, value);
  776. if (!old)
  777. xalloc_die ();
  778. if (location_empty (spec_name_prefix_loc))
  779. {
  780. spec_name_prefix = value;
  781. spec_name_prefix_loc = *loc;
  782. char buf2[1024];
  783. size_t len2 = sizeof (buf2);
  784. char *new = asnprintf (buf2, &len2, "%%define api.prefix {%s}", value);
  785. if (!new)
  786. xalloc_die ();
  787. deprecated_directive (loc, old, new);
  788. if (new != buf2)
  789. free (new);
  790. }
  791. else
  792. duplicate_directive (old, spec_file_prefix_loc, *loc);
  793. if (old != buf1)
  794. free (old);
  795. }
  796. static void
  797. handle_pure_parser (location const *loc, char const *directive)
  798. {
  799. bison_directive (loc, directive);
  800. deprecated_directive (loc, directive, "%define api.pure");
  801. muscle_percent_define_insert ("api.pure", *loc, muscle_keyword, "",
  802. MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
  803. }
  804. static void
  805. handle_require (location const *loc, char const *version)
  806. {
  807. /* Changes of behavior are only on minor version changes, so "3.0.5"
  808. is the same as "3.0". */
  809. errno = 0;
  810. char* cp = NULL;
  811. unsigned long major = strtoul (version, &cp, 10);
  812. if (errno || *cp != '.')
  813. {
  814. complain (loc, complaint, _("invalid version requirement: %s"),
  815. version);
  816. return;
  817. }
  818. ++cp;
  819. unsigned long minor = strtoul (cp, NULL, 10);
  820. if (errno)
  821. {
  822. complain (loc, complaint, _("invalid version requirement: %s"),
  823. version);
  824. return;
  825. }
  826. required_version = major * 100 + minor;
  827. /* Pretend to be at least 3.4, to check features published in 3.4
  828. while developping it. */
  829. const char* api_version = "3.4";
  830. const char* package_version =
  831. strverscmp (api_version, PACKAGE_VERSION) > 0
  832. ? api_version : PACKAGE_VERSION;
  833. if (strverscmp (version, package_version) > 0)
  834. {
  835. complain (loc, complaint, _("require bison %s, but have %s"),
  836. version, package_version);
  837. exit (EX_MISMATCH);
  838. }
  839. }
  840. static void
  841. handle_skeleton (location const *loc, char const *skel)
  842. {
  843. char const *skeleton_user = skel;
  844. if (strchr (skeleton_user, '/'))
  845. {
  846. size_t dir_length = strlen (current_file);
  847. while (dir_length && current_file[dir_length - 1] != '/')
  848. --dir_length;
  849. while (dir_length && current_file[dir_length - 1] == '/')
  850. --dir_length;
  851. char *skeleton_build =
  852. xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
  853. if (dir_length > 0)
  854. {
  855. memcpy (skeleton_build, current_file, dir_length);
  856. skeleton_build[dir_length++] = '/';
  857. }
  858. strcpy (skeleton_build + dir_length, skeleton_user);
  859. skeleton_user = uniqstr_new (skeleton_build);
  860. free (skeleton_build);
  861. }
  862. skeleton_arg (skeleton_user, grammar_prio, *loc);
  863. }
  864. static void
  865. handle_yacc (location const *loc, char const *directive)
  866. {
  867. bison_directive (loc, directive);
  868. bool warned = false;
  869. if (location_empty (yacc_loc))
  870. yacc_loc = *loc;
  871. else
  872. {
  873. duplicate_directive (directive, yacc_loc, *loc);
  874. warned = true;
  875. }
  876. if (!warned
  877. && STRNEQ (directive, "%fixed-output-files")
  878. && STRNEQ (directive, "%yacc"))
  879. deprecated_directive (loc, directive, "%fixed-output-files");
  880. }
  881. static void
  882. gram_error (location const *loc, char const *msg)
  883. {
  884. complain (loc, complaint, "%s", msg);
  885. }
  886. static char const *
  887. char_name (char c)
  888. {
  889. if (c == '\'')
  890. return "'\\''";
  891. else
  892. {
  893. char buf[4];
  894. buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
  895. return quotearg_style (escape_quoting_style, buf);
  896. }
  897. }
  898. static
  899. void
  900. current_lhs (symbol *sym, location loc, named_ref *ref)
  901. {
  902. current_lhs_symbol = sym;
  903. current_lhs_loc = loc;
  904. if (sym)
  905. symbol_location_as_lhs_set (sym, loc);
  906. /* In order to simplify memory management, named references for lhs
  907. are always assigned by deep copy into the current symbol_list
  908. node. This is because a single named-ref in the grammar may
  909. result in several uses when the user factors lhs between several
  910. rules using "|". Therefore free the parser's original copy. */
  911. free (current_lhs_named_ref);
  912. current_lhs_named_ref = ref;
  913. }
  914. static void tron (FILE *yyo)
  915. {
  916. begin_use_class ("value", yyo);
  917. }
  918. static void troff (FILE *yyo)
  919. {
  920. end_use_class ("value", yyo);
  921. }