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];
- int invalid = 0;
- //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) printf("SEMICOLON\n"); flagSemicolon=0;}
- \" {BEGIN SRLIT; strcat(aux, yytext); start_line = line; start_col = col; col+=yyleng;}
- <SRLIT>[^\n\r\"] {strcat(aux, yytext); col+=yyleng;}
- <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>"\"" {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;}
- ";" {if(flag) printf("SEMICOLON\n"); col+=yyleng;flagSemicolon=0; if(!flag) return SEMICOLON;}
- "_" {if(flag) printf("BLANKID\n"); col+=yyleng;flagSemicolon=0; if(!flag) return BLANKID;}
- "package" {if(flag) printf("PACKAGE\n"); col+=yyleng;flagSemicolon=0; if(!flag) return PACKAGE;}
- "return" {if(flag) printf("RETURN\n"); col+=yyleng; flagSemicolon=1; if(!flag) return RETURN;}
- "&&" {if(flag) printf("AND\n"); col+=yyleng;flagSemicolon=0; if(!flag) return AND;}
- "*" {if(flag) printf("STAR\n"); col+=yyleng;flagSemicolon=0; if(!flag) return STAR;}
- "=" {if(flag) printf("ASSIGN\n"); col+=yyleng;flagSemicolon=0; if(!flag) return ASSIGN;}
- "," {if(flag) printf("COMMA\n"); col+=yyleng;flagSemicolon=0; if(!flag) return COMMA;}
- "/" {if(flag) printf("DIV\n"); col+=yyleng;flagSemicolon=0; if(!flag) return DIV;}
- "==" {if(flag) printf("EQ\n"); col+=yyleng;flagSemicolon=0; if(!flag) return EQ;}
- ">" {if(flag) printf("GT\n"); col+=yyleng;flagSemicolon=0; if(!flag) return GT;}
- ">=" {if(flag) printf("GE\n"); col+=yyleng;flagSemicolon=0; if(!flag) return GE;}
- "{" {if(flag) printf("LBRACE\n"); col+=yyleng;flagSemicolon=0; if(!flag) return LBRACE;}
- "<=" {if(flag) printf("LE\n"); col+=yyleng;flagSemicolon=0; if(!flag) return LE;}
- "(" {if(flag) printf("LPAR\n"); col+=yyleng;flagSemicolon=0; if(!flag) return LPAR;}
- "[" {if(flag) printf("LSQ\n"); col+=yyleng;flagSemicolon=0; if(!flag) return LSQ;}
- "<" {if(flag) printf("LT\n"); col+=yyleng;flagSemicolon=0; if(!flag) return LT;}
- "-" {if(flag) printf("MINUS\n"); col+=yyleng;flagSemicolon=0; if(!flag) return MINUS;}
- "%" {if(flag) printf("MOD\n"); col+=yyleng;flagSemicolon=0; if(!flag) return MOD;}
- "!=" {if(flag) printf("NE\n"); col+=yyleng;flagSemicolon=0; if(!flag) return NE;}
- "!" {if(flag) printf("NOT\n"); col+=yyleng;flagSemicolon=0; if(!flag) return NOT;}
- "||" {if(flag) printf("OR\n"); col+=yyleng;flagSemicolon=0; if(!flag) return OR;}
- "+" {if(flag) printf("PLUS\n"); col+=yyleng;flagSemicolon=0; if(!flag) return PLUS;}
- "}" {if(flag) printf("RBRACE\n"); col+=yyleng; flagSemicolon=1; if(!flag) return RBRACE;}
- ")" {if(flag) printf("RPAR\n"); col+=yyleng; flagSemicolon=1; if(!flag) return RPAR;}
- "]" {if(flag) printf("RSQ\n"); col+=yyleng; flagSemicolon=1; if(!flag) return RSQ;}
- "else" {if(flag) printf("ELSE\n"); col+=yyleng;flagSemicolon=0; if(!flag) return ELSE;}
- "for" {if(flag) printf("FOR\n"); col+=yyleng;flagSemicolon=0; if(!flag) return FOR;}
- "if" {if(flag) printf("IF\n"); col+=yyleng;flagSemicolon=0; if(!flag) return IF;}
- "var" {if(flag) printf("VAR\n"); col+=yyleng; flagSemicolon=0; if(!flag) return VAR;}
- "int" {if(flag) printf("INT\n"); col+=yyleng; flagSemicolon=0; if(!flag) return INT;}
- "float32" {if(flag) printf("FLOAT32\n"); col+=yyleng; flagSemicolon=0; if(!flag) return FLOAT32;}
- "bool" {if(flag) printf("BOOL\n"); col+=yyleng; flagSemicolon=0; if(!flag) return BOOL;}
- "string" {if(flag) printf("STRING\n"); col+=yyleng; flagSemicolon=0; if(!flag) return STRING;}
- "fmt.Println" {if(flag) printf("PRINT\n"); col+=yyleng; flagSemicolon=0; if(!flag) return PRINT;}
- "strconv.Atoi" {if(flag) printf("PARSEINT\n"); col+=yyleng; flagSemicolon=0; if(!flag) return PARSEINT;}
- "func" {if(flag) printf("FUNC\n"); col+=yyleng; flagSemicolon=0; if(!flag) return FUNC;}
- "os.Args" {if(flag) printf("CMDARGS\n"); col+=yyleng; flagSemicolon=0; if(!flag) return CMDARGS;}
- {reserved} {if(flag) printf("RESERVED(%s)\n", yytext); col+=yyleng; flagSemicolon=0; yyval.token = strdup(yytext); if(!flag) return RESERVED;}
- {letter}({letter}|{decimal})* {if(flag) printf("ID(%s)\n", yytext); col+=yyleng;flagSemicolon=1; yyval.token = strdup(yytext); if(!flag) return ID;}
- ({oct}|{hex}|{decimal})* {if(flag) printf("INTLIT(%s)\n", yytext); col+=yyleng; flagSemicolon=1; yyval.token = strdup(yytext); if(!flag) return INTLIT;}
- {float} {if(flag) printf("REALLIT(%s)\n", yytext); col+=yyleng; flagSemicolon=1; yyval.token = strdup(yytext); if(!flag) return REALLIT;}
- " "|\t {col+=yyleng;}
- {nl} {if(flagSemicolon) printf("SEMICOLON\n"); line++; col=1; flagSemicolon=0;}
- <<EOF>> {if(flagSemicolon) printf("SEMICOLON\n"); yyterminate();}
- . {printf("Line %d, column %d: illegal character (%s)\n", line, col, yytext); col+=yyleng;}
- %%
- 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;
- 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