Flex (y 2)

Ejemplo flex: C2HTML

A continuación, mostramos el código flex asociado al programa C2HTML con parte del código C eliminado, para poder mostrar con claridad como se representan las expresiones regulares que nos permitirán obtener, uno por uno, los diversos elementos del programa fuente C analizado. 1

odig		([0-7])
dig		([0-9])
xdig		([0-9a-fA-F])
lu		(([uUlL]|[uU][lL]|[lL][uU])?)

mant		({dig}+\.{dig}*|{dig}*\.{dig}+)
mant2		({dig}+)
exp		([eE][+-]?{dig}+)
fl		(([flFL]|[fF][lL]|[lL][fF])?)

esc		(\\(.|\n|{odig}{odig}?{odig}?|[xX]{xdig}+))
sp		([\ \t])

alnum		([a-zA-Z\$_0-9])
alpha		([a-zA-Z\$_])

directive	(define|undef|include|if|ifdef|ifndef|else|elif|endif|error|pragma)
%%
"/*"		{	PUSH(incomment); fprintf(OUTPUT, "<I>/*"); }
<incomment>\n	|
<incomment>.	pr(OUTPUT, yytext);
<incomment>TODO	|
<incomment>FIXME	|
<incomment>XXX		|
<incomment>ARGSUSED	|
<incomment>NEVERREACHED	fprintf(OUTPUT, "<U>%s</u>", yytext);
<incomment>"*/"		{	fprintf(OUTPUT, "*/</i>"); POP(); }

"#"{sp}*\n		fprintf(OUTPUT, "<B>%s</b>", yytext); 
"#"{directive}		{	PUSH(indirective);
				fprintf(OUTPUT, "<B>%s</b><I>", yytext);
			}
<indirective>defined	fprintf(OUTPUT, "<B>%s</b>", yytext);
<indirective>\\\n	fputs("\\\n", OUTPUT);
<indirective>\n		{	fputs("</i>\n", OUTPUT); POP(); }

auto			|
break			|
case			|
char			|
const			|
continue		|
default			|
do			|
double			|
else			|
enum			|
extern			|
float			|
for			|
goto			|
if			|
int			|
long			|
register		|
return			|
short			|
signed			|
sizeof			|
static			|
struct			|
switch			|
typedef			|
union			|
unsigned		|
void			|
volatile		|
while			fprintf(OUTPUT, "<B>%s</b>", yytext);

0{odig}*{lu}		|
[1-9]{dig}*{lu}		|
0[xX]{xdig}*{lu}	|
{mant}({exp})?{fl}	|
{mant2}{exp}{fl}	fprintf(OUTPUT, "<B><I>%s</i></b>", yytext);

\"			{	PUSH(instring); fputs("<B><I>\"", OUTPUT); }
<instring>\"		{	fputs("\"</i></b>", OUTPUT); POP(); }
<instring>{esc}		{	fputs("<U>", OUTPUT);pr(OUTPUT, yytext);
				fputs("</u>", OUTPUT);
			}
<instring>\n		|
<instring>.		pr(OUTPUT, yytext);

\'			{	PUSH(inchar); fputs("<B><I>\'", OUTPUT); }
<inchar>\'		{	fputs("\'</i></b>", OUTPUT); POP(); }
<inchar>{esc}		{	fputs("<U>", OUTPUT);pr(OUTPUT, yytext);
				fputs("</u>", OUTPUT);
			}
<inchar>\n		|
<inchar>.		fputs(yytext, OUTPUT);

<*>^"(@"[^@]*"@)\n"	fprintf(OUTPUT, "<%0.*s>", yyleng-5, yytext+2);

[a-zA-Z_\$]{alnum}*	{	/* identifier */
				HashEntry *he;
				he = hashTableLookup(&syms_table, yytext);
				/* .... Código no mostrado */
			}
"..."			|
"&&"			|
"-="			|
"~"			|
"+"			|
";"			|
"<<="			|
"&="			|
"->"			|
">>"			|
"%"			|
","			|
"<"			|
"^"			|
">>="			|
"*="			|
"/="			|
"^="			|
"&"			|
"-"			|
"="			|
"!="			|
"++"			|
"<<"			|
"|="			|
"."			|
">"			|
"|"			|
"%="			|
"+="			|
"<="			|
"||"			|
"/"			|
"?"			|
"--"			|
"=="			|
"!"			|
"*"			|
":"			pr(OUTPUT, yytext);

"["			|
"]"			|
"{"			|
"}"			|
"("			|
")"			fputs(yytext, OUTPUT);

\n			|
.			fputs(yytext, OUTPUT);

%%
/* functions */

/* Nuevamente, código no mostrado */

/* $Id: lexical.l,v 0.17 1999/06/24 22:06:40 luis Exp luis $ */

Referencias


Notas:


Luis.Colorado@Hispalinux.ES
Puentelarrá 31
28031 MADRID
http://SLUG.HispaLinux.ES/~luis/