Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.52 KB | None | 0 0
  1. %X COMMENTFIRST
  2. %X COMMENT
  3. %X SRLIT
  4.  
  5. %{
  6. #include "y.tab.h"
  7. #include "ast.h"
  8.  
  9. int line = 1;
  10. int col = 1;
  11. int start_line = 1;
  12. int start_col = 1;
  13.  
  14. int flag = 0;
  15. int flag_tree = 0;
  16. int flag_error = 0;
  17. int flagSemicolon = 0;
  18. int flag_aux = 0;
  19.  
  20. char aux[1000];
  21. char aux_error[1000];
  22.  
  23. int invalid = 0;
  24. int eof = 0;
  25. int str = 0;
  26. int begin_str, token_line, token_col;
  27.  
  28. extern node_type* root;
  29. %}
  30.  
  31. letter [a-zA-Z_]
  32. decimal [0-9]
  33. oct [0][0-9]
  34. hex [0][x|X][0-9a-fA-F]+
  35. reserved "++"|"--"|"break"|"default"|"func"|"interface"|"select"|"case"|"defer"|"go"|"map"|"struct"|"chan"|"else"|"goto"|"package"|"switch"|"const"|"fallthrough"|"if"|"range"|"type"|"continue"|"for"|"import"|"return"|"var"
  36. seq_escape \\(f|n|r|t|\\|\")
  37. exponent ("e"|"E")("+"|"-")?{decimal}+
  38. float_aux {decimal}*"."{decimal}+{exponent}?|{decimal}+"."{decimal}*{exponent}?
  39. float {float_aux}|"."{decimal}+{exponent}?|{decimal}*{exponent}
  40. nl \n|\r|\r\n
  41.  
  42. %%
  43. "/*" {BEGIN COMMENTFIRST; start_line = line; start_col = col; col+=yyleng;}
  44. <COMMENTFIRST>. {col+=yyleng;}
  45. <COMMENTFIRST>{nl} {line++; col=1;}
  46. <COMMENTFIRST><<EOF>> {printf("Line %d, column %d: unterminated comment\n", start_line, start_col); yyterminate();}
  47. <COMMENTFIRST>"*/" {BEGIN 0; col+=yyleng;}
  48.  
  49. "//" {BEGIN COMMENT; start_line = line; start_col = col; col+=yyleng;}
  50. <COMMENT>. {col+=yyleng;}
  51. <COMMENT>{nl} {BEGIN 0; line++; col=1; if(flagSemicolon && flag) printf("SEMICOLON\n"); str=0; if(flag_aux && flagSemicolon){flagSemicolon=0; return SEMICOLON;} flagSemicolon=0;}
  52.  
  53. \" {BEGIN SRLIT; strcat(aux, yytext); start_line = line; start_col = col; col+=yyleng; begin_str = col;}
  54. <SRLIT>{seq_escape} {strcat(aux, yytext); col+=yyleng;}
  55. <SRLIT>(\\.)|(\\) {strcpy(aux, "\0"); printf("Line %d, column %d: invalid escape sequence (%s)\n", line, col, yytext); col+=yyleng; invalid=1;}
  56. <SRLIT>{nl} {strcpy(aux, "\0"); printf("Line %d, column %d: unterminated string literal\n", start_line, start_col); line++; col=1; BEGIN 0;}
  57. <SRLIT><<EOF>> {strcpy(aux, "\0"); printf("Line %d, column %d: unterminated string literal\n", start_line, start_col); yyterminate();}
  58. <SRLIT>"\"" {BEGIN 0; strcat(aux, yytext); strcpy(aux_error, aux); str=1; if(!invalid) flagSemicolon=1; if(flag && !invalid) printf("STRLIT(%s)\n", aux); col+=yyleng; strcpy(aux, "\0"); yylval.token = strdup(yytext); if(flag_aux & !invalid) return STRLIT; invalid=0;}
  59. <SRLIT>. {strcat(aux, yytext); col+=yyleng;}
  60.  
  61.  
  62. ";" {if(flag) printf("SEMICOLON\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return SEMICOLON;}
  63. "_" {if(flag) printf("BLANKID\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return BLANKID;}
  64. "package" {if(flag) printf("PACKAGE\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return PACKAGE;}
  65. "return" {if(flag) printf("RETURN\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; str=0; if(flag_aux) return RETURN;}
  66. "&&" {if(flag) printf("AND\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return AND;}
  67. "*" {if(flag) printf("STAR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return STAR; }
  68. "=" {if(flag) printf("ASSIGN\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return ASSIGN; }
  69. "," {if(flag) printf("COMMA\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return COMMA; }
  70. "/" {if(flag) printf("DIV\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return DIV; }
  71. "==" {if(flag) printf("EQ\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return EQ; }
  72. ">" {if(flag) printf("GT\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return GT; }
  73. ">=" {if(flag) printf("GE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return GE; }
  74. "{" {if(flag) printf("LBRACE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return LBRACE; }
  75. "<=" {if(flag) printf("LE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return LE; }
  76. "(" {if(flag) printf("LPAR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return LPAR; }
  77. "[" {if(flag) printf("LSQ\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return LSQ; }
  78. "<" {if(flag) printf("LT\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return LT; }
  79. "-" {if(flag) printf("MINUS\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return MINUS; }
  80. "%" {if(flag) printf("MOD\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return MOD; }
  81. "!=" {if(flag) printf("NE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return NE; }
  82. "!" {if(flag) printf("NOT\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return NOT; }
  83. "||" {if(flag) printf("OR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return OR; }
  84. "+" {if(flag) printf("PLUS\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return PLUS; }
  85. "}" {if(flag) printf("RBRACE\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; str=0; if(flag_aux) return RBRACE; }
  86. ")" {if(flag) printf("RPAR\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; str=0; if(flag_aux) return RPAR; }
  87. "]" {if(flag) printf("RSQ\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; str=0; if(flag_aux) return RSQ; }
  88. "else" {if(flag) printf("ELSE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return ELSE; }
  89. "for" {if(flag) printf("FOR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return FOR; }
  90. "if" {if(flag) printf("IF\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return IF; }
  91. "var" {if(flag) printf("VAR\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return VAR; }
  92. "int" {if(flag) printf("INT\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return INT; }
  93. "float32" {if(flag) printf("FLOAT32\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return FLOAT32; }
  94. "bool" {if(flag) printf("BOOL\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return BOOL; }
  95. "string" {if(flag) printf("STRING\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return STRING; }
  96. "fmt.Println" {if(flag) printf("PRINT\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return PRINT; }
  97. "strconv.Atoi" {if(flag) printf("PARSEINT\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return PARSEINT; }
  98. "func" {if(flag) printf("FUNC\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return FUNC; }
  99. "os.Args" {if(flag) printf("CMDARGS\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; if(flag_aux) return CMDARGS; }
  100.  
  101.  
  102. {reserved} {if(flag) printf("RESERVED(%s)\n", yytext); col+=yyleng; flagSemicolon=0; yylval.token = strdup(yytext); str=0; if(flag_aux) return RESERVED;}
  103. {letter}({letter}|{decimal})* {if(flag) printf("ID(%s)\n", yytext); col+=yyleng;flagSemicolon=1; yylval.token = strdup(yytext); str=0; if(flag_aux) return ID;}
  104. ({oct}|{hex}|{decimal})* {if(flag) printf("INTLIT(%s)\n", yytext); col+=yyleng; flagSemicolon=1; yylval.token = strdup(yytext); str=0; if(flag_aux) return INTLIT;}
  105. {float} {if(flag) printf("REALLIT(%s)\n", yytext); col+=yyleng; flagSemicolon=1; yylval.token = strdup(yytext); str=0; if(flag_aux) return REALLIT;}
  106.  
  107.  
  108. " "|\t {col+=yyleng;}
  109. {nl} {if(flagSemicolon && flag) printf("SEMICOLON\n"); line++; col=1; str=0; if(flag_aux && flagSemicolon){flagSemicolon=0; return SEMICOLON;} flagSemicolon=0;}
  110. . {printf("Line %d, column %d: illegal character (%s)\n", line, col, yytext); col+=yyleng;}
  111. <<EOF>> {if(flagSemicolon && flag) printf("SEMICOLON\n"); col+=yyleng; eof=1; str=0; if(flag_aux && flagSemicolon) return SEMICOLON; return 0;}
  112.  
  113. %%
  114. int main(int argc, char* argv[]) {
  115. if(argc>1){
  116. if(strcmp(argv[1],"-l") == 0){
  117. flag = 1;
  118. yylex();
  119. }
  120. else if(strcmp(argv[1],"-t") == 0){
  121. flag_tree = 1;
  122. flag_aux = 1;
  123. yyparse();
  124. }
  125.  
  126.  
  127. }
  128. else{
  129. flag_aux = 1;
  130. yyparse();
  131. }
  132.  
  133. //if(flag_tree && flag_aux_error)
  134. //print tree
  135.  
  136. //clear tree
  137.  
  138. return 0;
  139. }
  140.  
  141. void yyerror(char *s){
  142. flag_error = 1;
  143. if(str){
  144. str = 0;
  145. printf("Line %d, column %d: %s: %s\n", line, (int)(begin_str-(int)yyleng), s, aux_error);
  146. }
  147. else if(eof){
  148. eof = 0;
  149. printf("Line %d, column %d: %s: %s\n", token_line, (int)(token_col-(int)yyleng), s, yytext);
  150. }
  151. else
  152. printf("Line %d, column %d: %s: %s\n", line, (int)(col-(int)yyleng), s, yytext);
  153. }
  154.  
  155. int yywrap() {
  156. return 1;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement