Hi Laurent,

Here I set my example up along the lines of your second suggestion.

grammar Sum {
  token TOP { ^ <Sum> $ }
  rule Sum { <Expr>+ % <op> }
  rule Expr { <num> | { self.incr } '[' ~ ']'  <Sum> { self.decr } }
  token op { <[-+]> }
  token num { \d+ }
  token flag { <?> }
  method incr { self.actions.incr }
  method decr { self.actions.decr }
}
class Act {
  has Int $.nest;
  method num ($/) { say ">>> $/ at nesting level $!nest" }
  method incr { $!nest++ }
  method decr { $!nest-- }
}

This works nicely and fulfils my requirements.

Thanks,
Theo

Laurent Rosenfeld schreef op 2018-08-01 23:00:
Hi Theo,

You probably cannot use a grammar rule to change a dynamic variable
(unless you include some action code into the rule), I agree, but I
think you can use an action method attached to a rule to do it. I have
actually done it recently in a real $work grammar that I intend to
present at The Perl Conference in Glasgow in two weeks from now. Your
use case and mine are admittedly very different, but it seems to me
that they are syntactically very similar.

As an alternative, you could use an action object with an attribute (a
mutable attribute, _is rw_) for the mode. Basically, you instantiate
an object of the actions class (with the desired attribute set to the
proper initial mode at object creation), and then pass the object
instead of the class to the grammar when calling the _parse_ method.
It should be easy then to modify the attribute as needed. This is
perhaps slightly cleaner than using a dynamic variable.

I hope this helps.
Laurent.

2018-08-01 21:21 GMT+02:00 Theo van den Heuvel
<vdheu...@heuvelhlt.nl>:

Hi Laurent,

dynamic variables were my first attempt (see original post). The
problem as I see it,
but I may well be mistaken, is that I cannot use the grammar rule to
change the variable, e.g,
switching it on before and off after handling the item in question,
Sum in my example, and use that in my action class,
because these actions take place later.

Best,
Theo

Laurent Rosenfeld schreef op 2018-08-01 20:57:
Theo,

if you define a dynamic variable (with the * twigil, for example
$*mode)) in the section of the code right before calling the parse
(or
equivalent) method, then your actions class should be able to read
it
and to modify it when needed. Then, it is a matter of defining your
action methods to do different things depending on the value of that
dynamic variable, and to change that variable value depending on the
result of the parsing.

Best,
Laurent.

2018-08-01 20:29 GMT+02:00 Theo van den Heuvel
<vdheu...@heuvelhlt.nl>:

Hi Laurent,

yes I have, but the mode switching is supposed to happen
mid-parsing. I hope to avoid having to interrupt the parse, because
picking up after a subparse is going to be hard.
I was looking for a way to communicate a change of mode with the
action class, but:
a) I don't think there is a way to make the grammar parameter
visible to the action class. At least, I can't think of one.
b) the relation between rule X in the grammar and method X in the
action class is hiding within the engine.
c) loosely formulated grammar and action handling don't seem to
share a timeline.

Thanks,

Laurent Rosenfeld schreef op 2018-08-01 19:57:
Hi Theo,

have you considered using only one grammar but simply calling it
with
two different actions classes as a parameter depending on the mode
you
want to use?

2018-08-01 16:41 GMT+02:00 Theo van den Heuvel
<vdheu...@heuvelhlt.nl>:

Hi Perl6-people,

I am looking for some inspiration. I am working with a grammar that
I would like to have operate in two different modes.
In both modes the rules are identical, but the methods should behave
differently. There are probably better ways to do this than I can
think of at this point.

As an illustration (not the actual problem), say we want to proces
arithmetical expressions in two modes: normally we just copy the
input, but within a pair of braces we directly calculate the result.
grammar actions are easy to write per mode, but the combination is
harder.

In our example we would like to convert "{3 + 5} + {2 -1}" into "8 +
1". In my original case the grammar is large.

So far, I have considered the following ideas:
- using a parameter on the grammar rules
- meddling with the AST
- using a dynamic variable (but actions are performed later)
- using the actions method (I don't see how I could use that here)

One way to get this done is by combining the first two ideas:

grammar Sum {
token TOP { ^ <Sum: 0> $ }
rule Sum($p) { <Expr>+ % <op> <flag>**{$p}}
rule Expr { <num> | '[' ~ ']' <Sum: 1> }
token op { <[-+]> }
token num { \d+ }
token flag { <?> }
}

The presence of the flag is the clue for the actions.

This is less than satisfactory because we would have to pass on the
parameter to all non-terminals.

Can anyone think of a better way to do this?

thanks,

--
Theo van den Heuvel

--
Theo van den Heuvel
Van den Heuvel HLT Consultancy

--
Theo van den Heuvel
Van den Heuvel HLT Consultancy

--
Theo van den Heuvel
Van den Heuvel HLT Consultancy

Reply via email to