Debian Codegraph @

documentation/manual/grammar/polymorphic.yo

raw
1 Bic() also supports em(polymorphic semantic values). Polymorphic semantic
2 values were developed as an alternative to using unions. Traditional unions
3 can still be used, but are now considered somewhat `old school'. Polymorphic
4 semantic values were added to b() as the result of a suggestion originally
5 made by Dallas A. Clement in September 2007.
6
7 In this section a simple example program is developed illustrating the use of
8 polymorphic semantic values. The sources of the example can be retrieved from
9 the distribution's lurl(poly) directory.
10
11 One may wonder why a tt(union) is still used by b() as bf(C++) offers
12 inherently superior approaches to combine multiple types into one union
13 type. The bf(C++) way to do so is by defining a polymorphic base class and a
14 series of derived classes implementing the various exclusive data types. The
15 tt(union) approach is still supported by b(), mainly for historic reasons as
16 it is supported by bf(bison)(1) and bf(bison++); dropping the tt(union) would
17 needlessly impede backward compatibility.
18
19 The preferred alternative to a tt(union), however, is a polymorphic base
20 class. The example program (cf. lurl(poly)) uses a polymorphic semantic value
21 type, supporting either tt(int) or tt(std::string) semantic values. These
22 types are asociated with tags (resp. tt(INT) and tt(TEXT)) using the
23 tt(%polymorphic) directive, discussed next.
24
25