lrama icon indicating copy to clipboard operation
lrama copied to clipboard

Add support for aliased Named References for actions of rhs in Parameterizing rules

Open ydah opened this issue 1 year ago • 4 comments

This PR support for aliased Named References for actions of rhs in Parameterizing rules.

Motivation

If there is a non-terminal symbol in the RHS of parameterizing rules as shown below, I want to access it in the Named Reference.

%rule plus_n(X): X term[number] { $$ = $1 + $number }
                ;

ydah avatar May 02 '24 09:05 ydah

Can I ask the use case of this functionality? We can use arbitrary name for the variable (X) then if we want to access Y by $alias, we can write %rule pair(X, alias):.

yui-knk avatar May 04 '24 01:05 yui-knk

@yui-knk I changed it to an appropriate example in PR description. For example, I think it makes sense to set an alias for nterms, which is not a parameter in rhs for parameterization rules. WDYT?

ydah avatar May 04 '24 03:05 ydah

IIUC aliased Named References for non parameter in RHS is already supported.

yui-knk avatar May 04 '24 03:05 yui-knk

@yui-knk Is there any blocker left for this pull request?

ydah avatar May 22 '24 11:05 ydah

@ydah I'm bit confused because:

  1. It seems the test case passes without source code changes
diff --git a/lib/lrama/grammar/binding.rb b/lib/lrama/grammar/binding.rb
index 21b6ad5..d215ad0 100644
--- a/lib/lrama/grammar/binding.rb
+++ b/lib/lrama/grammar/binding.rb
@@ -16,7 +16,7 @@ module Lrama
           resolved_args = symbol.args.map { |arg| resolve_symbol(arg) }
           Lrama::Lexer::Token::InstantiateRule.new(s_value: symbol.s_value, location: symbol.location, args: resolved_args, lhs_tag: symbol.lhs_tag)
         else
-          parameter_to_arg(symbol) || symbol
+          @parameter_to_arg[symbol.s_value] || symbol
         end
       end
$ bundle exec rake
...
Finished in 9.62 seconds (files took 0.27494 seconds to load)
219 examples, 0 failures
  1. It seems PR description is misaligned

If there is a non-terminal symbol in the RHS of parameterizing rules as shown below,

%rule plus_n(X): X term[number] { $$ = $1 + $number }
                ;

But I guess term might be terminal (!= non-terminal).

Could you check them?

yui-knk avatar May 25 '24 01:05 yui-knk

@yui-knk

  1. The test case was incorrect. I add commit https://github.com/ruby/lrama/commit/db76b6317eeb78b501b8ceb2d36f46e6f12c5f56 Correctly, specifying a Named Reference for a parameter results in the following error:
Failures:

  1) Lrama::Parser#parse when parameterizing rules when user defined rules with action with named references expands parameterizing rules
     Failure/Error: raise location.generate_error_message(message)

     RuntimeError:
       parameterizing_rules/user_defined/with_action_and_named_references.y:17:53: Referring symbol `summand` is not found.
       %rule sum(X, Y) <i>: X[summand] '+' Y[addend] { $$ = $summand + $addend; }
  1. Fixed an incorrect test case, so fixed the example. What do you think?

ydah avatar May 26 '24 15:05 ydah

LGTM!

yui-knk avatar Jun 01 '24 04:06 yui-knk