Grammar Documentation
Grammar Reference
Generated May 8, 2026 · 15 rules · BISON · Generated by DevDiagrams

grammar_file

entry point
The top-level rule. A Bison grammar file consists of an optional
prologue, a declarations section, the first "%%" separator, one or
more grammar rules, and an optional epilogue.
grammar_file
: prologue_opt declarations "%%" rules epilogue_opt
;
prologue_opt declarations "%%" rules epilogue_opt

prologue_opt

An optional C declarations block enclosed in "%{" and "%}".
Contents are copied verbatim to the top of the generated parser
file, before the definition of yyparse. May be omitted entirely
if no C declarations are needed.
prologue_opt
: /* empty */
| "%{" PROLOGUE_CONTENT "%}"
;
"%{" PROLOGUE_CONTENT "%}"
PROLOGUE_CONTENT

epilogue_opt

An optional epilogue section introduced by a second "%%".
Contains arbitrary C code copied verbatim to the end of the
generated parser file. May be omitted if empty.
epilogue_opt
: /* empty */
| "%%" EPILOGUE_CONTENT
;
"%%" EPILOGUE_CONTENT
EPILOGUE_CONTENT

declarations

Zero or more Bison declarations appearing before the first "%%".
Declarations define terminal and nonterminal symbols, specify
operator precedence and associativity, and configure parser
options.
declarations
: /* empty */
| declarations declaration
;
declarations declaration

declaration

A single Bison declaration. Covers token declarations, precedence
and associativity declarations, type annotations, union definitions,
start symbol, conflict suppression, and various parser directives.
A bare action block may also appear as a declaration.
declaration
: "%token" tag_opt symbol_list
| "%left" tag_opt symbol_list
| "%right" tag_opt symbol_list
| "%nonassoc" tag_opt symbol_list
| "%type" tag nonterminal_list
| "%union" ACTION_CONTENT
| "%start" IDENTIFIER
| "%expect" INTEGER
| "%pure_parser"
| "%error_verbose"
| "%defines"
| "%locations"
| "%glr-parser"
| "%skeleton" STRING_LITERAL
| "%output" STRING_LITERAL
| "%name-prefix" STRING_LITERAL
| "%require" STRING_LITERAL
| "%prec" symbol
| ACTION_CONTENT
;
"%token" tag_opt symbol_list "%left" tag_opt symbol_list "%right" tag_opt symbol_list "%nonassoc" tag_opt symbol_list "%type" tag nonterminal_list "%union" ACTION_CONTENT "%start" IDENTIFIER "%expect" INTEGER "%pure_parser" "%error_verbose" "%defines" "%locations" "%glr-parser" "%skeleton" STRING_LITERAL "%output" STRING_LITERAL "%name-prefix" STRING_LITERAL "%require" STRING_LITERAL "%prec" symbol ACTION_CONTENT
tag_optsymbol_listtagnonterminal_listACTION_CONTENTIDENTIFIERINTEGERSTRING_LITERALsymbol

tag_opt

An optional type tag used in token, precedence, and type
declarations to associate a semantic value type from the %union
with a symbol. Enclosed in angle brackets. e.g. <val>
tag_opt
: /* empty */
| tag
;
tag

tag

A type tag. An identifier enclosed in angle brackets referring to
a member name in the %union declaration.
e.g. "<val>", "<tptr>"
tag
: "<" IDENTIFIER ">"
;
"<" IDENTIFIER ">"
IDENTIFIER

symbol_list

A non-empty list of symbol declarations appearing after a %token,
%left, %right, or %nonassoc directive.
symbol_list
: symbol_decl
| symbol_list symbol_decl
;
symbol_decl symbol_list symbol_decl

symbol_decl

A single symbol declaration within a token or precedence directive.
May be a named token identifier, a named token with an explicit
numeric code, or a character literal token.
e.g. NUM, NUM 300, '+'
symbol_decl
: IDENTIFIER
| IDENTIFIER INTEGER
| CHAR_LITERAL
;
IDENTIFIER IDENTIFIER INTEGER CHAR_LITERAL
IDENTIFIERINTEGERCHAR_LITERAL

nonterminal_list

A non-empty list of nonterminal symbol names following a %type
directive. All listed nonterminals are assigned the type specified
by the preceding tag.
nonterminal_list
: IDENTIFIER
| nonterminal_list IDENTIFIER
;
IDENTIFIER nonterminal_list IDENTIFIER

rules

One or more grammar rules appearing after the first "%%".
rules
: rule
| rules rule
;
rule rules rule

rule

A single grammar rule. A nonterminal symbol name followed by ":",
one or more alternatives separated by "|", and terminated by ";".
e.g. expr : expr "+" term | term ;
rule
: IDENTIFIER ":" rule_alternatives ";"
;
IDENTIFIER ":" rule_alternatives ";"

rule_alternatives

A list of one or more rule alternatives separated by "|". Each
alternative defines one way to derive the rule's nonterminal.
rule_alternatives
: rule_rhs
| rule_alternatives "|" rule_rhs
;
rule_rhs rule_alternatives "|" rule_rhs

rule_rhs

A single rule alternative. A sequence of zero or more symbols,
mid-rule action blocks, and optional "%prec" precedence overrides.
An empty alternative matches the empty string (epsilon).
rule_rhs
: /* empty */
| rule_rhs symbol
| rule_rhs ACTION_CONTENT
| rule_rhs "%prec" symbol
;
rule_rhs symbol rule_rhs ACTION_CONTENT rule_rhs "%prec" symbol
rule_rhssymbolACTION_CONTENT

symbol

A grammar symbol appearing in a rule alternative. May be a named
token or nonterminal identifier, a character literal token, or a
string alias for a token.
symbol
: IDENTIFIER
| CHAR_LITERAL
| STRING_LITERAL
;
IDENTIFIER CHAR_LITERAL STRING_LITERAL
IDENTIFIERCHAR_LITERALSTRING_LITERAL