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

Improve errors where declared types are expected #2478

Open
kfcripps opened this issue Jul 28, 2020 · 3 comments
Open

Improve errors where declared types are expected #2478

kfcripps opened this issue Jul 28, 2020 · 3 comments
Labels
enhancement This topic discusses an improvement to existing compiler code.

Comments

@kfcripps
Copy link
Contributor

When the user supplies a token which is not a declared type, where a declared type is expected, the parser produces various types of syntax errors, many of which are not as accurate and helpful as they could be. I would also argue that this type of error is not really a "syntax error," but this point is less important than the first.

Here are just a few examples, but there are certainly others that can be found by looking through the grammar in p4parser.ypp.

  • An invalid instantiation, such as UndeclaredType() x; produces the error "syntax error, unexpected (."

  • An invalid variableDeclaration, such as UndeclaredType x; produces the error "syntax error, unexpected ;, expecting (."

  • An invalid typedefDeclaration, such as typedef UndeclaredType NewType;, produces the error "syntax error, unexpected IDENTIFIER, expecting ENUM or HEADER or HEADER_UNION or STRUCT."

  • An invalid structField, such as

struct A {
    UndeclaredType f;
}

produces the error "syntax error, unexpected IDENTIFIER "UndeclaredType"."

I would expect most of the above errors to be more along the lines of: "error: Declaration for UndeclaredType not found."

I think that this is a fairly common type of user error (not just in P4, but in all typed programming languages), so it would be nice for these errors to be improved upon, if it is not terribly difficult to do so. :)

@mihaibudiu
Copy link
Contributor

All these errors are default errors produced by bison. I personally don't know how to improve on them, but I agree it would be nice to have nicer error messages.

@mihaibudiu mihaibudiu added the enhancement This topic discusses an improvement to existing compiler code. label Jul 28, 2020
@jafingerhut
Copy link
Contributor

Related issue: #4813

@jafingerhut
Copy link
Contributor

A related comment on one technique to improve some error messages like these, and difficulties that one can encounter in using this approach, from Chris Dodd here: #4812 (comment)

"In general, to improve these parse errors, you need to add code (rules) to the bison parser that matches in the problematic cases and then emits a more specific/better error. Doing this without introducing conflicts and breaking things that should work is sometimes tricky."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This topic discusses an improvement to existing compiler code.
Projects
None yet
Development

No branches or pull requests

3 participants