kach / nearley

📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.6k stars 230 forks source link

Parsing typed/sorted expressions #437

Open Khazuar opened 5 years ago

Khazuar commented 5 years ago

I am trying to parse mathematical expressions using a nearley grammar.

I stumbled over expressions like (f + g)(x + y). Depending on what f and g are, the expression needs to be parsed completely differently.

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 with number[x] and f with function[f]. This doesn't really feel like a proper solution though. It's basically encoding the context into the input.

tjvr commented 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