Advertisement
Guest User

flex

a guest
Mar 28th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. %option noyywrap
  2.  
  3. %x comment_state
  4. %x nested_comment_state
  5. %x multi_comment_state
  6.  
  7. %{
  8. #include<stdio.h>
  9. #include<stdlib.h>
  10. #include<string.h>
  11.  
  12. int line_count = 1;
  13. FILE *logout;
  14. FILE *tokenout;
  15. char arr[100];
  16. int comment_line;
  17. %}
  18.  
  19. AlphaNumeric [a-zA-Z0-9]
  20. digits[0-9]
  21.  
  22. %%
  23.  
  24. "//" { BEGIN comment_state;}
  25. "/*" { comment_line = line_count;arr[0]=0;BEGIN multi_comment_state;}
  26. [ \t] { }
  27. [\n] {line_count++;}
  28. {digits}+ {fprintf(tokenout,"<CONST_INT,%s>\n",yytext);
  29. fprintf(logout,"Line no %d: Token <CONST_INT> Lexeme <%s> found .\n",line_count,yytext);}
  30. ({digits}*[\.]?{digits}+)|(({digits}*[\.]?{digits}+)([E]?[+|-]?{digits}+)) {fprintf(tokenout,"<CONST_FLOAT,%s>\n",yytext);
  31. fprintf(logout,"Line no %d: Token <CONST_FLOAT> Lexeme <%s> found .\n",line_count,yytext);}
  32. "if" {fprintf(tokenout,"<IF>\n");
  33. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  34. "for" {fprintf(tokenout,"<IF>\n");
  35. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  36. "do" {fprintf(tokenout,"<IF>\n");
  37. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  38. "int" {fprintf(tokenout,"<IF>\n");
  39. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  40. "float" {fprintf(tokenout,"<IF>\n");
  41. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  42. "void" {fprintf(tokenout,"<IF>\n");
  43. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  44. "switch" {fprintf(tokenout,"<IF>\n");
  45. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  46. "default" {fprintf(tokenout,"<IF>\n");
  47. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  48. "else" {fprintf(tokenout,"<IF>\n");
  49. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  50. "while" {fprintf(tokenout,"<IF>\n");
  51. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  52. "break" {fprintf(tokenout,"<IF>\n");
  53. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  54. "char" {fprintf(tokenout,"<IF>\n");
  55. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  56. "double" {fprintf(tokenout,"<IF>\n");
  57. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  58. "return" {fprintf(tokenout,"<IF>\n");
  59. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  60. "case" {fprintf(tokenout,"<IF>\n");
  61. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  62. "continue" {fprintf(tokenout,"<IF>\n");
  63. fprintf(logout,"Line no %d: Token <IF> Lexeme <%s> found .\n",line_count,yytext);}
  64. . {}
  65. <comment_state>[^\n\\]+ {fprintf(logout,"Line no %d: Token <Comment> Lexeme <%s> found .\n",line_count,yytext);}
  66. <comment_state>[\n] {line_count++; BEGIN INITIAL;}
  67. <comment_state>[\\] {BEGIN nested_comment_state;}
  68. <nested_comment_state>[\n] {line_count++;BEGIN comment_state;}
  69. <nested_comment_state>. {fprintf(logout,"Line no %d: Token <Comment> Lexeme <%s> found .\n",line_count,yytext);BEGIN comment_state;}
  70. <multi_comment_state>[^*\n]+ { strcat(arr,yytext);}
  71. <multi_comment_state>[\n] {line_count++;strcat(arr,yytext);}
  72. <multi_comment_state>. {strcat(arr,yytext);}
  73. <multi_comment_state>"*/" {fprintf(logout,"Line no %d: Token <Comment> Lexeme <%s> found .\n",comment_line,arr);arr[0]=0;BEGIN INITIAL;}
  74.  
  75. %%
  76. int main(int argc,char *argv[]){
  77.  
  78. /*
  79. {digits}+
  80. {
  81. fprintf(tokenout,"<CONST_INT,%s>",yytext);
  82. fprintf(logout,"Line no %d: Token <CONST_INT> Lexeme <%s> found .\n",line_count,yytext);
  83. }*/
  84.  
  85. if(argc!=2){
  86. printf("Please provide input file name and try again\n");
  87. return 0;
  88. }
  89.  
  90. FILE *fin=fopen(argv[1],"r");
  91. if(fin==NULL){
  92. printf("Cannot open specified file\n");
  93. return 0;
  94. }
  95.  
  96. logout = fopen("1405003_log.txt","w");
  97. tokenout = fopen("1405003_token.txt","w");
  98. yyin= fin;
  99. yylex();
  100. fclose(yyin);
  101. fclose(logout);
  102. fclose(tokenout);
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement