xref: /aosp_15_r20/prebuilts/build-tools/common/bison/README.md (revision cda5da8d549138a6648c5ee6d7a49cf8f4a657be)
1*cda5da8dSAndroid Build Coastguard WorkerThis directory contains data needed by Bison.
2*cda5da8dSAndroid Build Coastguard Worker
3*cda5da8dSAndroid Build Coastguard Worker# Directory Content
4*cda5da8dSAndroid Build Coastguard Worker## Skeletons
5*cda5da8dSAndroid Build Coastguard WorkerBison skeletons: the general shapes of the different parser kinds, that are
6*cda5da8dSAndroid Build Coastguard Workerspecialized for specific grammars by the bison program.
7*cda5da8dSAndroid Build Coastguard Worker
8*cda5da8dSAndroid Build Coastguard WorkerCurrently, the supported skeletons are:
9*cda5da8dSAndroid Build Coastguard Worker
10*cda5da8dSAndroid Build Coastguard Worker- yacc.c
11*cda5da8dSAndroid Build Coastguard Worker  It used to be named bison.simple: it corresponds to C Yacc
12*cda5da8dSAndroid Build Coastguard Worker  compatible LALR(1) parsers.
13*cda5da8dSAndroid Build Coastguard Worker
14*cda5da8dSAndroid Build Coastguard Worker- lalr1.cc
15*cda5da8dSAndroid Build Coastguard Worker  Produces a C++ parser class.
16*cda5da8dSAndroid Build Coastguard Worker
17*cda5da8dSAndroid Build Coastguard Worker- lalr1.java
18*cda5da8dSAndroid Build Coastguard Worker  Produces a Java parser class.
19*cda5da8dSAndroid Build Coastguard Worker
20*cda5da8dSAndroid Build Coastguard Worker- glr.c
21*cda5da8dSAndroid Build Coastguard Worker  A Generalized LR C parser based on Bison's LALR(1) tables.
22*cda5da8dSAndroid Build Coastguard Worker
23*cda5da8dSAndroid Build Coastguard Worker- glr.cc
24*cda5da8dSAndroid Build Coastguard Worker  A Generalized LR C++ parser.  Actually a C++ wrapper around glr.c.
25*cda5da8dSAndroid Build Coastguard Worker
26*cda5da8dSAndroid Build Coastguard WorkerThese skeletons are the only ones supported by the Bison team.  Because the
27*cda5da8dSAndroid Build Coastguard Workerinterface between skeletons and the bison program is not finished, *we are
28*cda5da8dSAndroid Build Coastguard Workernot bound to it*.  In particular, Bison is not mature enough for us to
29*cda5da8dSAndroid Build Coastguard Workerconsider that "foreign skeletons" are supported.
30*cda5da8dSAndroid Build Coastguard Worker
31*cda5da8dSAndroid Build Coastguard Worker## m4sugar
32*cda5da8dSAndroid Build Coastguard WorkerThis directory contains M4sugar, sort of an extended library for M4, which
33*cda5da8dSAndroid Build Coastguard Workeris used by Bison to instantiate the skeletons.
34*cda5da8dSAndroid Build Coastguard Worker
35*cda5da8dSAndroid Build Coastguard Worker## xslt
36*cda5da8dSAndroid Build Coastguard WorkerThis directory contains XSLT programs that transform Bison's XML output into
37*cda5da8dSAndroid Build Coastguard Workervarious formats.
38*cda5da8dSAndroid Build Coastguard Worker
39*cda5da8dSAndroid Build Coastguard Worker- bison.xsl
40*cda5da8dSAndroid Build Coastguard Worker  A library of routines used by the other XSLT programs.
41*cda5da8dSAndroid Build Coastguard Worker
42*cda5da8dSAndroid Build Coastguard Worker- xml2dot.xsl
43*cda5da8dSAndroid Build Coastguard Worker  Conversion into GraphViz's dot format.
44*cda5da8dSAndroid Build Coastguard Worker
45*cda5da8dSAndroid Build Coastguard Worker- xml2text.xsl
46*cda5da8dSAndroid Build Coastguard Worker  Conversion into text.
47*cda5da8dSAndroid Build Coastguard Worker
48*cda5da8dSAndroid Build Coastguard Worker- xml2xhtml.xsl
49*cda5da8dSAndroid Build Coastguard Worker  Conversion into XHTML.
50*cda5da8dSAndroid Build Coastguard Worker
51*cda5da8dSAndroid Build Coastguard Worker# Implementation Notes About the Skeletons
52*cda5da8dSAndroid Build Coastguard Worker
53*cda5da8dSAndroid Build Coastguard Worker"Skeleton" in Bison parlance means "backend": a skeleton is fed by the bison
54*cda5da8dSAndroid Build Coastguard Workerexecutable with LR tables, facts about the symbols, etc. and they generate
55*cda5da8dSAndroid Build Coastguard Workerthe output (say parser.cc, parser.hh, location.hh, etc.).  They are only in
56*cda5da8dSAndroid Build Coastguard Workercharge of generating the parser and its auxiliary files, they do not
57*cda5da8dSAndroid Build Coastguard Workergenerate the XML output, the parser.output reports, nor the graphical
58*cda5da8dSAndroid Build Coastguard Workerrendering.
59*cda5da8dSAndroid Build Coastguard Worker
60*cda5da8dSAndroid Build Coastguard WorkerThe bits of information passing from bison to the backend is named
61*cda5da8dSAndroid Build Coastguard Worker"muscles".  Muscles are passed to M4 via its standard input: it's a set of
62*cda5da8dSAndroid Build Coastguard Workerm4 definitions.  To see them, use `--trace=muscles`.
63*cda5da8dSAndroid Build Coastguard Worker
64*cda5da8dSAndroid Build Coastguard WorkerExcept for muscles, whose names are generated by bison, the skeletons have
65*cda5da8dSAndroid Build Coastguard Workerno constraint at all on the macro names: there is no technical/theoretical
66*cda5da8dSAndroid Build Coastguard Workerlimitation, as long as you generate the output, you can do what you want.
67*cda5da8dSAndroid Build Coastguard WorkerHowever, of course, that would be a bad idea if, say, the C and C++
68*cda5da8dSAndroid Build Coastguard Workerskeletons used different approaches and had completely different
69*cda5da8dSAndroid Build Coastguard Workerimplementations.  That would be a maintenance nightmare.
70*cda5da8dSAndroid Build Coastguard Worker
71*cda5da8dSAndroid Build Coastguard WorkerBelow, we document some of the macros that we use in several of the
72*cda5da8dSAndroid Build Coastguard Workerskeletons.  If you are to write a new skeleton, please, implement them for
73*cda5da8dSAndroid Build Coastguard Workeryour language.  Overall, be sure to follow the same patterns as the existing
74*cda5da8dSAndroid Build Coastguard Workerskeletons.
75*cda5da8dSAndroid Build Coastguard Worker
76*cda5da8dSAndroid Build Coastguard Worker## Vocabulary
77*cda5da8dSAndroid Build Coastguard Worker
78*cda5da8dSAndroid Build Coastguard WorkerWe use "formal arguments", or "formals" for short, to denote the declared
79*cda5da8dSAndroid Build Coastguard Workerparameters of a function (e.g., `int argc, const char **argv`).  Yes, this
80*cda5da8dSAndroid Build Coastguard Workeris somewhat contradictory with `param` in the `%param` directives.
81*cda5da8dSAndroid Build Coastguard Worker
82*cda5da8dSAndroid Build Coastguard WorkerWe use "effective arguments", or "args" for short, to denote the values
83*cda5da8dSAndroid Build Coastguard Workerpassed in function calls (e.g., `argc, argv`).
84*cda5da8dSAndroid Build Coastguard Worker
85*cda5da8dSAndroid Build Coastguard Worker## Symbols
86*cda5da8dSAndroid Build Coastguard Worker
87*cda5da8dSAndroid Build Coastguard Worker### `b4_symbol(NUM, FIELD)`
88*cda5da8dSAndroid Build Coastguard WorkerIn order to unify the handling of the various aspects of symbols (tag, type
89*cda5da8dSAndroid Build Coastguard Workername, whether terminal, etc.), bison.exe defines one macro per (token,
90*cda5da8dSAndroid Build Coastguard Workerfield), where field can `has_id`, `id`, etc.: see
91*cda5da8dSAndroid Build Coastguard Worker`prepare_symbol_definitions()` in `src/output.c`.
92*cda5da8dSAndroid Build Coastguard Worker
93*cda5da8dSAndroid Build Coastguard WorkerNUM can be:
94*cda5da8dSAndroid Build Coastguard Worker- `empty` to denote the "empty" pseudo-symbol when it exists,
95*cda5da8dSAndroid Build Coastguard Worker- `eof`, `error`, or `undef`
96*cda5da8dSAndroid Build Coastguard Worker- a symbol number.
97*cda5da8dSAndroid Build Coastguard Worker
98*cda5da8dSAndroid Build Coastguard WorkerFIELD can be:
99*cda5da8dSAndroid Build Coastguard Worker
100*cda5da8dSAndroid Build Coastguard Worker- `has_id`: 0 or 1
101*cda5da8dSAndroid Build Coastguard Worker  Whether the symbol has an `id`.
102*cda5da8dSAndroid Build Coastguard Worker
103*cda5da8dSAndroid Build Coastguard Worker- `id`: string (e.g., `exp`, `NUM`, or `TOK_NUM` with api.token.prefix)
104*cda5da8dSAndroid Build Coastguard Worker  If `has_id`, the name of the token kind (prefixed by api.token.prefix if
105*cda5da8dSAndroid Build Coastguard Worker  defined), otherwise empty.  Guaranteed to be usable as a C identifier.
106*cda5da8dSAndroid Build Coastguard Worker  This is used to define the token kind (i.e., the enum used by the return
107*cda5da8dSAndroid Build Coastguard Worker  value of yylex).  Should be named `token_kind`.
108*cda5da8dSAndroid Build Coastguard Worker
109*cda5da8dSAndroid Build Coastguard Worker- `tag`: string
110*cda5da8dSAndroid Build Coastguard Worker  A human readable representation of the symbol.  Can be `'foo'`,
111*cda5da8dSAndroid Build Coastguard Worker  `'foo.id'`, `'"foo"'` etc.
112*cda5da8dSAndroid Build Coastguard Worker
113*cda5da8dSAndroid Build Coastguard Worker- `code`: integer
114*cda5da8dSAndroid Build Coastguard Worker  The token code associated to the token kind `id`.
115*cda5da8dSAndroid Build Coastguard Worker  The external number as used by yylex.  Can be ASCII code when a character,
116*cda5da8dSAndroid Build Coastguard Worker  some number chosen by bison, or some user number in the case of `%token
117*cda5da8dSAndroid Build Coastguard Worker  FOO <NUM>`.  Corresponds to `yychar` in `yacc.c`.
118*cda5da8dSAndroid Build Coastguard Worker
119*cda5da8dSAndroid Build Coastguard Worker- `is_token`: 0 or 1
120*cda5da8dSAndroid Build Coastguard Worker  Whether this is a terminal symbol.
121*cda5da8dSAndroid Build Coastguard Worker
122*cda5da8dSAndroid Build Coastguard Worker- `kind_base`: string (e.g., `YYSYMBOL_exp`, `YYSYMBOL_NUM`)
123*cda5da8dSAndroid Build Coastguard Worker  The base of the symbol kind, i.e., the enumerator of this symbol (token or
124*cda5da8dSAndroid Build Coastguard Worker  nonterminal) which is mapped to its `number`.
125*cda5da8dSAndroid Build Coastguard Worker
126*cda5da8dSAndroid Build Coastguard Worker- `kind`: string
127*cda5da8dSAndroid Build Coastguard Worker  Same as `kind_base`, but possibly with a prefix in some languages.  E.g.,
128*cda5da8dSAndroid Build Coastguard Worker  EOF's `kind_base` and `kind` are `YYSYMBOL_YYEOF` in C, but are
129*cda5da8dSAndroid Build Coastguard Worker  `S_YYEMPTY` and `symbol_kind::S_YYEMPTY` in C++.
130*cda5da8dSAndroid Build Coastguard Worker
131*cda5da8dSAndroid Build Coastguard Worker- `number`: integer
132*cda5da8dSAndroid Build Coastguard Worker  The code associated to the `kind`.
133*cda5da8dSAndroid Build Coastguard Worker  The internal number (computed from the external number by yytranslate).
134*cda5da8dSAndroid Build Coastguard Worker  Corresponds to yytoken in yacc.c.  This is the same number that serves as
135*cda5da8dSAndroid Build Coastguard Worker  key in b4_symbol(NUM, FIELD).
136*cda5da8dSAndroid Build Coastguard Worker
137*cda5da8dSAndroid Build Coastguard Worker  In bison, symbols are first assigned increasing numbers in order of
138*cda5da8dSAndroid Build Coastguard Worker  appearance (but tokens first, then nterms).  After grammar reduction,
139*cda5da8dSAndroid Build Coastguard Worker  unused nterms are then renumbered to appear last (i.e., first tokens, then
140*cda5da8dSAndroid Build Coastguard Worker  used nterms and finally unused nterms).  This final number NUM is the one
141*cda5da8dSAndroid Build Coastguard Worker  contained in this field, and it is the one used as key in `b4_symbol(NUM,
142*cda5da8dSAndroid Build Coastguard Worker  FIELD)`.
143*cda5da8dSAndroid Build Coastguard Worker
144*cda5da8dSAndroid Build Coastguard Worker  The code of the rule actions, however, is emitted before we know what
145*cda5da8dSAndroid Build Coastguard Worker  symbols are unused, so they use the original numbers.  To avoid confusion,
146*cda5da8dSAndroid Build Coastguard Worker  they actually use "orig NUM" instead of just "NUM".  bison also emits
147*cda5da8dSAndroid Build Coastguard Worker  definitions for `b4_symbol(orig NUM, number)` that map from original
148*cda5da8dSAndroid Build Coastguard Worker  numbers to the new ones.  `b4_symbol` actually resolves `orig NUM` in the
149*cda5da8dSAndroid Build Coastguard Worker  other case, i.e., `b4_symbol(orig 42, tag)` would return the tag of the
150*cda5da8dSAndroid Build Coastguard Worker  symbols whose original number was 42.
151*cda5da8dSAndroid Build Coastguard Worker
152*cda5da8dSAndroid Build Coastguard Worker- `has_type`: 0, 1
153*cda5da8dSAndroid Build Coastguard Worker  Whether has a semantic value.
154*cda5da8dSAndroid Build Coastguard Worker
155*cda5da8dSAndroid Build Coastguard Worker- `type_tag`: string
156*cda5da8dSAndroid Build Coastguard Worker  When api.value.type=union, the generated name for the union member.
157*cda5da8dSAndroid Build Coastguard Worker  yytype_INT etc. for symbols that has_id, otherwise yytype_1 etc.
158*cda5da8dSAndroid Build Coastguard Worker
159*cda5da8dSAndroid Build Coastguard Worker- `type`: string
160*cda5da8dSAndroid Build Coastguard Worker  If it has a semantic value, its type tag, or, if variant are used,
161*cda5da8dSAndroid Build Coastguard Worker  its type.
162*cda5da8dSAndroid Build Coastguard Worker  In the case of api.value.type=union, type is the real type (e.g. int).
163*cda5da8dSAndroid Build Coastguard Worker
164*cda5da8dSAndroid Build Coastguard Worker- `slot`: string
165*cda5da8dSAndroid Build Coastguard Worker  If it has a semantic value, the name of the union member (i.e., bounces to
166*cda5da8dSAndroid Build Coastguard Worker  either `type_tag` or `type`).  It would be better to fix our mess and
167*cda5da8dSAndroid Build Coastguard Worker  always use `type` for the true type of the member, and `type_tag` for the
168*cda5da8dSAndroid Build Coastguard Worker  name of the union member.
169*cda5da8dSAndroid Build Coastguard Worker
170*cda5da8dSAndroid Build Coastguard Worker- `has_printer`: 0, 1
171*cda5da8dSAndroid Build Coastguard Worker- `printer`: string
172*cda5da8dSAndroid Build Coastguard Worker- `printer_file`: string
173*cda5da8dSAndroid Build Coastguard Worker- `printer_line`: integer
174*cda5da8dSAndroid Build Coastguard Worker- `printer_loc`: location
175*cda5da8dSAndroid Build Coastguard Worker  If the symbol has a printer, everything about it.
176*cda5da8dSAndroid Build Coastguard Worker
177*cda5da8dSAndroid Build Coastguard Worker- `has_destructor`, `destructor`, `destructor_file`, `destructor_line`, `destructor_loc`
178*cda5da8dSAndroid Build Coastguard Worker  Likewise.
179*cda5da8dSAndroid Build Coastguard Worker
180*cda5da8dSAndroid Build Coastguard Worker### `b4_symbol_value(VAL, [SYMBOL-NUM], [TYPE-TAG])`
181*cda5da8dSAndroid Build Coastguard WorkerExpansion of $$, $1, $<TYPE-TAG>3, etc.
182*cda5da8dSAndroid Build Coastguard Worker
183*cda5da8dSAndroid Build Coastguard WorkerThe semantic value from a given VAL.
184*cda5da8dSAndroid Build Coastguard Worker- `VAL`: some semantic value storage (typically a union).  e.g., `yylval`
185*cda5da8dSAndroid Build Coastguard Worker- `SYMBOL-NUM`: the symbol number from which we extract the type tag.
186*cda5da8dSAndroid Build Coastguard Worker- `TYPE-TAG`, the user forced the `<TYPE-TAG>`.
187*cda5da8dSAndroid Build Coastguard Worker
188*cda5da8dSAndroid Build Coastguard WorkerThe result can be used safely, it is put in parens to avoid nasty precedence
189*cda5da8dSAndroid Build Coastguard Workerissues.
190*cda5da8dSAndroid Build Coastguard Worker
191*cda5da8dSAndroid Build Coastguard Worker### `b4_lhs_value(SYMBOL-NUM, [TYPE])`
192*cda5da8dSAndroid Build Coastguard WorkerExpansion of `$$` or `$<TYPE>$`, for symbol `SYMBOL-NUM`.
193*cda5da8dSAndroid Build Coastguard Worker
194*cda5da8dSAndroid Build Coastguard Worker### `b4_rhs_data(RULE-LENGTH, POS)`
195*cda5da8dSAndroid Build Coastguard WorkerThe data corresponding to the symbol `#POS`, where the current rule has
196*cda5da8dSAndroid Build Coastguard Worker`RULE-LENGTH` symbols on RHS.
197*cda5da8dSAndroid Build Coastguard Worker
198*cda5da8dSAndroid Build Coastguard Worker### `b4_rhs_value(RULE-LENGTH, POS, SYMBOL-NUM, [TYPE])`
199*cda5da8dSAndroid Build Coastguard WorkerExpansion of `$<TYPE>POS`, where the current rule has `RULE-LENGTH` symbols
200*cda5da8dSAndroid Build Coastguard Workeron RHS.
201*cda5da8dSAndroid Build Coastguard Worker
202*cda5da8dSAndroid Build Coastguard Worker<!--
203*cda5da8dSAndroid Build Coastguard Worker
204*cda5da8dSAndroid Build Coastguard WorkerLocal Variables:
205*cda5da8dSAndroid Build Coastguard Workermode: markdown
206*cda5da8dSAndroid Build Coastguard Workerfill-column: 76
207*cda5da8dSAndroid Build Coastguard Workerispell-dictionary: "american"
208*cda5da8dSAndroid Build Coastguard WorkerEnd:
209*cda5da8dSAndroid Build Coastguard Worker
210*cda5da8dSAndroid Build Coastguard WorkerCopyright (C) 2002, 2008-2015, 2018-2021 Free Software Foundation, Inc.
211*cda5da8dSAndroid Build Coastguard Worker
212*cda5da8dSAndroid Build Coastguard WorkerThis file is part of GNU Bison.
213*cda5da8dSAndroid Build Coastguard Worker
214*cda5da8dSAndroid Build Coastguard WorkerThis program is free software: you can redistribute it and/or modify
215*cda5da8dSAndroid Build Coastguard Workerit under the terms of the GNU General Public License as published by
216*cda5da8dSAndroid Build Coastguard Workerthe Free Software Foundation, either version 3 of the License, or
217*cda5da8dSAndroid Build Coastguard Worker(at your option) any later version.
218*cda5da8dSAndroid Build Coastguard Worker
219*cda5da8dSAndroid Build Coastguard WorkerThis program is distributed in the hope that it will be useful,
220*cda5da8dSAndroid Build Coastguard Workerbut WITHOUT ANY WARRANTY; without even the implied warranty of
221*cda5da8dSAndroid Build Coastguard WorkerMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
222*cda5da8dSAndroid Build Coastguard WorkerGNU General Public License for more details.
223*cda5da8dSAndroid Build Coastguard Worker
224*cda5da8dSAndroid Build Coastguard WorkerYou should have received a copy of the GNU General Public License
225*cda5da8dSAndroid Build Coastguard Workeralong with this program.  If not, see <https://www.gnu.org/licenses/>.
226*cda5da8dSAndroid Build Coastguard Worker
227*cda5da8dSAndroid Build Coastguard Worker-->
228