gpt4 book ai didi

c - 下推自动机的结构问题

转载 作者:行者123 更新时间:2023-11-30 17:08:10 25 4
gpt4 key购买 nike

我认为我的结构知识缺乏。我收到如下错误:

  1. pda.c:33:26: error: 'top' undeclared (first use in this function)
    if(pda.stack[top] == '\0')
  2. pda.c:54:7: error: 'accepted' undeclared (first use in this function)
    if(accepted == 0)
  3. pda.c: In function 'qX':
    pda.c:93:17: error: 'top' undeclared (first use in this function)
    pda.stack[top] = input;

基本上我的全局结构中的变量没有被识别,我不知道为什么。这是我记录的代码:

//This project is a PDA which accepts alphabetical, lower-case palindromes written out and divided by a #.
//Example: "hello world#dlrow olleh" is an accepted string.
#include <stdio.h>
//Pushdown automata structure includes the string to validate with an index, the stack
//with an index, the current state, the running status, and the result.
struct PDA {
char string[200];
char stack[200];
int current;
int top;
int qOrp;
int finished;
int accepted;
};
//Initialization of struct and state methods.
struct PDA pda = {.current = 0, .top = 0, .qOrp = 0, .finished = 0, .accepted = 0};
int validateInput();
int qE(char input);
int qX(char input);
int pX(char input);
int pE();

int main() {
//Prompt for string to validate.
printf("Enter the string to be verified: ");
scanf("%s\n", &pda.string);
//Validates the alphabet of the string.
pda.finished = validateInput();
//While the PDA is rinning, do...
while(pda.finished == 0) {
//Different cases for different states.
switch(pda.qOrp) {
case 0:
//State for the initial, empty stack.
if(pda.stack[top] == '\0')
pda.finished = qE(pda.string[current]);
//State for non-empty stack before the #.
else {
pda.finished = qE(pda.string[current]);
}
break;
case 1:
//State for empty stack after #.
if(pda.stack[top] == '\0')
pda.finished = pE();
//State for non-empty stack after #.
else
pda.finished = pX(pda.string[current]);
break;
//Reject if not in one of the states above.
default:
pda.finished = 1;
}
}
//Print for acceptance/rejection.
if(accepted == 0)
printf("The string \"%s\" is rejected.", pda.string);
else
printf("The string \"%s\" is accepted.", pda.string);

return 0;
}
//Loops through the string to validate and makes sure the alphabet is correct. Otherwise, reject.
int validateInput() {
int i = 0;

while(pda.string[i] != '\0') {
if(pda.string[i] < 97 || pda.string[i] > 122 || pda.string[i] != '#' || pda.string[i] != ' ')
return 1;
}

return 0;
}
//Initial State. Checks for illegal characters. Otherwise, push current character.
int qE(char input) {
if(input == '#')
return 1;
else {
pda.stack[top] = input;
pda.top++;
pda.current++;
return 0;
}
}
//Second State. Switches to next state on #. Otherwise, push current character.
int qX(char input) {
if(input == '#') {
pda.qOrp++;
pda.current--;
pda.top--;
}
else {
pda.stack[top] = input;
pda.top++;
pda.current++;
}
}
//Third state. Checks for illegal characters. Otherwise, pop one element from stack.
int pX(char input) {
if(input == '#')
return 1;
else {
if(pda.string[current] == pda.stack[top]) {
pda.stack[top] = '\0';
pda.top--;
pda.current--;
}
else
return 1;
}
}
//Final state. Declares the string "accepted."
int pE() {
pda.accepted = 1;
return 1;
}

有什么想法吗?谢谢。

最佳答案

就像结构中的其他变量一样,top 必须使用点表示法作为 pda.top 进行访问。并且accepted必须是pda.accepted

某些语言有名称范围运算符,如 withusing,但 C 没有快捷方式。

关于c - 下推自动机的结构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33796701/

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