Open Khazuar opened 5 years ago
I would probably parse them both to the same structure, i.e. (* (+ f g) (+ x y))
, and then have a step after parsing which transforms the first one into the mapsto
thing.
Generally you want your parser just to deal with syntax. The semantics (meaning) of the notation, can be handled in a later stage. :)
Sent with GitHawk
I am trying to parse mathematical expressions using a nearley grammar.
I stumbled over expressions like
(f + g)(x + y)
. Depending on whatf
andg
are, the expression needs to be parsed completely differently.f + g
is shorthand syntax for(x mapsto f(x) + g(x))
and therefore the whole expression is something like(x mapsto f(x) + g(x))(x + y)
.I figure this could be resolved if the parser knew about the types of the provided variables, something achievable by using some sort of context (I read through the discussions in #203 and #63) for the parsing (or dirty globals). This would also deal with unknown variable names.
It would probably also be possible to preprocess the input (maybe even during lexing) and e.g. replace
x
withnumber[x]
andf
withfunction[f]
. This doesn't really feel like a proper solution though. It's basically encoding the context into the input.