مثالی از فایل Flex


در ادامه یک فایل Lex از ویرایش Flex می‌آید. که یک رشته‌ای از اعداد صحیح را از ورودی، تشخیص داده و در خروجی نمایشش می‌دهد:
/*** Definition section ***/
٪{
/* C code to be copied verbatim */

include


٪}
/* This tells flex to read only one input file */
٪option noyywrap
٪٪
/*** Rules section ***/
/* [۰-۹]+ matches a string of one or more digits */
[۰-۹]+ {
/* yytext is a string containing the matched text. */
printf(«Saw an integer: ٪s\n», yytext);
}
. { /* Ignore all other characters. */ }
٪٪
/*** C Code section ***/
int main(void)
{
/* Call the lexer, then quit. */
yylex();
return ۰;
}
اگر رشته ورودی به Flex ارسال شود، آنرا به فایل C (lex.yy.c) تبدیل می‌کند. و به فایل قابل اجرا، کامپایل می‌شود که رشته‌ای از اعداد صحیح را تشخیص داده و در خروجی نمایش می‌دهد. به‌عنوان مثال اگر رشتهٔ زیر را به‌عنوان ورودی بدهیم: abc۱۲۳z.!&*۲ghj۶ برنامه، خروجی زیر را خواهد داشت:

Saw an integer: ۱۲۳ Saw an integer: ۲ Saw an integer: ۶




.: Weblog Themes By Pichak :.