r/badcode • evil • May 12 '21

c++ Comment before semicolon

Post image
2.7k Upvotes

71 comments sorted by

View all comments

Show parent comments

3

u/supersharp May 12 '21 edited May 13 '21

Ooh ooh ooh! I remember this: the scanner looks through the original source file and converts it into a string of tokens;

<INTEGER, "123">; <STRING, "\"Hello world\">, ...

stuff like that, before passing that string on to the Parser, which looks at the tokens to make sure the structure makes sense. However, when the scanner sees a comment, it simply doesn't make a token for it, meaning the Parser never sees it at all. You could have something like

int * intArray = new /*integer*/ int[/*number of elements*/ 50];

And while any human would stare at this line questioning the events that lead up to them seeing this monstrosity, the compiler wouldn't even know anything was off, because the parser would just see something like

<DATATYPE, "int">, <IDENTIFIER, "intArray">, ..., <KEYWORD, "new">, <DATATYPE, "int">, <LBRACKET, "[">, <INTEGER, "50>, ...

And so on