Allow to use aliased named references for actions of RHS in parameterizing rules.
%rule sum(X, Y): X[summand] '+' Y[addend] { $$ = $summand + $addend }
;
Allow to use named references for actions of RHS in parameterizing rules caller side.
opt_nl: '\n'?[nl] <str> { $$ = $nl; }
;
Allow to define parameterizing rules in the middle of the grammar.
%rule defined_option(X): /* empty */
| X
;
%%
program : defined_option(number) <i>
| defined_list(number) <i>
;
%rule defined_list(X): /* empty */ /* <--- here */
| defined_list(X) number
;
Support to report unused terminal symbols.
Run exe/lrama --report=terms
to show unused terminal symbols.
❯ exe/lrama --report=terms sample/calc.y
11 Unused Terms
0 YYerror
1 YYUNDEF
2 '\\\\'
3 '\\13'
4 keyword_class2
5 tNUMBER
6 tPLUS
7 tMINUS
8 tEQ
9 tEQEQ
10 '>'
Support to report unused rules.
Run exe/lrama --report=rules
to show unused rules.
❯ exe/lrama --report=rules sample/calc.y
3 Unused Rules
0 unused_option
1 unused_list
2 unused_nonempty_list
Support %locations
directive to ensure compatibility with Bison.
Change to %locations
directive not set by default.
Support to warning redefined parameterizing rules.
Run exe/lrama -W
or exe/lrama --warnings
to show redefined parameterizing rules.
❯ exe/lrama -W sample/calc.y
parameterizing rule redefined: redefined_method(X)
parameterizing rule redefined: redefined_method(X)
Support to -v
and --verbose
option.
These options align with Bison behavior. So same as '--report=state' option.
Allow to specify tag on callee side of parameterizing rules.
%union {
int i;
}
%rule with_tag(X) <i>: X { $$ = $1; }
;
Allow to use named references for actions of RHS in parameterizing rules.
%rule option(number): /* empty */
| number { $$ = $number; }
;
Allow to nested parameterizing rules with tag.
%union {
int i;
}
%rule nested_nested_option(X): /* empty */
| X
;
%rule nested_option(X): /* empty */
| nested_nested_option(X) <i>
;
%rule option(Y): /* empty */
| nested_option(Y) <i>
;
User can use 'symbol'?
, 'symbol'+
and 'symbol'*
in RHS of user defined parameterizing rules.
%rule with_word_seps(X): /* empty */
| X ' '+
;
Support trace actions for debugging.
Run exe/lrama --trace=actions
to show grammar rules with actions.
❯ exe/lrama --trace=actions sample/calc.y
Grammar rules with actions:
$accept -> list, YYEOF {}
list -> ε {}
list -> list, LF {}
list -> list, expr, LF { printf("=> %d\n", $2); }
expr -> NUM {}
expr -> expr, '+', expr { $$ = $1 + $3; }
expr -> expr, '-', expr { $$ = $1 - $3; }
expr -> expr, '*', expr { $$ = $1 * $3; }
expr -> expr, '/', expr { $$ = $1 / $3; }
expr -> '(', expr, ')' { $$ = $2; }
Support inlining for rules.
The %inline
directive causes all references to symbols to be replaced with its definition.
%rule %inline op: PLUS { + }
| TIMES { * }
;
%%
expr : number { $$ = $1; }
| expr op expr { $$ = $1 $2 $3; }
;
as same as
expr : number { $$ = $1; }
| expr '+' expr { $$ = $1 + $3; }
| expr '*' expr { $$ = $1 * $3; }
;
User can specify the type of mid rule action by tag (<bar>
) instead of specifying it with in an action.
primary: k_case expr_value terms?
{
$<val>$ = p->case_labels;
p->case_labels = Qnil;
}
case_body
k_end
{
...
}
can be written as
primary: k_case expr_value terms?
{
$$ = p->case_labels;
p->case_labels = Qnil;
}<val>
case_body
k_end
{
...
}
%destructor
for midrule action is invoked only when tag is specified by Typed Midrule Actions.
Difference from Bison's Typed Midrule Actions is that tag is postposed in Lrama however it's preposed in Bison.
Bison supports this feature from 3.1.
Support preceded
, terminated
and delimited
rules.
program: preceded(opening, X)
// Expanded to
program: preceded_opening_X
preceded_opening_X: opening X
program: terminated(X, closing)
// Expanded to
program: terminated_X_closing
terminated_X_closing: X closing
program: delimited(opening, X, closing)
// Expanded to
program: delimited_opening_X_closing
delimited_opening_X_closing: opening X closing
User can set codes for freeing semantic value resources by using %destructor
.
In general, these resources are freed by actions or after parsing.
However if syntax error happens in parsing, these codes may not be executed.
Codes associated to %destructor
are executed when semantic value is popped from the stack by an error.
%token <val1> NUM
%type <val2> expr2
%type <val3> expr
%destructor {
printf("destructor for val1: %d\n", $$);
} <val1> // printer for TAG
%destructor {
printf("destructor for val2: %d\n", $$);
} <val2>
%destructor {
printf("destructor for expr: %d\n", $$);
} expr // printer for symbol
Bison supports this feature from 1.75b.
Provide functionalities for Bring Your Own Stack.
Ruby’s Ripper library requires their own semantic value stack to manage Ruby Objects returned by user defined callback method. Currently Ripper uses semantic value stack (yyvsa
) which is used by parser to manage Node. This hack introduces some limitation on Ripper. For example, Ripper can not execute semantic analysis depending on Node structure.
Lrama introduces two features to support another semantic value stack by parser generator users.
- Callback entry points
User can emulate semantic value stack by these callbacks. Lrama provides these five callbacks. Registered functions are called when each event happen. For example %after-shift function is called when shift happens on original semantic value stack.
%after-shift
function_name%before-reduce
function_name%after-reduce
function_name%after-shift-error-token
function_name%after-pop-stack
function_name
$:n
variable to access index of each grammar symbols
User also needs to access semantic value of their stack in grammar action. $:n
provides the way to access to it. $:n
is translated to the minus index from the top of the stack.
For example
primary: k_if expr_value then compstmt if_tail k_end
{
/*% ripper: if!($:2, $:4, $:5) %*/
/* $:2 = -5, $:4 = -3, $:5 = -2. */
}
If %no-stdlib
directive is set, Lrama doesn't load Lrama standard library for
parameterizing rules, stdlib.y.
Allow to pass an instantiated rule to other parameterizing rules.
%rule constant(X) : X
;
%rule option(Y) : /* empty */
| Y
;
%%
program : option(constant(number)) // Nested rule
;
%%
Allow to use nested parameterizing rules when define parameterizing rules.
%rule option(x) : /* empty */
| X
;
%rule double(Y) : Y Y
;
%rule double_opt(A) : option(double(A)) // Nested rule
;
%%
program : double_opt(number)
;
%%
Allow to define parameterizing rule by %rule
directive.
%rule pair(X, Y): X Y { $$ = $1 + $2; }
;
%%
program: stmt
;
stmt: pair(ODD, EVEN) <num>
| pair(EVEN, ODD) <num>
;
Allow to specify type of rules by specifying tag, <i>
in below example.
Tag is post-modification style.
%union {
int i;
}
%%
program : option(number) <i>
| number_alias? <i>
;
Support function call style parameterizing rules for option
, nonempty_list
and list
.
Support separated_list
and separated_nonempty_list
parameterizing rules.
program: separated_list(',', number)
// Expanded to
program: separated_list_number
separated_list_number: ε
separated_list_number: separated_nonempty_list_number
separated_nonempty_list_number: number
separated_nonempty_list_number: separated_nonempty_list_number ',' number
program: separated_nonempty_list(',', number)
// Expanded to
program: separated_nonempty_list_number
separated_nonempty_list_number: number
separated_nonempty_list_number: separated_nonempty_list_number ',' number
Parameterizing rules are template of rules. It's very common pattern to write "list" grammar rule like:
opt_args: /* none */
| args
;
args: arg
| args arg
Lrama supports these suffixes:
?
: option+
: nonempty list*
: list
Idea of Parameterizing rules comes from Menhir LR(1) parser generator (https://gallium.inria.fr/~fpottier/menhir/manual.html#sec32).
Replace Lrama's parser from hand written parser to LR parser generated by Racc.
Lrama uses --embedded
option to generate LR parser because Racc is changed from default gem to bundled gem by Ruby 3.3 (ruby#132).
Make error recovery function configurable on runtime by two new macros.
YYMAXREPAIR
: Expected to return max length of repair operations.%parse-param
is passed to this function.YYERROR_RECOVERY_ENABLED
: Expected to return bool value to determine error recovery is enabled or not.%parse-param
is passed to this function.
Support token insert base Error Recovery.
-e
option is needed to generate parser with error recovery functions.
Instead of positional references like $1
or $$
,
named references allow to access to symbol by name.
primary: k_class cpath superclass bodystmt k_end
{
$primary = new_class($cpath, $bodystmt, $superclass);
}
Alias name can be declared.
expr[result]: expr[ex-left] '+' expr[ex.right]
{
$result = $[ex-left] + $[ex.right];
}
Bison supports this feature from 2.5.
%parse-param
are added to these macros and functions to remove ytab.sed hack from Ruby.
YY_LOCATION_PRINT
YY_SYMBOL_PRINT
yy_stack_print
YY_STACK_PRINT
YY_REDUCE_PRINT
yysyntax_error
See also: ruby/ruby#7807
When -
is given as grammar file name, reads the grammar source from STDIN, and takes the next argument as the input file name. This mode helps pre-process a grammar source.
This is the first version migrated to Ruby. This version generates "parse.c" compatible with Bison 3.8.2.