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

Can't find type of argument #166

Closed
nblei opened this issue Aug 4, 2023 · 6 comments
Closed

Can't find type of argument #166

nblei opened this issue Aug 4, 2023 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@nblei
Copy link

nblei commented Aug 4, 2023

I am trying to add support for unions to veryl:

The relevant addition to the grammar is below

UnionDeclaration: Union Identifier LBrace UnionList RBrace;

UnionList: UnionGroup { Comma UnionGroup } [ Comma ];

UnionGroup: { Attribute } ( LBrace UnionList RBrace | UnionItem );

UnionItem: Identifier Colon ScalarType;

Note that this is largely mimicking the existing grammar for structs:

StructDeclaration: Struct Identifier LBrace StructList RBrace;

StructList: StructGroup { Comma StructGroup } [ Comma ];

StructGroup: { Attribute } ( LBrace StructList RBrace | StructItem );

StructItem: Identifier Colon ScalarType;

If I replace UnionItem in the definition of UnionGroup with StructItem and remove the definition of UnionItem, parol generates a parser successfully, otherwise it errors with

  error: Parol error
   = Can't find type of argument UnionItem
   = No details

I would like to have UnionItem as a separate non-terminal, as the auto-generated data structures do not include parent references, thus distinguishing between a StructItem in a StructDeclaration and a StructItem in a UnionDeclaration would involve something let setting global state, which I would like to avoid.

Full grammar: https://github.com/nblei/veryl/blob/add_token/crates/parser/veryl.par

@jsinger67
Copy link
Owner

jsinger67 commented Aug 4, 2023

The error message is misleading and I will have a look at this.
But generally StructItem and UnionItem are structural identical and therefore also StructList and UnionList will be.
Try to factor them out in a more generally MemberList resp. MemberItem. So both can use the same and ambiguity should be resolved.

@jsinger67
Copy link
Owner

@nblei ,
I can reproduce the problem you described with your changes.
I'm pretty sure, the symbol table resolves the type of the UnionItem erroneously to the already inserted StructItem because of structural identity.
Currently I’m not sure if this is really a bug and how parol should handle structural identity in general.
Let me explain why.
If you introduce two non-terminals with the same RHS of their productions like

StructItem: Identifier Colon ScalarType;
UnionItem: Identifier Colon ScalarType;

you have at least made your grammar unnecessary complex. The types parol would normally derive from your grammar have been doubled and the compile time for parol’s grammar analysis has increased.

On the other hand, from a user’s perspective the types may be structural identical, but they can be different in terms of their implementation.

I will think about this, but currently I tend to think this is a bug in parol.

@jsinger67
Copy link
Owner

image
This confirms my guess. The symbol table used the already existing type StructItem.

@nblei
Copy link
Author

nblei commented Aug 5, 2023

Yeah, I get that, hence why I didn't report this as a bug :-).

In my use case, which is using parol as a parser for a hardware description language, I am all in favor of increased capabilities at increased computational cost, since lexing/parsing the language is computationally insignificant in relation to logic synthesis, APR, etc.

@jsinger67 jsinger67 added the bug Something isn't working label Aug 11, 2023
@jsinger67 jsinger67 self-assigned this Aug 11, 2023
@jsinger67 jsinger67 removed the bug Something isn't working label Aug 11, 2023
@jsinger67
Copy link
Owner

jsinger67 commented Aug 11, 2023

@nblei
I found that the loss of the type-information is happening in a very early stage of grammar processing.
To be precise, during the left factoring phase.
Since this is a very fundamental step in grammar processing that has huge influence on the number of lookahead tokens the parser needs to consider, I think I must rethink my assessment of about how to classify this issue.
I was willing to accept that this is a bug, which I refrain to do for now.
Thus, I ask for some deferral.

@jsinger67 jsinger67 added the bug Something isn't working label Aug 12, 2023
jsinger67 added a commit that referenced this issue Aug 12, 2023
 => eliminate_duplicates
 * This fixes issue Can't find type of argument #166
@jsinger67
Copy link
Owner

jsinger67 commented Aug 12, 2023

Fixed with commit b9020fd
This commit is based on 8e78a8b (Removed optimization in grammar transformation => eliminate_duplicates)

A version 0.23.1 will soon be released 😊
Have fun! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants