Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Ana Catarina Mestre dos Santos Silva: 2016240765
- //Maria João Gonçalves Pereira: 2016222017
- %X COMMENTFIRST
- %X COMMENT
- %X SRLIT
- %{
- #include "y.tab.h"
- #include "ast.h"
- int line = 1, col = 1;
- int start_line = 1, start_col = 1;
- int flag = 0, flag_tree = 0, flag_s = 0, flag_error = 0;
- extern int flag_error_c;
- int flagSemicolon = 0;
- char aux[1000];
- char aux_error[1000];
- int invalid = 0, str = 0, nl = 0, tab = 0, eof = 0;
- int token_line, token_col;
- extern node* root;
- %}
- letter [a-zA-Z_]
- decimal [0-9]
- oct [0][0-9]
- hex [0][x|X][0-9a-fA-F]+
- 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"
- seq_escape \\f|\\n|\\r|\\t|\\\\|\\\"
- exponent ("e"|"E")("+"|"-")?{decimal}+
- float_aux {decimal}*"."{decimal}+{exponent}?|{decimal}+"."{decimal}*{exponent}?
- float {float_aux}|"."{decimal}+{exponent}?|{decimal}*{exponent}
- nl \n|\r|\r\n
- %%
- "/*" {BEGIN COMMENTFIRST; start_line = line; start_col = col; col+=yyleng;}
- <COMMENTFIRST>. {col+=yyleng;}
- <COMMENTFIRST>{nl} {line++; col=1;}
- <COMMENTFIRST><<EOF>> {printf("Line %d, column %d: unterminated comment\n", start_line, start_col); yyterminate();}
- <COMMENTFIRST>"*/" {BEGIN 0; col+=yyleng;}
- "//" {BEGIN COMMENT; start_line = line; start_col = col; col+=yyleng;}
- <COMMENT>. {col+=yyleng;}
- <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;}
- \" {BEGIN SRLIT; strcat(aux, yytext); start_line = line; start_col = col; col+=yyleng; token_line=line; token_col=col;}
- <SRLIT>{seq_escape} {strcat(aux, yytext); col+=yyleng;}
- <SRLIT>(\\.)|(\\) {strcpy(aux, "\0"); printf("Line %d, column %d: invalid escape sequence (%s)\n", line, col, yytext); col+=yyleng; invalid=1; flag_error = 1;}
- <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;}
- <SRLIT><<EOF>> {strcpy(aux, "\0"); printf("Line %d, column %d: unterminated string literal\n", start_line, start_col); flag_error = 1; yyterminate();}
- <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;}
- <SRLIT>. {strcat(aux, yytext); col+=yyleng;}
- ";" {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;}
- "_" {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;}
- "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;}
- "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;}}
- "&&" {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;}}
- "*" {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;}}
- "=" {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;}}
- "," {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; }
- "/" {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;}}
- "==" {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;}}
- ">" {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;}}
- ">=" {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;}}
- "{" {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; }
- "<=" {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;}}
- "(" {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; }
- "[" {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; }
- "<" {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;}}
- "-" {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;}}
- "%" {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;}}
- "!=" {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;}}
- "!" {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;}}
- "||" {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;}}
- "+" {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;}}
- "}" {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; }
- ")" {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; }
- "]" {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; }
- "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; }
- "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;}}
- "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;}}
- "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; }
- "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; }
- "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; }
- "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; }
- "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; }
- "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;}}
- "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;}}
- "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; }
- "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;}}
- {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;}
- {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;}}
- ({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;}}
- {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;}}
- " "|\t {col+=yyleng; str=0; nl=0; tab=1; token_line=line; token_col=col;}
- {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;}
- . {printf("Line %d, column %d: illegal character (%s)\n", line, col, yytext); col+=yyleng; flag_error = 1;}
- <<EOF>> {if(flagSemicolon && flag) printf("SEMICOLON\n"); eof=1; col+=yyleng; if(flag_tree && flagSemicolon){flagSemicolon=0; return SEMICOLON;} flagSemicolon=0; return 0;}
- %%
- int main(int argc, char* argv[]) {
- if(argc==2){
- if(strcmp(argv[1],"-l") == 0){
- flag = 1;
- yylex();
- }
- else if(strcmp(argv[1],"-t") == 0){
- flag_tree = 1;
- yyparse();
- }
- else if(strcmp(argv[1],"-s") == 0){
- flag_s = 1;
- flag_tree = 1;
- yyparse();
- }
- }
- else if(argc==3){
- if((strcmp(argv[1],"-l") == 0) || (strcmp(argv[2],"-l") == 0)){
- flag = 1;
- yylex();
- }
- if((strcmp(argv[1],"-t") == 0) || (strcmp(argv[2],"-t") == 0)){
- flag_tree = 1;
- yyparse();
- }
- if((strcmp(argv[1],"-s") == 0) || (strcmp(argv[2],"-s") == 0)){
- flag_s = 1;
- flag_tree = 1;
- yyparse();
- }
- }
- else if(argc==4){
- if((strcmp(argv[1],"-l") == 0) || (strcmp(argv[2],"-l") == 0) || (strcmp(argv[3],"-l") == 0)){
- flag = 1;
- yylex();
- }
- if((strcmp(argv[1],"-t") == 0) || (strcmp(argv[2],"-t") == 0) || (strcmp(argv[3],"-t") == 0)){
- flag_tree = 1;
- yyparse();
- }
- if((strcmp(argv[1],"-s") == 0) || (strcmp(argv[2],"-s") == 0) || (strcmp(argv[3],"-s") == 0)){
- flag_s = 1;
- flag_tree = 1;
- yyparse();
- }
- }
- else{
- flag_s = 1;
- flag_tree = 1;
- yyparse();
- }
- if(flag_s && !flag_error){
- create_table(root);
- if(!flag_error_c){
- print_global_table();
- print_local_table();
- print_ast_annotated(root, 0);
- }
- }
- if(flag_tree && !flag_error && !flag_s){
- print_ast(root, 0);
- }
- clear_ast(root);
- return 0;
- }
- void yyerror(char *s){
- flag_error = 1;
- if(str){
- str = 0;
- printf("Line %d, column %d: %s: %s\n", token_line, (int)(token_col-(int)yyleng), s, aux_error);
- }
- else if(nl && !tab && eof){
- nl = 0;
- printf("Line %d, column %d: %s: %s\n", token_line-1, 1, s, yytext);
- }
- else if((tab && eof)|| (!eof && nl)){
- tab = 0;
- printf("Line %d, column %d: %s: %s\n", token_line-1, token_col, s, yytext);
- }
- else{
- printf("Line %d, column %d: %s: %s\n", token_line, (int)(token_col-(int)yyleng), s, yytext);
- }
- }
- int yywrap() {
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement