'Is there a way to run an action on a grammar that's already been parsed?

If I do:

my $parsed_grammar = PG.parse( $some_string );

Is there any way to to do something like the pseudo code below?

$parsed_grammar.run_action( $action_class.new );


Solution 1:[1]

No.

Your grammar is basically a program.

Contrary to other (regex) implementations, Raku grammars are basically just another way to write a class and methods. It's all code underneath. Code that can have callbacks for each method run. That's what your action class is: a way to specify the callbacks.

So, the parsing of your grammar happens at compile time. That creates code that gets run when you call .parse with the given string as the input.

Your misconception seems to be that running .parse on a grammar parses the grammar. It doesn't. It runs the grammar, it parses your input string.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elizabeth Mattijsen