segeljakt / pratt

Pratt parser written in Rust
84 stars 5 forks source link

How to define a sequence? #8

Open oovm opened 1 year ago

oovm commented 1 year ago

I came across a special syntax where a space between two Primary expressions indicates a special connection.

For example:

"a"  "b"
// equivalent to
"a" + "b"

In the current situation, two consecutive Primary expressions will cause the following expressions to disappear directly.

Is there any suitable patch hacking?

segeljakt commented 1 year ago

Hi, that is an interesting point. I will try to find a fix, it might require some additions to the API. This page gives some ideas about how to support the juxtaposition operator in a pratt parser. In our case, I think we need to modify the lbp and led to do something when encountering a prefix and nilfix (primary) expression.

segeljakt commented 1 year ago

@oovm do you possibly have an example grammar to test?

oovm commented 11 months ago

@segeljakt

I created a test case here, the content after juxtaposition will be discarded.

https://github.com/oovm/wolfram-parser/blob/1d848b9f763da50b5a3712d0a930a591ea3259c8/projects/wolfram-parser/tests/main.rs#L10-L41

This is the implementation part of pratt parser:

https://github.com/oovm/wolfram-parser/blob/1d848b9f763da50b5a3712d0a930a591ea3259c8/projects/wolfram-parser/src/parser/from_str/parse_expr/mod.rs#L22-L87

oovm commented 11 months ago

There is another problem.

In regular language, prefix + and infix + can be distinguished by this expression:

Expr : Term (Infix Term)*
Term : Prefix* Factor Suffix*

But in languages with juxtaposition, only expressions like this can be used

Expr :  
    | Prefix
    | Infix
    | Factor 
    | Suffix

At this time infix + will be overwritten by prefix +.

Is there any way to mark such polymorphic operators?

oovm commented 11 months ago

This problem also exists with the prefix !(not) and the suffix !(factorial).

eg: x^5!y -> x^(5!)*y

image

In the lexer stage, I have no way of knowing whether the previous expression terminates.

segeljakt commented 10 months ago

Hi, I'm sorry for the late response. I am currently working towards a deadline so I haven't had time to take a look. I will return as soon as possible.