Semantic Values
Previous: <Grammar in Bison=>GrammarinB> * Next: <Semantic Actions=>SemanticAc> * Up: <Concepts=>Concepts>

#Wrap on
{fH3}Semantic Values{f}

A formal grammar selects tokens only by their classifications: for example,
if a rule mentions the terminal symbol `integer constant', it means that
{fEmphasis}any{f} integer constant is grammatically valid in that position.  The
precise value of the constant is irrelevant to how to parse the input: if
{fEmphasis}x+4{f} is grammatical then {fEmphasis}x+1{f} or {fEmphasis}x+3989{f} is equally
grammatical.

But the precise value is very important for what the input means once it is
parsed.  A compiler is useless if it fails to distinguish between 4, 1 and
3989 as constants in the program!  Therefore, each token in a Bison grammar
has both a token type and a {fUnderline}semantic value{f}.  \*Note <Semantics=>Semantics>: Defining Language Semantics,
for details.

The token type is a terminal symbol defined in the grammar, such as
{fCode}INTEGER{f}, {fCode}IDENTIFIER{f} or {fCode}','{f}.  It tells everything
you need to know to decide where the token may validly appear and how to
group it with other tokens.  The grammar rules know nothing about tokens
except their types.

The semantic value has all the rest of the information about the
meaning of the token, such as the value of an integer, or the name of an
identifier.  (A token such as {fCode}','{f} which is just punctuation doesn't
need to have any semantic value.)

For example, an input token might be classified as token type
{fCode}INTEGER{f} and have the semantic value 4.  Another input token might
have the same token type {fCode}INTEGER{f} but value 3989.  When a grammar
rule says that {fCode}INTEGER{f} is allowed, either of these tokens is
acceptable because each is an {fCode}INTEGER{f}.  When the parser accepts the
token, it keeps track of the token's semantic value.

Each grouping can also have a semantic value as well as its nonterminal
symbol.  For example, in a calculator, an expression typically has a
semantic value that is a number.  In a compiler for a programming
language, an expression typically has a semantic value that is a tree
structure describing the meaning of the expression.

