0

我想从我的变量令牌中排除一些关键字我的变量令牌是:

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

和关键字是:

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

我尝试使用 {variable}^{KEYWORD} , ^{KEYWORD}{variable} 但它不起作用

我想制作变量令牌,使其无法从 KEYWORD 生成任何内容。怎么做..

4

1 回答 1

-1

我找不到解决此问题的灵活方法。所以我运行了一个函数来获取字符串中的单词,然后用关键字检查它们是否匹配。

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 函数 ::

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;
    }
于 2019-10-02T00:14:17.800 回答