Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.09 KB | None | 0 0
  1. //Ana Catarina Mestre dos Santos Silva: 2016240765
  2. //Maria João Gonçalves Pereira: 2016222017
  3.  
  4. %X COMMENTFIRST
  5. %X COMMENT
  6. %X SRLIT
  7.  
  8. %{
  9. #include "y.tab.h"
  10. #include "ast.h"
  11.  
  12. int line = 1, col = 1;
  13. int start_line = 1, start_col = 1;
  14.  
  15. int flag = 0, flag_tree = 0, flag_s = 0, flag_error = 0;
  16. extern int flag_error_c;
  17. int flagSemicolon = 0;
  18.  
  19. char aux[1000];
  20. char aux_error[1000];
  21.  
  22. int invalid = 0, str = 0, nl = 0, tab = 0, eof = 0;
  23. int token_line, token_col;
  24.  
  25. extern node* root;
  26. %}
  27.  
  28. letter [a-zA-Z_]
  29. decimal [0-9]
  30. oct [0][0-9]
  31. hex [0][x|X][0-9a-fA-F]+
  32. 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"
  33. seq_escape \\f|\\n|\\r|\\t|\\\\|\\\"
  34. exponent ("e"|"E")("+"|"-")?{decimal}+
  35. float_aux {decimal}*"."{decimal}+{exponent}?|{decimal}+"."{decimal}*{exponent}?
  36. float {float_aux}|"."{decimal}+{exponent}?|{decimal}*{exponent}
  37. nl \n|\r|\r\n
  38.  
  39. %%
  40. "/*" {BEGIN COMMENTFIRST; start_line = line; start_col = col; col+=yyleng;}
  41. <COMMENTFIRST>. {col+=yyleng;}
  42. <COMMENTFIRST>{nl} {line++; col=1;}
  43. <COMMENTFIRST><<EOF>> {printf("Line %d, column %d: unterminated comment\n", start_line, start_col); yyterminate();}
  44. <COMMENTFIRST>"*/" {BEGIN 0; col+=yyleng;}
  45.  
  46. "//" {BEGIN COMMENT; start_line = line; start_col = col; col+=yyleng;}
  47. <COMMENT>. {col+=yyleng;}
  48. <COMMENT>{nl} {BEGIN 0; line++; col=1; if(flagSemicolon && flag) printf("SEMICOLON\n"); str=0; if(flag_tree && flagSemicolon){flagSemicolon=0; return SEMICOLON;} flagSemicolon=0;}
  49.  
  50. \" {BEGIN SRLIT; strcat(aux, yytext); start_line = line; start_col = col; col+=yyleng; token_line=line; token_col=col;}
  51. <SRLIT>{seq_escape} {strcat(aux, yytext); col+=yyleng;}
  52. <SRLIT>(\\.)|(\\) {strcpy(aux, "\0"); printf("Line %d, column %d: invalid escape sequence (%s)\n", line, col, yytext); col+=yyleng; invalid=1; flag_error = 1;}
  53. <SRLIT>{nl} {strcpy(aux, "\0"); printf("Line %d, column %d: unterminated string literal\n", start_line, start_col); invalid=0; line++; col=1; flag_error = 1; BEGIN 0;}
  54. <SRLIT><<EOF>> {strcpy(aux, "\0"); printf("Line %d, column %d: unterminated string literal\n", start_line, start_col); flag_error = 1; yyterminate();}
  55. <SRLIT>"\"" {BEGIN 0; strcat(aux, yytext); strcpy(aux_error, aux); str=1; nl=0; tab=1; if(!invalid) flagSemicolon=1; if(flag && !invalid) printf("STRLIT(%s)\n", aux); if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(aux)); return STRLIT;}; col+=yyleng; strcpy(aux, "\0"); invalid=0;}
  56. <SRLIT>. {strcat(aux, yytext); col+=yyleng;}
  57.  
  58.  
  59. ";" {if(flag) printf("SEMICOLON\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return SEMICOLON;}
  60. "_" {if(flag) printf("BLANKID\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return BLANKID;}
  61. "package" {if(flag) printf("PACKAGE\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return PACKAGE;}
  62. "return" {if(flag) printf("RETURN\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return RETURN;}}
  63. "&&" {if(flag) printf("AND\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return AND;}}
  64. "*" {if(flag) printf("STAR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return STAR;}}
  65. "=" {if(flag) printf("ASSIGN\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return ASSIGN;}}
  66. "," {if(flag) printf("COMMA\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return COMMA; }
  67. "/" {if(flag) printf("DIV\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return DIV;}}
  68. "==" {if(flag) printf("EQ\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return EQ;}}
  69. ">" {if(flag) printf("GT\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return GT;}}
  70. ">=" {if(flag) printf("GE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return GE;}}
  71. "{" {if(flag) printf("LBRACE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return LBRACE; }
  72. "<=" {if(flag) printf("LE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return LE;}}
  73. "(" {if(flag) printf("LPAR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return LPAR; }
  74. "[" {if(flag) printf("LSQ\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return LSQ; }
  75. "<" {if(flag) printf("LT\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return LT;}}
  76. "-" {if(flag) printf("MINUS\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return MINUS;}}
  77. "%" {if(flag) printf("MOD\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return MOD;}}
  78. "!=" {if(flag) printf("NE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return NE;}}
  79. "!" {if(flag) printf("NOT\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return NOT;}}
  80. "||" {if(flag) printf("OR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return OR;}}
  81. "+" {if(flag) printf("PLUS\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return PLUS;}}
  82. "}" {if(flag) printf("RBRACE\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return RBRACE; }
  83. ")" {if(flag) printf("RPAR\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return RPAR; }
  84. "]" {if(flag) printf("RSQ\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return RSQ; }
  85. "else" {if(flag) printf("ELSE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return ELSE; }
  86. "for" {if(flag) printf("FOR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1;if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return FOR;}}
  87. "if" {if(flag) printf("IF\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return IF;}}
  88. "var" {if(flag) printf("VAR\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return VAR; }
  89. "int" {if(flag) printf("INT\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return INT; }
  90. "float32" {if(flag) printf("FLOAT32\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return FLOAT32; }
  91. "bool" {if(flag) printf("BOOL\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return BOOL; }
  92. "string" {if(flag) printf("STRING\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return STRING; }
  93. "fmt.Println" {if(flag) printf("PRINT\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return PRINT;}}
  94. "strconv.Atoi" {if(flag) printf("PARSEINT\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return PARSEINT;}}
  95. "func" {if(flag) printf("FUNC\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) return FUNC; }
  96. "os.Args" {if(flag) printf("CMDARGS\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; str=0; nl=0; tab=1; if(flag_tree) if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return CMDARGS;}}
  97.  
  98.  
  99. {reserved} {if(flag) printf("RESERVED(%s)\n", yytext); col+=yyleng; flagSemicolon=0; str=0; nl=0; tab=1; token_line=line; token_col=col; if(flag_tree) return RESERVED;}
  100. {letter}({letter}|{decimal})* {if(flag) printf("ID(%s)\n", yytext); col+=yyleng;flagSemicolon=1; str=0; nl=0; tab=1; token_line=line; token_col=col; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return ID;}}
  101. ({oct}|{hex}|{decimal})* {if(flag) printf("INTLIT(%s)\n", yytext); col+=yyleng; flagSemicolon=1; str=0; nl=0; tab=1; token_line=line; token_col=col; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return INTLIT;}}
  102. {float} {if(flag) printf("REALLIT(%s)\n", yytext); col+=yyleng; flagSemicolon=1; str=0; nl=0; tab=1; token_line=line; token_col=col; if(flag_tree){yylval.name = create_token(yytext, line, col-strlen(yytext)); return REALLIT;}}
  103.  
  104.  
  105. " "|\t {col+=yyleng; str=0; nl=0; tab=1; token_line=line; token_col=col;}
  106. {nl} {if(flagSemicolon && flag) printf("SEMICOLON\n"); nl = 1; line++; col=1; str=0; tab=0; token_line=line; if(flag_tree && flagSemicolon){flagSemicolon=0; return SEMICOLON;} flagSemicolon=0;}
  107. . {printf("Line %d, column %d: illegal character (%s)\n", line, col, yytext); col+=yyleng; flag_error = 1;}
  108. <<EOF>> {if(flagSemicolon && flag) printf("SEMICOLON\n"); eof=1; col+=yyleng; if(flag_tree && flagSemicolon){flagSemicolon=0; return SEMICOLON;} flagSemicolon=0; return 0;}
  109.  
  110. %%
  111. int main(int argc, char* argv[]) {
  112. if(argc==2){
  113. if(strcmp(argv[1],"-l") == 0){
  114. flag = 1;
  115. yylex();
  116. }
  117. else if(strcmp(argv[1],"-t") == 0){
  118. flag_tree = 1;
  119. yyparse();
  120. }
  121. else if(strcmp(argv[1],"-s") == 0){
  122. flag_s = 1;
  123. flag_tree = 1;
  124. yyparse();
  125. }
  126. }
  127. else if(argc==3){
  128. if((strcmp(argv[1],"-l") == 0) || (strcmp(argv[2],"-l") == 0)){
  129. flag = 1;
  130. yylex();
  131. }
  132. if((strcmp(argv[1],"-t") == 0) || (strcmp(argv[2],"-t") == 0)){
  133. flag_tree = 1;
  134. yyparse();
  135. }
  136. if((strcmp(argv[1],"-s") == 0) || (strcmp(argv[2],"-s") == 0)){
  137. flag_s = 1;
  138. flag_tree = 1;
  139. yyparse();
  140. }
  141. }
  142. else if(argc==4){
  143. if((strcmp(argv[1],"-l") == 0) || (strcmp(argv[2],"-l") == 0) || (strcmp(argv[3],"-l") == 0)){
  144. flag = 1;
  145. yylex();
  146. }
  147. if((strcmp(argv[1],"-t") == 0) || (strcmp(argv[2],"-t") == 0) || (strcmp(argv[3],"-t") == 0)){
  148. flag_tree = 1;
  149. yyparse();
  150. }
  151. if((strcmp(argv[1],"-s") == 0) || (strcmp(argv[2],"-s") == 0) || (strcmp(argv[3],"-s") == 0)){
  152. flag_s = 1;
  153. flag_tree = 1;
  154. yyparse();
  155.  
  156. }
  157. }
  158. else{
  159. flag_s = 1;
  160. flag_tree = 1;
  161. yyparse();
  162. }
  163.  
  164. if(flag_s && !flag_error){
  165. create_table(root);
  166. if(!flag_error_c){
  167. print_global_table();
  168. print_local_table();
  169. print_ast_annotated(root, 0);
  170. }
  171. }
  172.  
  173. if(flag_tree && !flag_error && !flag_s){
  174. print_ast(root, 0);
  175. }
  176.  
  177. clear_ast(root);
  178.  
  179. return 0;
  180. }
  181.  
  182. void yyerror(char *s){
  183. flag_error = 1;
  184. if(str){
  185. str = 0;
  186. printf("Line %d, column %d: %s: %s\n", token_line, (int)(token_col-(int)yyleng), s, aux_error);
  187. }
  188. else if(nl && !tab && eof){
  189. nl = 0;
  190. printf("Line %d, column %d: %s: %s\n", token_line-1, 1, s, yytext);
  191. }
  192. else if((tab && eof)|| (!eof && nl)){
  193. tab = 0;
  194. printf("Line %d, column %d: %s: %s\n", token_line-1, token_col, s, yytext);
  195. }
  196. else{
  197. printf("Line %d, column %d: %s: %s\n", token_line, (int)(token_col-(int)yyleng), s, yytext);
  198. }
  199. }
  200.  
  201. int yywrap() {
  202. return 1;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement