|
| 1 | +# Grammar Files |
| 2 | + |
| 3 | +Lrama reads Bison-style grammar files. Each grammar file has four sections in |
| 4 | +order: |
| 5 | + |
| 6 | +1. **Prologue**: C code copied verbatim into the generated parser. |
| 7 | +2. **Declarations**: Bison-style directives such as `%token` and `%start`. |
| 8 | +3. **Grammar rules**: The productions and semantic actions. |
| 9 | +4. **Epilogue**: C code appended to the end of the generated parser. |
| 10 | + |
| 11 | +A minimal grammar looks like this: |
| 12 | + |
| 13 | +```yacc |
| 14 | +%token INTEGER |
| 15 | +%% |
| 16 | +input: INTEGER '\n'; |
| 17 | +%% |
| 18 | +``` |
| 19 | + |
| 20 | +## Symbols |
| 21 | + |
| 22 | +- **Terminals** are tokens returned by the lexer. |
| 23 | +- **Nonterminals** are syntactic groupings defined by rules. |
| 24 | + |
| 25 | +Lrama accepts the common `%token`, `%type`, `%left`, `%right`, and |
| 26 | +`%precedence` declarations in the declarations section. |
| 27 | + |
| 28 | +## Rules and actions |
| 29 | + |
| 30 | +Grammar rules use the standard Bison syntax. Semantic actions are C code blocks |
| 31 | +that run when a rule is reduced. |
| 32 | + |
| 33 | +```yacc |
| 34 | +expr: |
| 35 | + expr '+' expr { $$ = $1 + $3; } |
| 36 | + | INTEGER { $$ = $1; } |
| 37 | + ; |
| 38 | +``` |
| 39 | + |
| 40 | +## Parameterized rules |
| 41 | + |
| 42 | +Lrama extends Bison-style rules with parameterization. A nonterminal definition |
| 43 | +may accept other symbols as parameters, allowing you to reuse rule templates. |
| 44 | +Parameterized rules are defined with `%rule` and invoked like a nonterminal. |
| 45 | + |
| 46 | +```yacc |
| 47 | +%rule option(X) |
| 48 | + : /* empty */ |
| 49 | + | X |
| 50 | + ; |
| 51 | +
|
| 52 | +program: |
| 53 | + option(statement) |
| 54 | + ; |
| 55 | +``` |
| 56 | + |
| 57 | +When Lrama expands a parameterized rule, it creates a concrete nonterminal |
| 58 | +whose name encodes the parameters. The example above expands to a rule named |
| 59 | +`option_statement`. |
| 60 | + |
| 61 | +### Parameterized rules in the standard library |
| 62 | + |
| 63 | +Lrama ships a standard library of reusable parameterized rules in |
| 64 | +[`lib/lrama/grammar/stdlib.y`](../../lib/lrama/grammar/stdlib.y). Common |
| 65 | +patterns include: |
| 66 | + |
| 67 | +- `option(X)`: optional symbol. |
| 68 | +- `list(X)`: zero or more repetitions. |
| 69 | +- `nonempty_list(X)`: one or more repetitions. |
| 70 | +- `separated_list(separator, X)`: separated list with optional empty case. |
| 71 | +- `separated_nonempty_list(separator, X)`: separated list with at least one |
| 72 | + element. |
| 73 | +- `delimited(opening, X, closing)`: wrap a symbol with delimiters. |
| 74 | + |
| 75 | +You can reference these directly by including the standard library in your |
| 76 | +grammar or copy them into your own grammar file. |
| 77 | + |
| 78 | +### Semantic values and locations |
| 79 | + |
| 80 | +Parameterized rules support the same semantic action syntax as ordinary rules. |
| 81 | +If you add actions to a parameterized rule, the generated nonterminal keeps the |
| 82 | +action and location references intact. When you call a parameterized rule, the |
| 83 | +resulting nonterminal can be used like any other symbol in subsequent rules. |
| 84 | + |
| 85 | +## Inlining |
| 86 | + |
| 87 | +The `%inline` directive replaces all references to a symbol with its |
| 88 | +definition. It is useful for eliminating extra nonterminals, removing |
| 89 | +shift/reduce conflicts, or keeping small helper rules from polluting the symbol |
| 90 | +list. |
| 91 | + |
| 92 | +```yacc |
| 93 | +%inline opt_newline |
| 94 | + : /* empty */ |
| 95 | + | '\n' |
| 96 | + ; |
| 97 | +
|
| 98 | +lines: |
| 99 | + lines opt_newline line |
| 100 | + | line |
| 101 | + ; |
| 102 | +``` |
| 103 | + |
| 104 | +An inline rule does not create a standalone nonterminal in the output. Instead, |
| 105 | +its productions are substituted wherever the inline symbol is referenced. This |
| 106 | +is why `%inline` is often paired with parameterized rules (for example, |
| 107 | +`%inline ioption(X)` in the standard library) to build reusable templates |
| 108 | +without growing the symbol table. |
| 109 | + |
| 110 | +## Error recovery |
| 111 | + |
| 112 | +Use `error` tokens in rules and enable recovery with `-e` when generating the |
| 113 | +parser. For guidance, see the [Error Recovery](06-error-recovery.md) chapter. |
0 commit comments