note
- As Bison reads tokens, it pushes them onto a stack along with their semantic values. The stack is called the parser stack. Pushing a token is traditionally called shifting.
- When the last n tokens and groupings shifted match the components of a grammar rule, they can be combined according to that rule. This is called reduction
- The lookahead token is stored in the variable yychar. Its semantic value and location, if any, are stored in the variables yylval and yylloc.
- This situation, where either a shift or a reduction would be valid, is called a shift/reduce conflict, Bison is designed to resolve these conflicts by choosing to shift, unless otherwise directed by operator precedence declarations.
if_stmt:
"if" expr "then" stmt
| "if" expr "then" stmt "else" stmt
;
if x then if y then win; else lose;
if x then do; if y then win; else lose; end;
==========
if x then if y then win; else lose;
if x then do; if y then win; end; else lose;
- %nonassoc declares that it is a syntax error to find the same operator twice “in a row" creates run-time error: using the operator in a associative way is a syntax error.
- %precedence allows to define only precedence and no associativity at all creates compile-time errors: an operator can be involved in an associativity-related conflict,
- %prec modifier declares the precedence of a particular rule by specifying a terminal symbol whose precedence should be used for that rule.
- the resolution of conflicts works by comparing the precedence of the rule being considered with that of the lookahead token. If the token’s precedence is higher, the choice is to shift. If the rule’s precedence is higher, the choice is to reduce
- A reduce/reduce conflict occurs if there are two or more rules that apply to the same sequence of input
- Bison resolves a reduce/reduce conflict by choosing to use the rule that appears first in the grammar