Best practices for addressing the "Lexer hack" in BNFC

47 views
Skip to first unread message

Conor Quinn

unread,
Oct 10, 2019, 12:22:16 AM10/10/19
to BNFC Developers and Users
Hi! When a new type is defined in a program (In C, using typedef), it needs to be lexed as a type for the parser to handle it correctly. At this point in the compilation pipeline, lexing has already happened. Other systems have hacks around this issue but I'm not sure how to solve it in BNFC. (https://en.wikipedia.org/wiki/The_lexer_hack)

Andreas Abel

unread,
Oct 10, 2019, 3:52:50 AM10/10/19
to bnfc...@googlegroups.com, Conor Quinn
Hello Conor,

the wikipedia page you linked says:

"The Clang parser handles the situation in a completely different
way, namely by using a non-reference lexical grammar. Clang's lexer does
not attempt to differentiate between type names and variable names: it
simply reports the current token as an identifier. The parser then uses
Clang's semantic analysis library to determine the nature of the
identifier."

This would be my advice: defer resolving ambiguities to the
type-checking phase, where you also can give better error message.

--Andreas
Reply all
Reply to author
Forward
0 new messages