Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #undef yywrap
- #define yywrap() 1
- #include <math.h>
- #include <unistd.h>
- #include <string.h>
- #define CODE 3
- #define ID 2
- #define CONST 1
- #define ERROR 0
- int code;
- typedef struct{
- int type;//CODE,ID,CONST
- int code;
- char* text;
- }Token;
- typedef struct{
- int size;
- void* elems;
- }Vector;
- %}
- CONSTANT ["][a-zA-Z0-9 :\*><+!=>><<::\.]*["]
- IDENTIFIER [a-zA-Z][a-zA-Z0-9]{0,7}
- DIGIT [0-9]
- FLOAT [1-9][0-9]*\.*[0-9]*
- %%
- "{" {code = 2;return CODE;}
- "}" {code = 3;return CODE;}
- "if" {code = 51;return CODE;}
- "else" {code = 52;return CODE;}
- "(" {code = 71;return CODE;}
- ")" {code = 72;return CODE;}
- "<" {code = 73;return CODE;}
- ">" {code = 74;return CODE;}
- "=" {code = 75;return CODE;}
- ";" {code = 76;return CODE;}
- "*" {code = 77;return CODE;}
- "+" {code = 78;return CODE;}
- "-" {code = 79;return CODE;}
- "#include" {code = 89;return CODE;}
- "using namespace" {code = 81;return CODE;}
- "int main" {code = 82;return CODE;}
- "int" {code = 83;return CODE;}
- "double" {code = 84;return CODE;}
- "const" {code = 85;return CODE;}
- "while" {code = 86;return CODE;}
- "cout" {code = 87;return CODE;}
- "endl" {code = 88;return CODE;}
- "return" {code = 89;return CODE;}
- "!=" {code = 90;return CODE;}
- "<<" {code = 91;return CODE;}
- ">>" {code = 92;return CODE;}
- "::" {code = 93;return CODE;}
- "," {code = 94;return CODE;}
- {CONSTANT} {code = 1;return CONST;}
- {DIGIT} {code = 1;return CONST;}
- {FLOAT} {code = 1;return CONST;}
- "0\."{DIGIT}+ {code =1 ;return CONST;}
- {IDENTIFIER} {code = 0;return ID;}
- [ \t\n]+ /* eat up whitespace */
- {DIGIT}{IDENTIFIER} {printf("ERROR : %s",yytext);code = -1;return ERROR;}
- "0"{DIGIT}+ {printf("ERROR : %s",yytext);code = -1;return ERROR;}
- "0\." {printf("ERROR : %s",yytext);code = -1;return ERROR;}
- . {printf("ERROR : %s",yytext);code=-1;return ERROR;}
- %%
- Vector* init_vector(){
- Vector* vector = malloc(sizeof(Vector));
- vector->elems = malloc(sizeof(Token)*300);
- vector->size = 0;
- return vector;
- }
- int get_position(Vector* vector,char* text){
- int position = 0;
- while(position < vector->size){
- if(strcmp(((Token*)vector->elems)[position].text,text)==0){
- return ++position;
- }
- position++;
- }
- return -1;
- }
- insert_vector(Vector* vector,int type){
- if(get_position(vector,yytext)==-1){
- ((Token*)vector->elems)[vector->size].type = type;
- ((Token*)vector->elems)[vector->size].text = strdup(yytext);
- ((Token*)vector->elems)[vector->size].code = code;
- vector->size++;
- }
- }
- sort_vector(Vector* vector){
- int ok = 0,i=0,result = 0;
- Token aux;
- do{
- ok=1;
- for(i=0;i<vector->size-1;i++){
- Token first =((Token*)vector->elems)[i];
- Token second =((Token*)vector->elems)[i+1];
- result = strcmp(first.text,second.text);
- if(result>=0){
- ok = 0;
- aux.type = ((Token*)vector->elems)[i+1].type;
- aux.text = ((Token*)vector->elems)[i+1].text;
- aux.code = ((Token*)vector->elems)[i+1].code;
- ((Token*)vector->elems)[i+1].type = ((Token*)vector->elems)[i].type;
- ((Token*)vector->elems)[i+1].text = ((Token*)vector->elems)[i].text;
- ((Token*)vector->elems)[i+1].code = ((Token*)vector->elems)[i].code;
- ((Token*)vector->elems)[i].type = aux.type;
- ((Token*)vector->elems)[i].text = aux.text;
- ((Token*)vector->elems)[i].code = aux.code;
- }
- }
- }while(ok !=1);
- }
- free_vector(Vector* vector){
- int i =0;
- for(i=0;i< vector->size;i++){
- free(((Token*)vector->elems)[i].text);
- }
- free(vector->elems);
- }
- main( argc, argv )
- int argc;
- char **argv;
- {
- ++argv, --argc; /* skip over program name */
- if ( argc > 0 )
- yyin = fopen( argv[0], "r" );
- else
- yyin = stdin;
- int result;
- Vector* codes = init_vector();
- Vector* TC = init_vector();
- Vector* TS = init_vector();
- while ((result = yylex())) {
- int index = codes->size;
- Token* tokens = (Token*) codes->elems;
- switch(result){
- case CODE:
- // printf("token is CODE text : %s code: %d\n",yytext,code);
- tokens[index].type = CODE;
- break;
- case CONST:
- // printf("token is CONST text : %s code: %d\n",yytext,code);
- tokens[index].type = CONST;
- insert_vector(TC,CONST);
- break;
- case ID:
- // printf("token is ID text : %s code: %d\n",yytext,code);
- tokens[index].type = ID;
- insert_vector(TS,ID);
- break;
- case ERROR:
- printf("token is ERROR text : %s code: %d\n",yytext,code);
- break;
- default:
- printf("never should be here text : %s code: %d\n",yytext,code);
- }
- tokens[index].text = strdup(yytext);
- tokens[index].code = code;
- codes->size++;
- }
- if(code == -1){
- return;
- }
- printf("yylex finished without error\n");
- FILE* FIP = fopen("FIP","w");
- int i=0;
- sort_vector(TC);
- sort_vector(TS);
- for(i=0;i<codes->size;i++){
- Token* tokens = (Token*) codes->elems;
- switch(tokens[i].type){
- case CODE:
- fprintf(FIP,"%d /\n",tokens[i].code);
- break;
- case CONST:
- fprintf(FIP,"%d %d\n",tokens[i].code,get_position(TC,tokens[i].text));
- break;
- case ID:
- fprintf(FIP,"%d %d\n",tokens[i].code,get_position(TS,tokens[i].text));
- break;
- }
- }
- fclose(FIP);
- printf("TC : ");
- for(i=0;i<TC->size;i++){
- Token token= ((Token*)TC->elems)[i];
- printf("%s ,",token.text);
- }
- printf("\nTS : ");
- for(i=0;i<TS->size;i++){
- Token token= ((Token*)TS->elems)[i];
- printf("%s ,",token.text);
- }
- free_vector(codes);
- free_vector(TC);
- free_vector(TS);
- }
Add Comment
Please, Sign In to add comment