Design lexical analyzer using lex tool

BATHULA PRAVEEN (BP)
0

 %{

#include<stdio.h>

int num_lines = 0;

}%

digit [0-9]

label [a-zA-Z_]|{digit})*

number {digit} + (\.{digit}+)?(E[+ -] ? {digit}+)?

%%

\/\/.* {printf("%s is a comment\n" , yytext);}

#.* {printf("%s is a preprocessor directive\n" , yytext);}

main\(\) {printf("%s is a main function\n",yytext);}

int|char|float|if|else|while|for| {printf("%s is keyword\n",yytext);}

"{" {printf("%s is a open brace\n",yytext);}

"}" {printf("%s is closed brace\n",yytext);}

"[" {printf("%s is a open Square brace\n",yytext);}

"]" {printf("%s is a closed Square brace\n",yytext);}

"(" {printf("%s is a open parentheses\n" ,yytext);}

")" {printf("%s is a closed parentheses\n",yytext);}

"++" {printf("%s is a increment operator\n",yytext);}

"--" {printf("%s is decrement operator\n",yytext);}

","|";"|"$" {printf("%s is special symbol\n",yytext);}

"+"|"-"|"*"|"/" {printf("%s is a arithmetic operator\n",yytext);}

"<"|"<="|">"|">=" {printf("%s is relational operator\n",yytext);}

"=" {printf("%s is a assignment operator\n",yytext);}

{id} {printf("%s is an identifier\n",yytext);}

{number} {printf("%s is a number\n",yytext);}

{id}\(\)|{id}\({id}\) {printf("%s is a function\n",yytext);}

{id}\[\]|{id}\[{number}] {printf("%s is an array\n",yytext);}

\"{id}\" {printf("%s is an string\n",yyetxt);

\n ++num_lines;

%%

main()

{

char fname[20];

FILE *fp;

printf("Enter the file name: ");

scanf("%s",fname);

fp = fopen(fname, "r");

if(fp == NULL)

printf("FI#LE doesnot exists");

else

{

yyin = fp;

yylex();

}

printf("no. of lines = %d " ,num_lines);

}

Post a Comment

0Comments

Post a Comment (0)