2015-11-19 67 views
0

我認爲我的結構知識是缺乏的。我得到的錯誤如:下推自動機的結構問題

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

任何想法?謝謝。

回答

0

就像結構中的其他變量一樣,top必須使用點符號作爲pda.top來訪問。而accepted必須是pda.accepted

某些語言有一個名稱範圍操作符,如withusing,但C沒有這個快捷方式。