Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve ambiguities locally? #23

Open
mlemerre opened this issue Jan 31, 2024 · 11 comments
Open

Resolve ambiguities locally? #23

mlemerre opened this issue Jan 31, 2024 · 11 comments

Comments

@mlemerre
Copy link

Hi,

I just ported a whole parser using pacomb and it is very nice. One thing that I don't understand well is how to deal with ambiguity. Specifically, the merge function is attached to the %parser declaration, so as I understand it, the whole file is parsed, and then ambiguities are resolved; but this can already be made using parse_all_buffer. What I would prefer to do is to resolve ambiguites locally, i.e. being able to attach merge function to rules, but I could not find how to do that. The only thing I found is partial_parse_buffer, but I don't think using directly would work correctly.

Is there a way to solve this problem? Maybe by adding a and%parser rule suffices?

@craff
Copy link
Owner

craff commented Jan 31, 2024 via email

@mlemerre
Copy link
Author

mlemerre commented Feb 1, 2024

Thanks for your answer! I think my previous question was not very clear. If I only have one ambiguous recursive rule, the documentation makes it clear how to resolve the ambiguity:

let%parser [@merge f] rec f = ...

Now, if I have mutually recursive rules, I would normally write:

let%parser rec f = ...

and g = ...

But how can I now attach different merge functions to f and g? I think I can only attach a merge function to f.

@mlemerre
Copy link
Author

mlemerre commented Feb 1, 2024

(By the way, the link in the github readme to the full documentation is broken)

@craff
Copy link
Owner

craff commented Feb 1, 2024 via email

@craff
Copy link
Owner

craff commented Feb 1, 2024 via email

@mlemerre
Copy link
Author

mlemerre commented Feb 1, 2024

let%parser @.*** merge_f] rec f = ... and @.*** merge_g] g = ... should work. I use that with @.***. Did you try that ? Cheers, Christophe

I assume that you wrote

and [@merge merge_g] g =

(github removes everything that looks like a mail addresse). Actually I tried:

and [@merge merge_expr] expr [@merge merge_expr] =

which does not work. Only let %parser [@merge merge_f] works. If I underestood correctly, the merge function would then be attached to the rule f? In that case, I could reorder my grammer (but I would still have a problem if I want to attach two merge functions to two different rules).

@mlemerre
Copy link
Author

mlemerre commented Feb 1, 2024

Actually, it is working. I must have been doing something wrong, I am very sorry.

@mlemerre
Copy link
Author

mlemerre commented Feb 1, 2024

It seems that raising Give_up in the merge function discards one of the parsetrees. I wonder if this is expected behaviour it seems hard to predict which parse tree will be discarded.

(I am using merge function to detect and report parsing ambiguities; failwith works here, but Give_up seemed convenient to report the location of the problem).

@craff
Copy link
Owner

craff commented Feb 1, 2024 via email

@craff
Copy link
Owner

craff commented Feb 1, 2024 via email

@mlemerre
Copy link
Author

mlemerre commented Feb 1, 2024

The merge_with_pos would be perfect from my use case, where I use merge to display the possible parse trees (to ask the user to disambiguate). But you eventually removed it? I understand that computing pos is expensive, but I would have thought that passing around spos would not be...

For context: my use case is to use case to detect ambiguities. The grammar describes complex types meant to be used by various people, and I believe that resolving all ambiguities in the grammar by giving arbitrary priorities to all constructs is bad. I think it is safer to only have the obvious priorities, and to warn the user when there are multiple parse trees for its file, asking him to parenthesize. I have just caught some errors in our tests using this design.

This is why I needed a parser with support for ambiguous parsing, and pacomb worked very nicely. It is a great package which deserves to be more well known!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants