Advertisement
Guest User

Untitled

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