gnulib: update
[bison.git] / data / README.md
blob0b9c6b569293346673df5facdb95c8895abb7597
1 This directory contains data needed by Bison.
3 # Directory Content
4 ## Skeletons
5 Bison skeletons: the general shapes of the different parser kinds, that are
6 specialized for specific grammars by the bison program.
8 Currently, the supported skeletons are:
10 - yacc.c
11   It used to be named bison.simple: it corresponds to C Yacc
12   compatible LALR(1) parsers.
14 - lalr1.cc
15   Produces a C++ parser class.
17 - lalr1.java
18   Produces a Java parser class.
20 - glr.c
21   A Generalized LR C parser based on Bison's LALR(1) tables.
23 - glr.cc
24   A Generalized LR C++ parser.  Actually a C++ wrapper around glr.c.
26 These skeletons are the only ones supported by the Bison team.  Because the
27 interface between skeletons and the bison program is not finished, *we are
28 not bound to it*.  In particular, Bison is not mature enough for us to
29 consider that "foreign skeletons" are supported.
31 ## m4sugar
32 This directory contains M4sugar, sort of an extended library for M4, which
33 is used by Bison to instantiate the skeletons.
35 ## xslt
36 This directory contains XSLT programs that transform Bison's XML output into
37 various formats.
39 - bison.xsl
40   A library of routines used by the other XSLT programs.
42 - xml2dot.xsl
43   Conversion into GraphViz's dot format.
45 - xml2text.xsl
46   Conversion into text.
48 - xml2xhtml.xsl
49   Conversion into XHTML.
51 # Implementation Notes About the Skeletons
53 "Skeleton" in Bison parlance means "backend": a skeleton is fed by the bison
54 executable with LR tables, facts about the symbols, etc. and they generate
55 the output (say parser.cc, parser.hh, location.hh, etc.).  They are only in
56 charge of generating the parser and its auxiliary files, they do not
57 generate the XML output, the parser.output reports, nor the graphical
58 rendering.
60 The bits of information passing from bison to the backend is named
61 "muscles".  Muscles are passed to M4 via its standard input: it's a set of
62 m4 definitions.  To see them, use `--trace=muscles`.
64 Except for muscles, whose names are generated by bison, the skeletons have
65 no constraint at all on the macro names: there is no technical/theoretical
66 limitation, as long as you generate the output, you can do what you want.
67 However, of course, that would be a bad idea if, say, the C and C++
68 skeletons used different approaches and had completely different
69 implementations.  That would be a maintenance nightmare.
71 Below, we document some of the macros that we use in several of the
72 skeletons.  If you are to write a new skeleton, please, implement them for
73 your language.  Overall, be sure to follow the same patterns as the existing
74 skeletons.
76 ## Symbols
78 ### `b4_symbol(NUM, FIELD)`
79 In order to unify the handling of the various aspects of symbols (tag, type
80 name, whether terminal, etc.), bison.exe defines one macro per (token,
81 field), where field can `has_id`, `id`, etc.: see
82 `prepare_symbol_definitions()` in `src/output.c`.
84 NUM can be:
85 - `empty` to denote the "empty" pseudo-symbol when it exists,
86 - `eof`, `error`, or `undef`
87 - a symbol number.
89 FIELD can be:
91 - `has_id`: 0 or 1
92   Whether the symbol has an `id`.
94 - `id`: string (e.g., `exp`, `NUM`, or `TOK_NUM` with api.token.prefix)
95   If `has_id`, the name of the token kind (prefixed by api.token.prefix if
96   defined), otherwise empty.  Guaranteed to be usable as a C identifier.
97   This is used to define the token kind (i.e., the enum used by the return
98   value of yylex).  Should be named `token_kind`.
100 - `tag`: string
101   A human readable representation of the symbol.  Can be `'foo'`,
102   `'foo.id'`, `'"foo"'` etc.
104 - `code`: integer
105   The token code associated to the token kind `id`.
106   The external number as used by yylex.  Can be ASCII code when a character,
107   some number chosen by bison, or some user number in the case of `%token
108   FOO <NUM>`.  Corresponds to `yychar` in `yacc.c`.
110 - `is_token`: 0 or 1
111   Whether this is a terminal symbol.
113 - `kind_base`: string (e.g., `YYSYMBOL_exp`, `YYSYMBOL_NUM`)
114   The base of the symbol kind, i.e., the enumerator of this symbol (token or
115   nonterminal) which is mapped to its `number`.
117 - `kind`: string
118   Same as `kind_base`, but possibly with a prefix in some languages.  E.g.,
119   EOF's `kind_base` and `kind` are `YYSYMBOL_YYEOF` in C, but are
120   `S_YYEMPTY` and `symbol_kind::S_YYEMPTY` in C++.
122 - `number`: integer
123   The code associated to the `kind`.
124   The internal number (computed from the external number by yytranslate).
125   Corresponds to yytoken in yacc.c.  This is the same number that serves as
126   key in b4_symbol(NUM, FIELD).
128   In bison, symbols are first assigned increasing numbers in order of
129   appearance (but tokens first, then nterms).  After grammar reduction,
130   unused nterms are then renumbered to appear last (i.e., first tokens, then
131   used nterms and finally unused nterms).  This final number NUM is the one
132   contained in this field, and it is the one used as key in `b4_symbol(NUM,
133   FIELD)`.
135   The code of the rule actions, however, is emitted before we know what
136   symbols are unused, so they use the original numbers.  To avoid confusion,
137   they actually use "orig NUM" instead of just "NUM".  bison also emits
138   definitions for `b4_symbol(orig NUM, number)` that map from original
139   numbers to the new ones.  `b4_symbol` actually resolves `orig NUM` in the
140   other case, i.e., `b4_symbol(orig 42, tag)` would return the tag of the
141   symbols whose original number was 42.
143 - `has_type`: 0, 1
144   Whether has a semantic value.
146 - `type_tag`: string
147   When api.value.type=union, the generated name for the union member.
148   yytype_INT etc. for symbols that has_id, otherwise yytype_1 etc.
150 - `type`: string
151   If it has a semantic value, its type tag, or, if variant are used,
152   its type.
153   In the case of api.value.type=union, type is the real type (e.g. int).
155 - `slot`: string
156   If it has a semantic value, the name of the union member (i.e., bounces to
157   either `type_tag` or `type`).  It would be better to fix our mess and
158   always use `type` for the true type of the member, and `type_tag` for the
159   name of the union member.
161 - `has_printer`: 0, 1
162 - `printer`: string
163 - `printer_file`: string
164 - `printer_line`: integer
165 - `printer_loc`: location
166   If the symbol has a printer, everything about it.
168 - `has_destructor`, `destructor`, `destructor_file`, `destructor_line`, `destructor_loc`
169   Likewise.
171 ### `b4_symbol_value(VAL, [SYMBOL-NUM], [TYPE-TAG])`
172 Expansion of $$, $1, $<TYPE-TAG>3, etc.
174 The semantic value from a given VAL.
175 - `VAL`: some semantic value storage (typically a union).  e.g., `yylval`
176 - `SYMBOL-NUM`: the symbol number from which we extract the type tag.
177 - `TYPE-TAG`, the user forced the `<TYPE-TAG>`.
179 The result can be used safely, it is put in parens to avoid nasty precedence
180 issues.
182 ### `b4_lhs_value(SYMBOL-NUM, [TYPE])`
183 Expansion of `$$` or `$<TYPE>$`, for symbol `SYMBOL-NUM`.
185 ### `b4_rhs_data(RULE-LENGTH, POS)`
186 The data corresponding to the symbol `#POS`, where the current rule has
187 `RULE-LENGTH` symbols on RHS.
189 ### `b4_rhs_value(RULE-LENGTH, POS, SYMBOL-NUM, [TYPE])`
190 Expansion of `$<TYPE>POS`, where the current rule has `RULE-LENGTH` symbols
191 on RHS.
193 <!--
195 Local Variables:
196 mode: markdown
197 fill-column: 76
198 ispell-dictionary: "american"
199 End:
201 Copyright (C) 2002, 2008-2015, 2018-2021 Free Software Foundation, Inc.
203 This file is part of GNU Bison.
205 This program is free software: you can redistribute it and/or modify
206 it under the terms of the GNU General Public License as published by
207 the Free Software Foundation, either version 3 of the License, or
208 (at your option) any later version.
210 This program is distributed in the hope that it will be useful,
211 but WITHOUT ANY WARRANTY; without even the implied warranty of
212 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
213 GNU General Public License for more details.
215 You should have received a copy of the GNU General Public License
216 along with this program.  If not, see <https://www.gnu.org/licenses/>.