Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %option noyywrap
- %x comment_state
- %x nested_comment_state
- %x multi_comment_state
- %{
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- int line_count = 1;
- FILE *logout;
- FILE *tokenout;
- char arr[100];
- int comment_line;
- %}
- AlphaNumeric [a-zA-Z0-9]
- digits[0-9]
- %%
- "//" { BEGIN comment_state;}
- "/*" { comment_line = line_count;arr[0]=0;BEGIN multi_comment_state;}
- [ \t] { }
- [\n] {line_count++;}
- {digits}+ {fprintf(tokenout,"<CONST_INT,%s>\n",yytext);
- fprintf(logout,"Line no %d: Token <CONST_INT> Lexeme <%s> found .\n",line_count,yytext);}
- ({digits}*[\.]?{digits}+)|(({digits}*[\.]?{digits}+)([E]?[+|-]?{digits}+)) {fprintf(tokenout,"<CONST_FLOAT,%s>\n",yytext);
- fprintf(logout,"Line no %d: Token <CONST_FLOAT> Lexeme <%s> found .\n",line_count,yytext);}
- "if" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "for" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "do" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "int" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "float" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "void" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "switch" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "default" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "else" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "while" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "break" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "char" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "double" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "return" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "case" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- "continue" {fprintf(tokenout,"<IF>\n");
- fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
- . {}
- <comment_state>[^\n\\]+ {fprintf(logout,"Line no %d: Token <Comment> Lexeme <%s> found .\n",line_count,yytext);}
- <comment_state>[\n] {line_count++; BEGIN INITIAL;}
- <comment_state>[\\] {BEGIN nested_comment_state;}
- <nested_comment_state>[\n] {line_count++;BEGIN comment_state;}
- <nested_comment_state>. {fprintf(logout,"Line no %d: Token <Comment> Lexeme <%s> found .\n",line_count,yytext);BEGIN comment_state;}
- <multi_comment_state>[^*\n]+ { strcat(arr,yytext);}
- <multi_comment_state>[\n] {line_count++;strcat(arr,yytext);}
- <multi_comment_state>. {strcat(arr,yytext);}
- <multi_comment_state>"*/" {fprintf(logout,"Line no %d: Token <Comment> Lexeme <%s> found .\n",comment_line,arr);arr[0]=0;BEGIN INITIAL;}
- %%
- int main(int argc,char *argv[]){
- /*
- {digits}+
- {
- fprintf(tokenout,"<CONST_INT,%s>",yytext);
- fprintf(logout,"Line no %d: Token <CONST_INT> Lexeme <%s> found .\n",line_count,yytext);
- }*/
- if(argc!=2){
- printf("Please provide input file name and try again\n");
- return 0;
- }
- FILE *fin=fopen(argv[1],"r");
- if(fin==NULL){
- printf("Cannot open specified file\n");
- return 0;
- }
- logout = fopen("1405003_log.txt","w");
- tokenout = fopen("1405003_token.txt","w");
- yyin= fin;
- yylex();
- fclose(yyin);
- fclose(logout);
- fclose(tokenout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement