06.01.2015 Views

Manual

Manual

Manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

54 Bison 2.3<br />

• Your macro should parenthesize its arguments, if need be, since the actual arguments<br />

may not be surrounded by parentheses. Also, your macro should expand to something<br />

that can be used as a single statement when it is followed by a semicolon.<br />

3.7 Bison Declarations<br />

The Bison declarations section of a Bison grammar defines the symbols used in formulating<br />

the grammar and the data types of semantic values. See Section 3.2 [Symbols], page 42.<br />

All token type names (but not single-character literal tokens such as ’+’ and ’*’) must<br />

be declared. Nonterminal symbols must be declared if you need to specify which data type<br />

to use for the semantic value (see Section 3.5.2 [More Than One Value Type], page 46).<br />

The first rule in the file also specifies the start symbol, by default. If you want some other<br />

symbol to be the start symbol, you must declare it explicitly (see Section 1.1 [Languages<br />

and Context-Free Grammars], page 11).<br />

3.7.1 Require a Version of Bison<br />

You may require the minimum version of Bison to process the grammar. If the requirement<br />

is not met, bison exits with an error (exit status 63).<br />

%require "version"<br />

3.7.2 Token Type Names<br />

The basic way to declare a token type name (terminal symbol) is as follows:<br />

%token name<br />

Bison will convert this into a #define directive in the parser, so that the function yylex<br />

(if it is in this file) can use the name name to stand for this token type’s code.<br />

Alternatively, you can use %left, %right, or %nonassoc instead of %token, if you wish<br />

to specify associativity and precedence. See Section 3.7.3 [Operator Precedence], page 55.<br />

You can explicitly specify the numeric code for a token type by appending a decimal or<br />

hexadecimal integer value in the field immediately following the token name:<br />

%token NUM 300<br />

%token XNUM 0x12d // a GNU extension<br />

It is generally best, however, to let Bison choose the numeric codes for all token types.<br />

Bison will automatically select codes that don’t conflict with each other or with normal<br />

characters.<br />

In the event that the stack type is a union, you must augment the %token or other<br />

token declaration to include the data type alternative delimited by angle-brackets (see<br />

Section 3.5.2 [More Than One Value Type], page 46).<br />

For example:<br />

%union { /* define stack type */<br />

double val;<br />

symrec *tptr;<br />

}<br />

%token NUM /* define token NUM and its type */<br />

You can associate a literal string token with a token type name by writing the literal<br />

string at the end of a %token declaration which declares the name. For example:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!