Grammar Documentation
Grammar Reference
grammar_file
entry pointThe 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.
prologue, a declarations section, the first "%%" separator, one or
more grammar rules, and an optional epilogue.
Grammar definition
grammar_file
: prologue_opt declarations "%%" rules epilogue_opt
;
Railroad diagram
References
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.
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.
Grammar definition
prologue_opt
: /* empty */
| "%{" PROLOGUE_CONTENT "%}"
;
Railroad diagram
Referenced by
References
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.
Contains arbitrary C code copied verbatim to the end of the
generated parser file. May be omitted if empty.
Grammar definition
epilogue_opt
: /* empty */
| "%%" EPILOGUE_CONTENT
;
Railroad diagram
Referenced by
References
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 define terminal and nonterminal symbols, specify
operator precedence and associativity, and configure parser
options.
Grammar definition
declarations
: /* empty */
| declarations declaration
;
Railroad diagram
Referenced by
References
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.
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.
Grammar definition
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
;
Railroad diagram
Referenced by
References
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>
declarations to associate a semantic value type from the %union
with a symbol. Enclosed in angle brackets. e.g. <val>
Grammar definition
tag_opt
: /* empty */
| tag
;
Railroad diagram
Referenced by
References
tag
A type tag. An identifier enclosed in angle brackets referring to
a member name in the %union declaration.
e.g. "<val>", "<tptr>"
a member name in the %union declaration.
e.g. "<val>", "<tptr>"
Grammar definition
tag
: "<" IDENTIFIER ">"
;
Railroad diagram
Referenced by
References
IDENTIFIER
symbol_list
A non-empty list of symbol declarations appearing after a %token,
%left, %right, or %nonassoc directive.
%left, %right, or %nonassoc directive.
Grammar definition
symbol_list
: symbol_decl
| symbol_list symbol_decl
;
Railroad diagram
Referenced by
References
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, '+'
May be a named token identifier, a named token with an explicit
numeric code, or a character literal token.
e.g. NUM, NUM 300, '+'
Grammar definition
symbol_decl
: IDENTIFIER
| IDENTIFIER INTEGER
| CHAR_LITERAL
;
Railroad diagram
Referenced by
References
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.
directive. All listed nonterminals are assigned the type specified
by the preceding tag.
Grammar definition
nonterminal_list
: IDENTIFIER
| nonterminal_list IDENTIFIER
;
Railroad diagram
Referenced by
References
IDENTIFIERnonterminal_list
rules
One or more grammar rules appearing after the first "%%".
Grammar definition
rules
: rule
| rules rule
;
Railroad diagram
Referenced by
References
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 ;
one or more alternatives separated by "|", and terminated by ";".
e.g. expr : expr "+" term | term ;
Grammar definition
rule
: IDENTIFIER ":" rule_alternatives ";"
;
Railroad diagram
Referenced by
References
IDENTIFIERrule_alternatives
rule_alternatives
A list of one or more rule alternatives separated by "|". Each
alternative defines one way to derive the rule's nonterminal.
alternative defines one way to derive the rule's nonterminal.
Grammar definition
rule_alternatives
: rule_rhs
| rule_alternatives "|" rule_rhs
;
Railroad diagram
Referenced by
References
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).
mid-rule action blocks, and optional "%prec" precedence overrides.
An empty alternative matches the empty string (epsilon).
Grammar definition
rule_rhs
: /* empty */
| rule_rhs symbol
| rule_rhs ACTION_CONTENT
| rule_rhs "%prec" symbol
;
Railroad diagram
Referenced by
References
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.
token or nonterminal identifier, a character literal token, or a
string alias for a token.
Grammar definition
symbol
: IDENTIFIER
| CHAR_LITERAL
| STRING_LITERAL
;
Railroad diagram
Referenced by
References
IDENTIFIERCHAR_LITERALSTRING_LITERAL