Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %X COMMENTFIRST
- %X COMMENT
- %X SRLIT
- %{
- #include "y.tab.h"
- #include "ast.h"
- int line = 1;
- int col = 1;
- int start_line = 1;
- int start_col = 1;
- int flag = 0;
- int flag_tree = 0;
- int flag_error = 0;
- int flagSemicolon = 0;
- char aux[1000];
- char aux_error[1000];
- int invalid = 0;
- int eof = 0;
- int str = 0;
- int begin_str, token_line, token_col;
- extern node_type* 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"); if(!flag && flagSemicolon){flagSemicolon=0; return SEMICOLON;} flagSemicolon=0;}
- \" {BEGIN SRLIT; strcat(aux, yytext); start_line = line; start_col = col; col+=yyleng; begin_str = 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;}
- <SRLIT>{nl} {strcpy(aux, "\0"); printf("Line %d, column %d: unterminated string literal\n", start_line, start_col); line++; col=1; BEGIN 0;}
- <SRLIT><<EOF>> {strcpy(aux, "\0"); printf("Line %d, column %d: unterminated string literal\n", start_line, start_col); yyterminate();}
- <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;}
- <SRLIT>. {strcat(aux, yytext); col+=yyleng;}
- ";" {if(flag) printf("SEMICOLON\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return SEMICOLON;}
- "_" {if(flag) printf("BLANKID\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return BLANKID;}
- "package" {if(flag) printf("PACKAGE\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return PACKAGE;}
- "return" {if(flag) printf("RETURN\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; if(!flag) return RETURN;}
- "&&" {if(flag) printf("AND\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return AND;}
- "*" {if(flag) printf("STAR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return STAR; }
- "=" {if(flag) printf("ASSIGN\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return ASSIGN; }
- "," {if(flag) printf("COMMA\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return COMMA; }
- "/" {if(flag) printf("DIV\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return DIV; }
- "==" {if(flag) printf("EQ\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return EQ; }
- ">" {if(flag) printf("GT\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return GT; }
- ">=" {if(flag) printf("GE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return GE; }
- "{" {if(flag) printf("LBRACE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return LBRACE; }
- "<=" {if(flag) printf("LE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return LE; }
- "(" {if(flag) printf("LPAR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return LPAR; }
- "[" {if(flag) printf("LSQ\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return LSQ; }
- "<" {if(flag) printf("LT\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return LT; }
- "-" {if(flag) printf("MINUS\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return MINUS; }
- "%" {if(flag) printf("MOD\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return MOD; }
- "!=" {if(flag) printf("NE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return NE; }
- "!" {if(flag) printf("NOT\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return NOT; }
- "||" {if(flag) printf("OR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return OR; }
- "+" {if(flag) printf("PLUS\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return PLUS; }
- "}" {if(flag) printf("RBRACE\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; if(!flag) return RBRACE; }
- ")" {if(flag) printf("RPAR\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; if(!flag) return RPAR; }
- "]" {if(flag) printf("RSQ\n"); col+=yyleng; flagSemicolon=1; token_line=line; token_col=col; if(!flag) return RSQ; }
- "else" {if(flag) printf("ELSE\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return ELSE; }
- "for" {if(flag) printf("FOR\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return FOR; }
- "if" {if(flag) printf("IF\n"); col+=yyleng;flagSemicolon=0; token_line=line; token_col=col; if(!flag) return IF; }
- "var" {if(flag) printf("VAR\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return VAR; }
- "int" {if(flag) printf("INT\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return INT; }
- "float32" {if(flag) printf("FLOAT32\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return FLOAT32; }
- "bool" {if(flag) printf("BOOL\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return BOOL; }
- "string" {if(flag) printf("STRING\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return STRING; }
- "fmt.Println" {if(flag) printf("PRINT\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return PRINT; }
- "strconv.Atoi" {if(flag) printf("PARSEINT\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return PARSEINT; }
- "func" {if(flag) printf("FUNC\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return FUNC; }
- "os.Args" {if(flag) printf("CMDARGS\n"); col+=yyleng; flagSemicolon=0; token_line=line; token_col=col; if(!flag) return CMDARGS; }
- {reserved} {if(flag) printf("RESERVED(%s)\n", yytext); col+=yyleng; flagSemicolon=0; yylval.token = strdup(yytext); if(!flag) return RESERVED;}
- {letter}({letter}|{decimal})* {if(flag) printf("ID(%s)\n", yytext); col+=yyleng;flagSemicolon=1; yylval.token = strdup(yytext); if(!flag) return ID;}
- ({oct}|{hex}|{decimal})* {if(flag) printf("INTLIT(%s)\n", yytext); col+=yyleng; flagSemicolon=1; yylval.token = strdup(yytext); if(!flag) return INTLIT;}
- {float} {if(flag) printf("REALLIT(%s)\n", yytext); col+=yyleng; flagSemicolon=1; yylval.token = strdup(yytext); if(!flag) return REALLIT;}
- " "|\t {col+=yyleng;}
- {nl} {if(flagSemicolon && flag) printf("SEMICOLON\n"); line++; col=1; if(!flag && flagSemicolon){flagSemicolon=0; return SEMICOLON;} flagSemicolon=0;}
- . {printf("Line %d, column %d: illegal character (%s)\n", line, col, yytext); col+=yyleng;}
- <<EOF>> {if(flagSemicolon && flag) printf("SEMICOLON\n"); col+=yyleng; eof=1; if(!flag && flagSemicolon) return SEMICOLON; return 0;}
- %%
- int main(int argc, char* argv[]) {
- if(argc>1){
- if(strcmp(argv[1],"-l") == 0)
- flag = 1;
- if(strcmp(argv[1],"-t") == 0)
- flag_tree = 1;
- }
- if(flag){
- yylex();
- }
- else{
- yyparse();
- //if(flag_tree && !flag_error)
- //print tree
- //clear tree
- }
- return 0;
- }
- void yyerror(char *s){
- flag_error = 1;
- if(str){
- str = 0;
- printf("Line %d, column %d: %s: %s\n", line, (int)(begin_str-(int)yyleng), s, aux_error);
- }
- else if(eof){
- eof = 0;
- printf("Line %d, column %d: %s: %s\n", token_line, (int)(token_col-(int)yyleng+1), s, yytext);
- }
- else
- printf("Line %d, column %d: %s: %s\n", line, (int)(col-(int)yyleng), s, yytext);
- }
- int yywrap() {
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement