What is the data type of the stack elements in yacc/bison?
I have tried yacc manuals, some academic ppts but couldn't find anything/
Like $$, $1, $2
etc.
CodePudding user response:
The default type is int
.
From the Bison manual section 3.4.1, Data types of semantic values:
Bison normally uses the type int for semantic values if your program uses the same data type for all language constructs.
I strongly recommend bookmarking and reading the Bison manual. It's contains a highly readable introduction to the tool, and it actually describes how Bison works. On the internet, you'll find lots of ancient documents for legacy tools, which may be the documents you were looking at. In general, they do all say that the default type is int
, but you might have to look hard to find the specification. For example, you can find it in Stephen Johnson's often-linked Yacc documentation, on http://dinosaur.compilertools.net, in the subsection labelled "Support for arbitrary value types":
By default, the values returned by actions and the lexical analyzer are integers.
You can also find it in the Posix specifications for yacc, which might be present on your machine as man 1p yacc
, available online in the Linux man-pages repository on http://man7.org. Alternatively, you can consult the official Posix specification for yacc on opengroup.org (it's possible that you have to register to see that page, but it's basically the same information as you'll find on man7.org.). In both cases, you'll find the description of the default type if you read very carefully; it's in the section labelled "Declarations section" in the description of the %type
declaration, where it says:
If this construct [i.e. %type ...] is present, yacc shall perform type checking; if this construct is not present, the parse stack shall hold only the int type.
Note that this is not a totally accurate description of Bison. If you use the declaration