Advertisement
Guest User

Untitled

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