I don't think there is literature on writing something like flex.
You need: to input the script text and rules, the output tokens.
So we need a translator with dynamic rules that are set regexpname. Parsim rules of text parsing, using the rules and rasparenny regexpal, parsim text, translate into tokens. I think many will agree with me that it is better dragon book you will not be able to find anything.