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);
<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:
But because lex picks the longest matching rule first, it won't work here:
"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?


fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org

Reply via email to