Guest User

Untitled

a guest
Nov 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. %%
  2.  
  3. [t]+
  4.  
  5. is |
  6.  
  7. am |
  8.  
  9. are |
  10.  
  11. was |
  12.  
  13. were {printf("%s: is a verb",yytext);}
  14.  
  15. [a-zA-Z]+ {printf("%s: is a verb",yytext);}
  16.  
  17. . |n
  18.  
  19. %%
  20.  
  21. int main(int argc, char *argv[]){
  22. yyin = fopen(argv[1], "r");
  23. yylex();
  24. fclose(yyin);
  25. }
  26.  
  27. %option noyywrap
  28. %x COMMENT_SINGLE
  29. %x COMMENT_MULTI
  30.  
  31. %top{
  32. /* for strndup */
  33. #include <string.h>
  34. }
  35.  
  36. %{
  37. char* commentStart;
  38. %}
  39.  
  40. %%
  41.  
  42. [ntr ]+ {
  43. /* ignore whitespace */ }
  44.  
  45. <INITIAL>"//" {
  46. /* begin of single-line comment */
  47. commentStart = yytext;
  48. BEGIN(COMMENT_SINGLE);
  49. }
  50.  
  51. <COMMENT_SINGLE>n {
  52. /* end of single-line comment */
  53. char* comment = strndup(commentStart, yytext - commentStart);
  54. printf("'%s': was a single-line commentn", comment);
  55. free(comment);
  56. BEGIN(INITIAL);
  57. }
  58.  
  59. <COMMENT_SINGLE>[^n]+ {
  60. /* suppress whatever is in the comment */
  61. }
  62.  
  63. <INITIAL>"/*" {
  64. /* begin of multi-line comment */
  65. commentStart = yytext;
  66. BEGIN(COMMENT_MULTI);
  67. }
  68.  
  69. <COMMENT_MULTI>"*/" {
  70. /* end of multi-line comment */
  71. char* comment = strndup(commentStart, yytext + 2 - commentStart);
  72. printf("'%s': was a multi-line commentn", comment);
  73. free(comment);
  74. BEGIN(INITIAL);
  75. }
  76.  
  77. <COMMENT_MULTI>. {
  78. /* suppress whatever is in the comment */
  79. }
  80.  
  81. <COMMENT_MULTI>n {
  82. /* don't print newlines */
  83. }
  84.  
  85. is |
  86. am |
  87. are |
  88. was |
  89. were {
  90. printf("'%s': is a verbn", yytext);
  91. }
  92.  
  93. [a-zA-Z]+ {
  94. printf("'%s': is not a verbn", yytext);
  95. }
  96.  
  97. . {
  98. /* don't print everything else */
  99. }
  100.  
  101. %%
  102.  
  103. int main(int argc, char *argv[]){
  104. yyin = fopen(argv[1], "r");
  105. yylex();
  106. fclose(yyin);
  107. }
Add Comment
Please, Sign In to add comment