logoalt Hacker News

hackyhackytoday at 5:35 AM2 repliesview on HN

What is the typedef problem?


Replies

nxobjecttoday at 5:45 AM

If stevefan1999's referring to a nasty frontend issue, it might be due to the fact that a name introduced by a typedef and an identical identifier can mingle in the same scope, which makes parsing pretty nasty – e.g. (example from source at end):

  typedef int AA;
  
  void foo()
  {
    AA AA;            /\* OK - define variable AA of type AA */
    int BB = AA * 2;  /\* OK - AA is just a variable name here \*/
  }

  void bar()
  {
    int aa = sizeof(AA), AA, bb = sizeof(AA);
  }

https://eli.thegreenplace.net/2011/05/02/the-context-sensiti...

I don't know off the top of my head whether there's a parser framework that makes this parse "straightforward" to express.

show 1 reply
waherntoday at 5:56 AM

Lexical parsing C is simple, except that typedef's technically make it non-context-free. See https://en.wikipedia.org/wiki/Lexer_hack When handwriting a parser, it's no big deal, but it's often a stumbling block for parser generators or other formal approaches. Though, I recall there's a PEG-based parser for C99/C11 floating around that was supposed to be compliant. But I'm having trouble finding a link, and maybe it was using something like LPeg, which has features beyond pure PEG that help with context dependent parsing.

show 2 replies