Posted by Jes Rahbek Klinke on 1999-09-27
Hi Frank
Your problem is that Flex tries to find the longest match starting at the current position, if two different clauses matches the same length the first is chosen. I can see that you identifier expressions only matches one character, if you instead wrote [0-9a-zA-Z]*, it would match the longest possible string of alphanumeric characters. If you place this clause last in the list you get your desired functionality.
The code would look like this
const |
int |
[...]
void |
volatile { fprintf(OUTPUT, "<font color=\"%s\">%s</font>", TYPES
,yytext); }
auto |
[...]
while |
main { fprintf(OUTPUT, "<font color=\"%s\">%s</font>", KEYWORDS,
yytext); }
[0-9a-zA-Z] { fprintf(OUTPUT, "<font color=\"%s\">%s</font>", IDENTIFIERS
,yytext); }
I hope this helps
Yours
Jes
>Okay I have the following lex code which generates a c to html compiler
>for converting C source code to syntax highlighted html. The only
>problem I am having is when there are keywords found inside of other
>words. For example if you compile this and then run it on the generated
>lex.yy.c you can see what I mean.
>
>to compile it:
>flex test.l
>gcc -o test lex.yy.c
>./test lex.yy.c test.html
>Then view the HTML in a browser and you can see what I mean.
>What I want to know is how to make regular expressions exclusive?
>Meaning that if I have a regular expression to match say the word
>"const" it won't find that word inside say "test_const". This is the whole
>problem now. If a keyword is found inside another word it gets
>highlighted with another color which is not what I want to happen.
Sponsored by Wrox Press - www.wrox.com. Programmer to Programmer (TM)
Previous post | Next post | Timeline | Home