Tuesday, 4 February 2014

Lex Program to Identify the Keywords of C Language

Lex Program to Identify the Keywords of C Language:

%{
int count;
/* -----you can write any comments here-------
*  program to recognize the keywords
*/
%}

%%
 [%\t ] +         /* "+" indicates zero or more and this pattern use for ignoring the whitespaces*/

auto|
double|
if|
static|
break|
else|
int|
struct|
case|
enum|
long|
switch|
char|
extern|
near|
typedef|
const|
float|
continue|
register|

union|
unsigned|
void|
while|
default|
do|
goto|
signed|
while|signed|unsigned  {printf("C keyWord(%d) :\t %s",count,yytext);
[a-zA-Z]+ { printf("%s: is not the Keyword\n", yytext);
 %%
 main( )
{
yylex();
}

Output:

$$ lex example.l
$$ gcc example.c -ll
$$ ./a.out
while asd goto char for
C keyWord(1):        while
asd: is not the keyword
C keyWord(2):       goto
C keyWord(3):      char
C keyWord(4):       for
auto
C keyWord(5):        auto
^D
$$ 

 
 

No comments:

Post a Comment