Replies: 1 comment
-
|
As of version v0.6.0 the above described feature is available in So, forget about the expanded grammar 😉 Feedback is appreciated. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
parol's approach to incorporating semantic user actions in grammar processing currently forces theuser to make a detour to the expanded grammar, a canonicalized version of the user's input grammar.
The applied canonicalization is necessary to be able to process the grammar efficiently by a parser.
Actually this is a technical requirement the user isn't necessarily aware of. And thus it is a
source of irritation.
A user actually wants to apply semantic actions to his initial input grammar. He doesn't even want
to know that there exists something like a transformed grammar.
This aspect was recognized in early stages of
parol's development and should be addressed by newfeatures of
parol. Thanks to Techcable for contributing a lot tothis idea. See also our discussion here
We took a look at some projects which dealt with similar problems like
ungrammar and
Oak.
As a result the idea was born to synthesize each semantic action of a transformed grammar so that it
structurally recreates the types of an abstract syntax tree the user expects.
Let's explain this with a short example.
A comma separated integer list grammar can be written like this:
The canonicalized version looks like this:
The interesting construct here is the repetition expression {...} and how it is transformed during
canonicalization:
where
a,randbare arbitrary expressions, is transformed toIn our example
ris actually"," Num, i.e. the expression between { and },aisNumandbis empty.
Ris a newly introduced non-terminal with one occurrence on the right-hand side of theinitial production and two productions where it forms the left-hand side.
The goal is now
Let's first detail the aspect of type deduction a bit.
Type deduction
In the example above the user may expect
Listto be of typeThe type of the vector in
struct Listcould have been a tuple too but I decided to use structureshere for several reasons. The first one is that a struct could be constructed via builder pattern.
The second one is that a production has symbols with "names" (token names and non-terminal names)
that could be used to generate named struct members, freeing the user from needing to know the
order of the tuple's elements. The third reason is that it is easier to have a fixed mapping between
productions and their types of kind
struct.To be clear, what the user expects to be the type of a semantic action is the type of the parameter
that is provided to his semantic action for a certain productions. In our example he wants his
semantic action for production
List: Num { "," Num };to have this signature:fn list(&mut self, arg: &List).For the semantic actions we want to synthesize the case is the other way around.
A semantic action for production
List: Num ListList;(taken from the transformedgrammar, known as expanded grammar) should have this signature:
fn list(&mut self, num: Num, list_list: ListList) -> List.To transform the input argument tuple into the output form we need to generate sematic code.
Now is the time to have a look at the semantic gap we have to fill in our generated actions.
Semantic content of the actions
All actions can be thought of functions that have input and output types. The semantic, i.e. the
code in the actions have at least one task. This task is to transform input values into a form that
represents the output value, resp. the output type. Some of the actions have additional tasks that
are defined by their position in the grammar. For instance "outer" actions, i.e. those that share
their names with the ones from the users grammar, have to call the related user's semantic actions
with the generated output type as argument.
Function with this format we want to synthesize completely from the structure of the user grammar
and the expanded grammar.
This synthesis of actions and types should be optional and the old approach should be still usable
to support users that want to have full control over their language processing and who don't have
problems to handle with expanded grammar and semantic actions on it.
Currently some experiments are conducted to test the feasibility of this concept.
Further updates and details will be posted here.
Everyone is encouraged to contribute to this discussion.
Beta Was this translation helpful? Give feedback.
All reactions