Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.46 KB | None | 0 0
  1. %{
  2. //A, B, C, D, E, F, G, H, I, J, K, L
  3. int line=1, column=1, aux_column, aux_line;
  4. int semicolon_flag = 0;
  5. int flag;
  6. int strlit_flag = 1;
  7. int strlitAuxSize = 0;
  8. char *strlitAux;
  9.  
  10. %}
  11.  
  12. INT "int"
  13. PACKAGE "package"
  14. FUNC "func"
  15. RETURN "return"
  16. VAR "var"
  17. IF "if"
  18. ELSE "else"
  19. FOR "for"
  20. FLOAT32 "float32"
  21. BOOL "bool"
  22. numero [0-9]+
  23. DIG [0-9]
  24. ID [a-zA-Z_][a-zA-Z0-9_]*
  25. INTLIT (0[xX][0-9a-fA-F]+)|(0[0-7]+)|([0-9]+)
  26. REALLIT (({numero})*\.?({numero})+|({numero})+\.?({numero})*)((e|E)[\-\+]?({numero})+)?
  27. SEMICOLON ";"
  28. RPAR ")"
  29. LPAR "("
  30. LBRACE "{"
  31. RBRACE "}"
  32. RSQ "]"
  33. LSQ "["
  34. STAR "*"
  35. PLUS "+"
  36. MINUS "-"
  37. COMMA ","
  38. BLANKID "_"
  39. AND "&&"
  40. ASSIGN "="
  41. DIV "/"
  42. EQ "=="
  43. GE ">="
  44. GT ">"
  45. LE "<="
  46. LT "<"
  47. MOD "%"
  48. NE "!="
  49. OR "||"
  50. NOT "!"
  51. STRING "string"
  52. PRINT "fmt.Println"
  53. PARSEINT "strconv.Atoi"
  54. CMDARGS "os.Args"
  55. RESERVED "++"|"--"|break|case|chan|const|continue|default|defer|fallthrough|go|goto|import|interface|map|range|select|struct|switch|type
  56. newline "\n"|"\r"|"\r\n"
  57. separator \t|" "|\v|\f
  58. escape \\\\|\\n|\\f|\\r|\\t|\\\"
  59.  
  60. %X MULTI_COMMENT
  61. %X SINGLE_COMMENT
  62. %X STRLITState
  63. %%
  64.  
  65. {separator} {column += yyleng;}
  66. {SEMICOLON} {if (flag == 1) printf("SEMICOLON\n"); semicolon_flag = 0;}
  67. {BLANKID} {if (flag == 1) printf("BLANKID\n"); column += yyleng;}
  68. {PACKAGE} {if (flag == 1) printf("PACKAGE\n"); column += yyleng;}
  69. {RETURN} {if (flag == 1) printf("RETURN\n");semicolon_flag = 1;}
  70. {AND} {if (flag == 1) printf("AND\n"); column += yyleng; semicolon_flag = 0;}
  71. {ASSIGN} {if (flag == 1) printf("ASSIGN\n"); column += yyleng; semicolon_flag = 0;}
  72. {STAR} {if (flag == 1) printf("STAR\n"); column += yyleng; semicolon_flag = 0;}
  73. {COMMA} {if (flag == 1) printf("COMMA\n"); column += yyleng; semicolon_flag = 0;}
  74. {DIV} {if (flag == 1) printf("DIV\n"); column += yyleng; semicolon_flag = 0;}
  75. {EQ} {if (flag == 1) printf("EQ\n"); column += yyleng; semicolon_flag = 0;}
  76. {GE} {if (flag == 1) printf("GE\n"); column += yyleng; semicolon_flag = 0;}
  77. {GT} {if (flag == 1) printf("GT\n"); column += yyleng; semicolon_flag = 0;}
  78. {LBRACE} {if (flag == 1) printf("LBRACE\n"); semicolon_flag = 0; column += yyleng;}
  79. {LE} {if (flag == 1) printf("LE\n"); column += yyleng; semicolon_flag = 0;}
  80. {LPAR} {if (flag == 1) printf("LPAR\n"); semicolon_flag = 0; column += yyleng;}
  81. {LSQ} {if (flag == 1) printf("LSQ\n"); semicolon_flag = 0; column += yyleng;}
  82. {LT} {if (flag == 1) printf("LT\n"); column += yyleng; semicolon_flag = 0;}
  83. {MINUS} {if (flag == 1) printf("MINUS\n"); column += yyleng; semicolon_flag = 0;}
  84. {MOD} {if (flag == 1) printf("MOD\n"); column += yyleng; semicolon_flag = 0;}
  85. {NE} {if (flag == 1) printf("NE\n"); column += yyleng; semicolon_flag = 0;}
  86. {NOT} {if (flag == 1) printf("NOT\n"); column += yyleng; semicolon_flag = 0;}
  87. {OR} {if (flag == 1) printf("OR\n"); column += yyleng; semicolon_flag = 0;}
  88. {PLUS} {if (flag == 1) printf("PLUS\n"); column += yyleng; semicolon_flag = 0;}
  89. {RBRACE} {if (flag == 1) printf("RBRACE\n"); semicolon_flag = 1; column += yyleng;}
  90. {RPAR} {if (flag == 1) printf("RPAR\n"); semicolon_flag = 1; column += yyleng;}
  91. {RSQ} {if (flag == 1) printf("RSQ\n"); semicolon_flag = 1; column += yyleng;}
  92. {ELSE} {if (flag == 1) printf("ELSE\n"); column += yyleng; semicolon_flag = 0;}
  93. {FOR} {if (flag == 1) printf("FOR\n"); column += yyleng; semicolon_flag = 0;}
  94. {IF} {if (flag == 1) printf("IF\n"); column += yyleng; semicolon_flag = 0;}
  95. {VAR} {if (flag == 1) printf("VAR\n"); column += yyleng; semicolon_flag = 0;}
  96. {INT} {if (flag == 1) printf("INT\n"); column += yyleng; semicolon_flag = 0;}
  97. {FLOAT32} {if (flag == 1) printf("FLOAT32\n"); column += yyleng; semicolon_flag = 0;}
  98. {BOOL} {if (flag == 1) printf("BOOL\n"); column += yyleng; semicolon_flag = 0;}
  99. {STRING} {if (flag == 1) printf("STRING\n"); column += yyleng; semicolon_flag = 0;}
  100. {PRINT} {if (flag == 1) printf("PRINT\n"); column += yyleng; semicolon_flag = 0;}
  101. {PARSEINT} {if (flag == 1) printf("PARSEINT\n"); column += yyleng; semicolon_flag = 0;}
  102. {FUNC} {if (flag == 1) printf("FUNC\n"); column += yyleng; semicolon_flag = 0;}
  103. {CMDARGS} {if (flag == 1) printf("CMDARGS\n"); column += yyleng; semicolon_flag = 0;}
  104. {INTLIT} {if (flag == 1) printf("INTLIT(%s)\n",yytext); semicolon_flag = 1; column += yyleng;}
  105. {REALLIT} {if (flag == 1) printf("REALLIT(%s)\n",yytext); semicolon_flag = 1; column += yyleng;}
  106. {RESERVED} {if (flag == 1) printf("RESERVED(%s)\n",yytext); column += yyleng; semicolon_flag = 0;}
  107. {ID} {if (flag == 1) printf("ID(%s)\n",yytext); semicolon_flag = 1; column += yyleng;}
  108. {newline} {line++; column = 1; if (semicolon_flag == 1 && flag == 1)printf("SEMICOLON\n");semicolon_flag=0;}
  109.  
  110. \/\/ {BEGIN SINGLE_COMMENT;
  111. column+=yyleng; }
  112.  
  113. <SINGLE_COMMENT>{newline} {line++;column=1;if(semicolon_flag == 1 && flag == 1) printf("SEMICOLON\n");semicolon_flag = 0;BEGIN 0;}
  114. <SINGLE_COMMENT>. {column+=yyleng;}
  115.  
  116. \/\* {BEGIN MULTI_COMMENT;
  117. aux_column = column;
  118. aux_line = line;
  119. column += yyleng;}
  120.  
  121. <MULTI_COMMENT>({newline}) {line += 1;
  122. column = 1;
  123. if(semicolon_flag == 1 && flag == 1) printf("SEMICOLON\n");
  124. semicolon_flag = 0;}
  125.  
  126. <MULTI_COMMENT>\*\/ {column+=yyleng;
  127. BEGIN 0;}
  128.  
  129. <MULTI_COMMENT><<EOF>> {printf("Line %d, column %d: unterminated comment\n", aux_line, aux_column);
  130. column+=yyleng; BEGIN 0;}
  131.  
  132. <MULTI_COMMENT>. {column += yyleng;}
  133.  
  134. "\"" {BEGIN STRLITState;
  135. aux_column = column;
  136. aux_line = line;
  137. column += yyleng;
  138. strlit_flag = 1;
  139. strlitAux = realloc(strlitAux,strlitAuxSize+1);
  140. strlitAux[strlitAuxSize] = yytext[0];
  141. strlitAuxSize++;
  142. semicolon_flag=1;}
  143.  
  144. <STRLITState>{escape} {column+=yyleng;
  145. strlitAux = realloc(strlitAux,strlitAuxSize+2);
  146. strlitAux[strlitAuxSize] = yytext[0];
  147. strlitAux[strlitAuxSize+1] = yytext[1];
  148. strlitAuxSize+=2;}
  149.  
  150. <STRLITState>((\\.)|(\\)) {printf("Line %d, column %d: invalid escape sequence (%s)\n", line, column, yytext);
  151. column+=yyleng;
  152. strlit_flag=0;
  153. semicolon_flag=0;
  154. memset(strlitAux,0,strlitAuxSize);
  155. strlitAuxSize = 0;}
  156.  
  157. <STRLITState><<EOF>> {column+=yyleng;
  158. printf("Line %d, column %d: unterminated string literal\n", aux_line, aux_column);
  159. semicolon_flag = 0;
  160. memset(strlitAux,0,strlitAuxSize);
  161. strlitAuxSize = 0;
  162. BEGIN 0;}
  163.  
  164. <STRLITState>"\"" {column+=yyleng;
  165. strlitAux = realloc(strlitAux,strlitAuxSize+1);
  166. strlitAux[strlitAuxSize] = yytext[0];
  167. strlitAuxSize++;
  168. if(strlit_flag==1 && flag == 1){
  169. printf("STRLIT(");
  170. for(int i = 0;i < strlitAuxSize; i++){
  171. printf("%c", strlitAux[i]);
  172. }
  173. printf(")\n");
  174. }
  175. memset(strlitAux,0,strlitAuxSize);
  176. strlitAuxSize = 0;
  177. BEGIN 0;}
  178.  
  179. <STRLITState>{newline} {line++;column=1;
  180. printf("Line %d, column %d: unterminated string literal\n", aux_line, aux_column);
  181. semicolon_flag = 0;
  182. memset(strlitAux,0,strlitAuxSize);
  183. strlitAuxSize = 0;
  184. BEGIN 0;}
  185.  
  186. <STRLITState>. {column+=yyleng;
  187. //printf("teste = %c\n",yytext[0]);
  188. strlitAux = realloc(strlitAux,strlitAuxSize+1);
  189. strlitAux[strlitAuxSize] = yytext[0];
  190. strlitAuxSize++;}
  191.  
  192. . {printf("Line %d, column %d: illegal character (%s)\n", line, column, yytext); column+=yyleng;}
  193. <<eof>> {if (semicolon_flag == 1 && flag == 1)printf("SEMICOLON\n");semicolon_flag=0; return 0;}
  194.  
  195.  
  196. %%
  197.  
  198. void yyerror (const char *s)
  199. {
  200. printf("Line %d, column %d: %s: %s\n",line,column,s,yytext);
  201. }
  202.  
  203. int main(int argc, char **argv) {
  204. if (argc > 1) {
  205. if (argv[1][0]=='-' && argv[1][1]=='l'){
  206. flag=1;
  207. yylex();
  208. }
  209. }
  210. else {
  211. yylex();
  212. }
  213. //yyparse();
  214. return 0;
  215. }
  216.  
  217. int yywrap() {
  218. return 1;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement