Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an infinite loop issue with recurrent calls to parameterizing rule #336

Merged
merged 3 commits into from
Jan 11, 2024

Conversation

ydah
Copy link
Collaborator

@ydah ydah commented Dec 31, 2023

This PR fixes the issue of an infinite loop when there is a recurrent parameterizing rule call.

For example, the following rule would have resulted in an infinite loop.

%rule list(X): /* empty */
             | X
             | list(X)
             ;

@yui-knk
Copy link
Collaborator

yui-knk commented Jan 6, 2024

I also want to resolve the issue that same rules are generated multiple times when a parameterizing rule is instantiated with same arguments.
For example file like below generates two option_number. This causes reduce/reduce conflicts because it is impossible to determine which option_number is used.

%union {
    int i;
}

%token number string

%rule option(Y) : /* empty */
                | Y
                ;

%%

program         : option(number)
                | string option(number)
                ;

%%
./exe/lrama --trace=rules tmp/sample.y
reduce/reduce conflicts: 3 found
Grammar rules:
$accept -> program, YYEOF
option_number -> ε
option_number -> number
program -> option_number
option_number -> ε
option_number -> number
program -> string, option_number

Both infinite loop and duplicated rules come from multiple instantiation of same parameterizing rule with same arguments. We can avoid it by recording the name of new rule's LHS and checking the name before building rules.
The records should be shared among all RuleBuilders, so that it might be an option to make parameterizing_rule_resolver to holds LHS names with hash. If you have better idea where it's recorded, please let me know.

ydah added 3 commits January 9, 2024 23:09
This PR fixes the issue of an infinite loop when there is a recurrent parameterizing rule call.

For example, the following rule would have resulted in an infinite loop.

```ruby
%rule list(X): /* empty */
             | X
             | list(X)
             ;
```
@ydah ydah force-pushed the fix-recursive-rule branch from 15a630b to f95fb31 Compare January 10, 2024 16:34
@ydah
Copy link
Collaborator Author

ydah commented Jan 10, 2024

Thanks for the review. Changed to avoid instantiating the same parameterization rule multiple times with the same arguments.

❯ ./exe/lrama --trace=rules tmp/sample.y
Grammar rules:
$accept -> program, YYEOF
option_number -> ε
option_number -> number
program -> option_number
program -> string, option_number

@yui-knk yui-knk merged commit 6cc1b54 into ruby:master Jan 11, 2024
16 checks passed
@yui-knk
Copy link
Collaborator

yui-knk commented Jan 11, 2024

Thank you!

@ydah ydah deleted the fix-recursive-rule branch January 11, 2024 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants