In your scanner rule for new lines, instead of just doing nothing like you do with spaces and tabs, you need to check whether a semi-colon token should be inserted. So imagine something like: {eol} { if (needs_semi_colon()) return tSEMICOLON; } Now, to implement needs_semi_colon(), you need two pieces of information: are there any tokens on this line (we should not add semi colons to empty lines), and what is the last token on the line? You'll need to keep track of these two informations. If we are on a non-empty line, and the last token we have seen should trigger the insertion of a semi-colon, then needs_semi_colon() should return true, otherwise it returns false. -Vincent Foley-Bourgon