gpt4 book ai didi

c - 在简单的词法分析器中将字符作为字符串

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:11 24 4
gpt4 key购买 nike

我正在尝试用 C 实现一个简单的词法分析器。我的问题是关于字符和字符串的。通常在我的链接列表插入中,我将 char 作为参数。但是在关键字的情况下,因为它们在打印时是字符串,所以我遇到了问题:

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define MAX 50

char token[MAX];
char ch, str[25];

//Structure definition for lexemes
struct lexeme{
char lexemes;
char tokenclass[MAX];
struct lexeme *next;
};

typedef struct lexeme lexeme;

lexeme *firstPtr = NULL;
lexeme *lastPtr = NULL;

//This method is for inserting the values into linked list.
void insert(char s, char *t){

lexeme *np;
np = malloc(sizeof(lexeme));
np->lexemes = s;
strcpy(np->tokenclass, t);
np->next = NULL;

if (firstPtr == NULL){
firstPtr = np;
}
else{
lastPtr->next = np;
}
lastPtr = np;
}
/*void insert_key(char *kyw, char *t){
lexeme *kp;
kp = malloc(sizeof(lexeme));
kp->lexemes

}*/

void keyw(char *p);
int i = 0;

//Array of keywords
char keys[12][10] = { "break", "char", "continue",
"double", "else", "end", "for", "if", "int", "return", "void", "while" };

int main() {


char seps[13] = " \n,;(){}[]\"";
char oper[] = "!%^&*-+=~|.<>/?";
int j;
//char fname[200];
FILE *f1;
//clrscr();
fopen_s(&f1, "input.txt", "r");

if (f1 == NULL)
{
printf("file not found");
}

while ((ch = fgetc(f1)) != EOF)
{

for (j = 0; j <= 14; j++)
{
if (ch == oper[j])
{
printf("%c is an operator\n", ch);
strcpy(token, "operator");
insert(ch, token);
str[i] = '\0';
keyw(str);
}
}
for (j = 0; j <= 12; j++)
{
/* if(i==-1)
break;*/
if (ch == seps[j])
{
// if(strcmp(ch,"==") || strcmp(ch,"<=") || strcmp(ch,">=") || strcmp(ch,"<")|| strcmp(ch,">") || strcmp(ch,"?="))
// printf("%s is a logical operator",ch);

str[i] = '\0';
keyw(str);
}
}
if (i != -1)
{
str[i] = ch;
i++;
}
else
i = 0;
}
printf("(");
while (firstPtr != NULL){

printf("%c,", firstPtr->lexemes);
printf("%s |", firstPtr->tokenclass);
//printf("---- %c,%s ---- \n", firstPtr->next->lexemes, firstPtr->next->tokenclass);
firstPtr = firstPtr->next;
}
printf(")");
printf("\n");
printf("\n");

system("pause");
return 1;

}

void keyw(char *p)
{
int k, flag = 0;
for (k = 0; k <= 11; k++)
{
if (strcmp(keys[k], p) == 0)
{
printf("%s is a keyword\n", p);
strcpy(token, "keyword");
insert(p[0], token);
flag = 1;
break;
}
}
if (flag == 0)
{
if (isdigit(p[0]))
{
printf("%s is a number\n", p);
strcpy(token, "number");
insert(p[0], token);
}
else
{

if (p[0] != '\0')
{
printf("%s is an identifier\n", p);
strcpy(token, "id");
insert(p[0], token);
}
}
}
i = -1;
}

虽然我的输入是:

 int a=5;
int b=3;
int c;
if(a>b){
c=7;
b=c+a;
end
}

通常我得到这样的输出:

<i,keyword |=,operator |>,operator |a,id |5,number |i,keyword |=,operator |b,id |3,number |i, keyword |c,id | .... and so on.

我知道在关键字的情况下我不应该给出 p[0]。我还检查了我的结构定义,并将我的 char lexemes 改为 char lexemes[] 但我遇到了一些错误。我试图找到 C 的适当 str 类,但我找不到。我想要我的输出:

( int,keyword )  (i,keyword) instead

那么你有什么建议呢?我应该怎么做才能实现它?

最佳答案

我的建议:关键字另存为数字。

注册部分

    if (strcmp(keys[k], p) == 0)
{
printf("%s is a keyword\n", p);
strcpy(token, "keyword");
insert(k, token);//insert(p[0], token);
flag = 1;
break;
}

打印部分

    if(firstPtr->lexemes < 12)
printf("%s,", keys[firstPtr->lexemes]);
else
printf("%c,", firstPtr->lexemes);
printf("%s |", firstPtr->tokenclass);

关于c - 在简单的词法分析器中将字符作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22770133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com