>
> 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.
...
> As an illustration (not the actual problem), say we want to proces
> arithmetical expressions in two modes:
sorry, false alarm. It does work as advertised.
Theo van den Heuvel
Theo van den Heuvel schreef op 2018-08-03 22:39:
Hi all,
My attempt at a solution below does not work. In larger examples the
decr gets called before the actions within Sum are processed. Maybe my
hunch that this would cause
Hi all,
My attempt at a solution below does not work. In larger examples the
decr gets called before the actions within Sum are processed. Maybe my
hunch that this would cause time order problems was correct. I need to
do some more researching. I'll post my findings.
best wishes,
Theo van
Hi Laurent,
Here I set my example up along the lines of your second suggestion.
grammar Sum {
token TOP { ^ $ }
rule Sum { + % }
rule Expr { | { self.incr } '[' ~ ']' { self.decr } }
token op { <[-+]> }
token num { \d+ }
token flag { }
method incr { self.actions.incr }
Hi Laurent,
thanks. I particularly like the second idea, Incidentally I used an
instantiated action class recently to configure the transformation in
another grammar, so I should have thought of that.
Great fan of "THink Perl6" by the way.
Good luck in Glasgow,
Theo
Laurent Rosenfeld
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
Hi Laurent,
the code below seems to do what I want. Even without a dynamic variable.
Maybe I should test more thoroughly.
my $nest = 0;
grammar Sum {
token TOP { ^ $ }
rule Sum { + % }
rule Expr { | { $nest++ } '[' ~ ']' { $nest-- } }
token op { <[-+]> }
token num { \d+ }
Hi Laurent,
I will do some experimenting, because I could be wrong about the timing
thing. A first experiment seems to work.
I will post my solution when I am satisfied that it works.
thanks,
Theo
Theo van den Heuvel schreef op 2018-08-01 21:21:
Hi Laurent,
dynamic variables were my first
Hello Theo,
have you considered placing the code for the actions directly inside the
grammar's tokens and rules instead? You can just { make 123 } right in
the middle and it'll have the same effect as doing that in an action
method, but you'll be able to put code after it, too.
HTH
- Timo
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
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
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
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 :
>
>
> Hi Perl6-people,
>
> I am looking for some inspiration. I am working with a
Hi Yary,
no, i haven't. I need to think about this. I have trouble seeing how
this would solve my problem. As I indicated: the grammar itself does not
change. It is only the methods that need to change. I don't think I
could use the actions method to actually select an Actions class.
Have you considered subclassing your grammar? The child inherits the rules
from the parent, and an override the changed methods. Or, the parent
grammar could be rules-and-common-methods-only, and then have two child
grammas for "inside parens" vs "outside parens."
Not sure the mechanics of
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
16 matches
Mail list logo