[fpc-pascal] tply: start conditions

Martok listbox at martoks-place.de
Fri Aug 10 12:06:24 CEST 2018


Hi all,

this is more of a generic lex question, but since plex doesn't support the flex extension that could easily fix it, I'm asking here.

Take these lex rules:
----------------------------------------------------------------------
%start INCOMLINE
%%
"//"                    start(INCOMLINE);
<INCOMLINE>\n           start(0);
<INCOMLINE>.            yymore;
[a-zA-Z_]([a-zA-Z0-9_])* return(ID);
----------------------------------------------------------------------

The idea is that the comment start sequence "//" puts the lexer in the INCOMLINE state, which eats everything until the next
newline, then goes back into the start state. This works for lines like that:
  Foo//####
But because lex picks the longest matching rule first, it won't work here:
  Foo//Bar
"Bar" still gets parsed as an ID token, because the identifier rule is valid for any state and considered longer.

Flex has Condition Scopes (see http://dinosaur.compilertools.net/flex/flex_11.html near the end), where one could just put all
generic rules in a "INITIAL only" scope. This is a bit awkward if one has to write the prefix every time...

I have solved this in the past by including a mini scanner in the action because comments are easy, but still... does anyone
have a better idea to do this right in the lexer?

-- 
Regards,
Martok





More information about the fpc-pascal mailing list