Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- //A, B, C, D, E, F, G, H, I, J, K, L
- int line=1, column=1, aux_column, aux_line;
- int semicolon_flag = 0;
- int flag;
- int strlit_flag = 1;
- int strlitAuxSize = 0;
- char *strlitAux;
- %}
- INT "int"
- PACKAGE "package"
- FUNC "func"
- RETURN "return"
- VAR "var"
- IF "if"
- ELSE "else"
- FOR "for"
- FLOAT32 "float32"
- BOOL "bool"
- numero [0-9]+
- DIG [0-9]
- ID [a-zA-Z_][a-zA-Z0-9_]*
- INTLIT (0[xX][0-9a-fA-F]+)|(0[0-7]+)|([0-9]+)
- REALLIT (({numero})*\.?({numero})+|({numero})+\.?({numero})*)((e|E)[\-\+]?({numero})+)?
- SEMICOLON ";"
- RPAR ")"
- LPAR "("
- LBRACE "{"
- RBRACE "}"
- RSQ "]"
- LSQ "["
- STAR "*"
- PLUS "+"
- MINUS "-"
- COMMA ","
- BLANKID "_"
- AND "&&"
- ASSIGN "="
- DIV "/"
- EQ "=="
- GE ">="
- GT ">"
- LE "<="
- LT "<"
- MOD "%"
- NE "!="
- OR "||"
- NOT "!"
- STRING "string"
- PRINT "fmt.Println"
- PARSEINT "strconv.Atoi"
- CMDARGS "os.Args"
- RESERVED "++"|"--"|break|case|chan|const|continue|default|defer|fallthrough|go|goto|import|interface|map|range|select|struct|switch|type
- newline "\n"|"\r"|"\r\n"
- separator \t|" "|\v|\f
- escape \\\\|\\n|\\f|\\r|\\t|\\\"
- %X MULTI_COMMENT
- %X SINGLE_COMMENT
- %X STRLITState
- %%
- {separator} {column += yyleng;}
- {SEMICOLON} {if (flag == 1) printf("SEMICOLON\n"); semicolon_flag = 0;}
- {BLANKID} {if (flag == 1) printf("BLANKID\n"); column += yyleng;}
- {PACKAGE} {if (flag == 1) printf("PACKAGE\n"); column += yyleng;}
- {RETURN} {if (flag == 1) printf("RETURN\n");semicolon_flag = 1;}
- {AND} {if (flag == 1) printf("AND\n"); column += yyleng; semicolon_flag = 0;}
- {ASSIGN} {if (flag == 1) printf("ASSIGN\n"); column += yyleng; semicolon_flag = 0;}
- {STAR} {if (flag == 1) printf("STAR\n"); column += yyleng; semicolon_flag = 0;}
- {COMMA} {if (flag == 1) printf("COMMA\n"); column += yyleng; semicolon_flag = 0;}
- {DIV} {if (flag == 1) printf("DIV\n"); column += yyleng; semicolon_flag = 0;}
- {EQ} {if (flag == 1) printf("EQ\n"); column += yyleng; semicolon_flag = 0;}
- {GE} {if (flag == 1) printf("GE\n"); column += yyleng; semicolon_flag = 0;}
- {GT} {if (flag == 1) printf("GT\n"); column += yyleng; semicolon_flag = 0;}
- {LBRACE} {if (flag == 1) printf("LBRACE\n"); semicolon_flag = 0; column += yyleng;}
- {LE} {if (flag == 1) printf("LE\n"); column += yyleng; semicolon_flag = 0;}
- {LPAR} {if (flag == 1) printf("LPAR\n"); semicolon_flag = 0; column += yyleng;}
- {LSQ} {if (flag == 1) printf("LSQ\n"); semicolon_flag = 0; column += yyleng;}
- {LT} {if (flag == 1) printf("LT\n"); column += yyleng; semicolon_flag = 0;}
- {MINUS} {if (flag == 1) printf("MINUS\n"); column += yyleng; semicolon_flag = 0;}
- {MOD} {if (flag == 1) printf("MOD\n"); column += yyleng; semicolon_flag = 0;}
- {NE} {if (flag == 1) printf("NE\n"); column += yyleng; semicolon_flag = 0;}
- {NOT} {if (flag == 1) printf("NOT\n"); column += yyleng; semicolon_flag = 0;}
- {OR} {if (flag == 1) printf("OR\n"); column += yyleng; semicolon_flag = 0;}
- {PLUS} {if (flag == 1) printf("PLUS\n"); column += yyleng; semicolon_flag = 0;}
- {RBRACE} {if (flag == 1) printf("RBRACE\n"); semicolon_flag = 1; column += yyleng;}
- {RPAR} {if (flag == 1) printf("RPAR\n"); semicolon_flag = 1; column += yyleng;}
- {RSQ} {if (flag == 1) printf("RSQ\n"); semicolon_flag = 1; column += yyleng;}
- {ELSE} {if (flag == 1) printf("ELSE\n"); column += yyleng; semicolon_flag = 0;}
- {FOR} {if (flag == 1) printf("FOR\n"); column += yyleng; semicolon_flag = 0;}
- {IF} {if (flag == 1) printf("IF\n"); column += yyleng; semicolon_flag = 0;}
- {VAR} {if (flag == 1) printf("VAR\n"); column += yyleng; semicolon_flag = 0;}
- {INT} {if (flag == 1) printf("INT\n"); column += yyleng; semicolon_flag = 0;}
- {FLOAT32} {if (flag == 1) printf("FLOAT32\n"); column += yyleng; semicolon_flag = 0;}
- {BOOL} {if (flag == 1) printf("BOOL\n"); column += yyleng; semicolon_flag = 0;}
- {STRING} {if (flag == 1) printf("STRING\n"); column += yyleng; semicolon_flag = 0;}
- {PRINT} {if (flag == 1) printf("PRINT\n"); column += yyleng; semicolon_flag = 0;}
- {PARSEINT} {if (flag == 1) printf("PARSEINT\n"); column += yyleng; semicolon_flag = 0;}
- {FUNC} {if (flag == 1) printf("FUNC\n"); column += yyleng; semicolon_flag = 0;}
- {CMDARGS} {if (flag == 1) printf("CMDARGS\n"); column += yyleng; semicolon_flag = 0;}
- {INTLIT} {if (flag == 1) printf("INTLIT(%s)\n",yytext); semicolon_flag = 1; column += yyleng;}
- {REALLIT} {if (flag == 1) printf("REALLIT(%s)\n",yytext); semicolon_flag = 1; column += yyleng;}
- {RESERVED} {if (flag == 1) printf("RESERVED(%s)\n",yytext); column += yyleng; semicolon_flag = 0;}
- {ID} {if (flag == 1) printf("ID(%s)\n",yytext); semicolon_flag = 1; column += yyleng;}
- {newline} {line++; column = 1; if (semicolon_flag == 1 && flag == 1)printf("SEMICOLON\n");semicolon_flag=0;}
- \/\/ {BEGIN SINGLE_COMMENT;
- column+=yyleng; }
- <SINGLE_COMMENT>{newline} {line++;column=1;if(semicolon_flag == 1 && flag == 1) printf("SEMICOLON\n");semicolon_flag = 0;BEGIN 0;}
- <SINGLE_COMMENT>. {column+=yyleng;}
- \/\* {BEGIN MULTI_COMMENT;
- aux_column = column;
- aux_line = line;
- column += yyleng;}
- <MULTI_COMMENT>({newline}) {line += 1;
- column = 1;
- if(semicolon_flag == 1 && flag == 1) printf("SEMICOLON\n");
- semicolon_flag = 0;}
- <MULTI_COMMENT>\*\/ {column+=yyleng;
- BEGIN 0;}
- <MULTI_COMMENT><<EOF>> {printf("Line %d, column %d: unterminated comment\n", aux_line, aux_column);
- column+=yyleng; BEGIN 0;}
- <MULTI_COMMENT>. {column += yyleng;}
- "\"" {BEGIN STRLITState;
- aux_column = column;
- aux_line = line;
- column += yyleng;
- strlit_flag = 1;
- strlitAux = realloc(strlitAux,strlitAuxSize+1);
- strlitAux[strlitAuxSize] = yytext[0];
- strlitAuxSize++;
- semicolon_flag=1;}
- <STRLITState>{escape} {column+=yyleng;
- strlitAux = realloc(strlitAux,strlitAuxSize+2);
- strlitAux[strlitAuxSize] = yytext[0];
- strlitAux[strlitAuxSize+1] = yytext[1];
- strlitAuxSize+=2;}
- <STRLITState>((\\.)|(\\)) {printf("Line %d, column %d: invalid escape sequence (%s)\n", line, column, yytext);
- column+=yyleng;
- strlit_flag=0;
- semicolon_flag=0;
- memset(strlitAux,0,strlitAuxSize);
- strlitAuxSize = 0;}
- <STRLITState><<EOF>> {column+=yyleng;
- printf("Line %d, column %d: unterminated string literal\n", aux_line, aux_column);
- semicolon_flag = 0;
- memset(strlitAux,0,strlitAuxSize);
- strlitAuxSize = 0;
- BEGIN 0;}
- <STRLITState>"\"" {column+=yyleng;
- strlitAux = realloc(strlitAux,strlitAuxSize+1);
- strlitAux[strlitAuxSize] = yytext[0];
- strlitAuxSize++;
- if(strlit_flag==1 && flag == 1){
- printf("STRLIT(");
- for(int i = 0;i < strlitAuxSize; i++){
- printf("%c", strlitAux[i]);
- }
- printf(")\n");
- }
- memset(strlitAux,0,strlitAuxSize);
- strlitAuxSize = 0;
- BEGIN 0;}
- <STRLITState>{newline} {line++;column=1;
- printf("Line %d, column %d: unterminated string literal\n", aux_line, aux_column);
- semicolon_flag = 0;
- memset(strlitAux,0,strlitAuxSize);
- strlitAuxSize = 0;
- BEGIN 0;}
- <STRLITState>. {column+=yyleng;
- //printf("teste = %c\n",yytext[0]);
- strlitAux = realloc(strlitAux,strlitAuxSize+1);
- strlitAux[strlitAuxSize] = yytext[0];
- strlitAuxSize++;}
- . {printf("Line %d, column %d: illegal character (%s)\n", line, column, yytext); column+=yyleng;}
- <<eof>> {if (semicolon_flag == 1 && flag == 1)printf("SEMICOLON\n");semicolon_flag=0; return 0;}
- %%
- void yyerror (const char *s)
- {
- printf("Line %d, column %d: %s: %s\n",line,column,s,yytext);
- }
- int main(int argc, char **argv) {
- if (argc > 1) {
- if (argv[1][0]=='-' && argv[1][1]=='l'){
- flag=1;
- yylex();
- }
- }
- else {
- yylex();
- }
- //yyparse();
- return 0;
- }
- int yywrap() {
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement