C语言 如何在 GnuWin32 Flex 中从另一个令牌/表达式中排除一个令牌/表达式



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

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"

我尝试使用 {变量}^{关键字}, ^{关键字}{变量} 但它不起作用

我想使变量令牌无法从关键字生成任何内容。怎么做..

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

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

is关键字功能 ::

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;
}

最新更新