ads

4. Write a Program to develop a lexical analyzer using Lex tool.

 

Lex  Program:

%{

int COMMENT=0;

%}

identifier[a-zA-Z][a-zA-Z0-9]*

%%

#.* {printf("\n %sisapreprocessordirective",yytext);}

int |

float |

char |

double |

while |

for |

struct |

typedef |

do |

if |

break |

continue |

void |

switch |

return |

else |

goto {printf("\n\t%s is a keyword",yytext);}

"/*" {COMMENT=1;}{printf("\n\t %s is a COMMENT",yytext);}

{identifier}\( {if(!COMMENT)printf("\nFUNCTION \n\t%s",yytext);}

\{  {if(!COMMENT)printf("\n BLOCK BEGINS");}

\}  {if(!COMMENT)printf("BLOCK ENDS ");}

{identifier}(\[[0-9]*\])? {if(!COMMENT) printf("\n %s IDENTIFIER",yytext);}

\".*\" {if(!COMMENT)printf("\n\t %s is a STRING",yytext);}

[0-9]+ {if(!COMMENT) printf("\n %s is a NUMBER ",yytext);}

\)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");}

\( ECHO;

= {if(!COMMENT)printf("\n\t %s is an ASSIGNMENT OPERATOR",yytext);}

\<= |

\>= |

\< |

== |

\> {if(!COMMENT) printf("\n\t%s is a RELATIONAL OPERATOR",yytext);}

%%

int main(int argc, char **argv)

{

FILE *file;

file=fopen("p4.c","r");

if(!file)

{

printf("could not open the file");

exit(0);

}

yyin=file;

yylex();

printf("\n");

return(0);

}

int yywrap()

{

return(1);

}

C  Program:

 

#include<stdio.h>

#include<conio.h>

void    main()

{

int     a,b,c;

a=1;

b=2;

c=a+b;

printf("Sum:%d",c);

}

   OUTPUT:

No comments:

Post a Comment