How to exclude a token/expression from another token/expression in GnuWin32 Flex

132 Views Asked by At

I want to exclude some keywords from my variable token My variable token is:

variable [a-z|A-Z]+[a-z|A-Z|0-9]*

and Keyword is:

Datatype "int"|"double"|"char"|"void"
KEYWORD "include"|"define"|{Datatype}|"return"|"if"|"else"|"elif"|"loop"|"while"|"run"|"new"

I tried to use {variable}^{KEYWORD} , ^{KEYWORD}{variable} but it's not working

I want to make variable token such a way that it cant generate anything from KEYWORD. How to do that..

1

There are 1 best solutions below

2
sabertooth On

I couldn't find a flex way to solve this . So i ran a function to get words in a string and then checked them with keywords for match.

void getKeyword(char *yytext){
        char temp[109];
        for(int i=0;i<strlen(yytext);i++){
            for(int j=i+1;j<=strlen(yytext);j++){
                if(yytext[j]=='\n' || yytext[j]==' ' || yytext[j]=='(' || yytext[j]==';'|| yytext[j]==','){
//Terminator
                    int id=0;
                    int k=i;
                    while(k<j && (yytext[k]==' '))k++; //removing back spaces
                    int l=j-1;
                    while(l>=k && (yytext[l]==' '))l--; // removing forward spaces
                    for(;k<=l;k++){
                        temp[id++]=yytext[k]; //storing the word
                    }
                    temp[id]='\0';

                    if(isKeyword(temp)){ //checker function
                        i=j-1;
                        //Saving it to an char array
                         memcpy(out[6][idx[6]++],temp,strlen(temp));
                        break;
                    }
                }
            }
        }
    }

iskeyword function ::

int isKeyword(char *c){
        if(!strcmp("return",c) || !strcmp("include",c) || !strcmp("define",c) || !strcmp("int",c)|| !strcmp("double",c)|| !strcmp("char",c)|| !strcmp("void",c)|| !strcmp("if",c)|| !strcmp("elif",c)|| !strcmp("else",c)|| !strcmp("loop",c)|| !strcmp("while",c) )
            return 1;
        return 0;
    }