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

Keeping a node even it has zero matches #344

Open
iK4tsu opened this issue Feb 19, 2024 · 1 comment
Open

Keeping a node even it has zero matches #344

iK4tsu opened this issue Feb 19, 2024 · 1 comment

Comments

@iK4tsu
Copy link
Contributor

iK4tsu commented Feb 19, 2024

Given the grammar

Example:

Begin < AList
AList < A*
A <- 'a'

It would be nice to keep AList even for zero matches, given that 0 is a valid match. I expect something like

Example[0,0][]
  +-Example.Begin[0,0][]
    +-Example.AList[0,0][]

It is possible to achieve something like this by using '^AList', however, this also implies that all rules not defined in the grammar are kept, which creates unwanted noise. Moreover, since there is no way to halt the persistence, i.e. all child nodes share the same constraint, it causes even more noise when more than zero elements match.

Either all zero or more constraints would need to be kept in the final tree, which is probably undesired given the breaking change, or a new symbol that enforces the tagged rule, and only the tagged rule, to always be kept, could be added. Unless there is an intended and already implemented way of achieving this.

This affects constraints that can match with zero elements, meaning that the zero or one constraint would work similarly.

Edit: I didn't test this grammar. I think the description is simple enough to understand the desired outcome.

@veelo
Copy link
Collaborator

veelo commented Feb 20, 2024

I agree that there is either too little or a little too much.

/+dub.sdl:
dependency "pegged" version="~>0.4.9"
+/
import std;

void main()
{
    import pegged.grammar;

    mixin(grammar(`
    Example:
        Begin <^ AList
        AList <- A*
        A <- 'a'
    `));
                  
    writeln(Example(""));
}

Output:

Example[0, 0][]
 +-Example.Begin[0, 0][]
    +-Example.AList[0, 0][]
       +-zeroOrMore!(Example.A)[0, 0][]

I haven't studied the docs or the code in detail, but I would expect the last line not to be there. Relevant docs: https://github.com/PhilippeSigaud/Pegged/wiki/Tree-Decimation.

The question is, how big of a deal is this? Does the added noise in the parse tree cause problems big enough to invest in a solution? Perhaps, instead of a fix to Pegged itself, it is viable to change the tree using a semantic action? https://github.com/PhilippeSigaud/Pegged/wiki/Semantic-Actions

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