2009-11-09 42 views
0

我正在製作這個非常簡單的lex程序(只是一個介紹性的程序)。但是在編譯lex.yy.c中,我得到這個錯誤爲:lex程序錯誤?

inToPostfix.l:26: error: ‘struct stackoperand’ has no member named ‘top’ 
inToPostfix.l:32: error: ‘struct stackoperator’ has no member named ‘top’.... 

我不能讓任何原因這個錯誤,因爲我已經在特定的結構定義頂部。 你能看到它的任何理由嗎?

代碼公佈在http://pastebin.com/d5f059c1d

+1

重新標記,以法,爲柔性標籤通常是指Adobe Flex的語言,而不是Unix編程柔性這對法語言 – 2009-11-09 19:57:07

+0

@mawia:編譯時,它修復了所有的問題,你欠了很多人們接受了答案,也得到了贊成票。你沒有給出一個單一的接受;你還沒有給出一個加票(或者倒票)。 – 2009-11-09 21:58:21

回答

0

移動線3線16

您還需要從結構聲明中刪除初始化 - 至少對於C(但C++編譯器並沒有想太多)。

struct stackoperator 
{ 
char stack[10]; 
int top =-1; 
}; 

要:

struct stackoperator 
{ 
char stack[10]; 
int top; 
}; 

在行動,你還需要申報 '通道'。

你還需要聲明你的函數 - 我使它們成爲靜態的。這將編譯(假設你有一個C99編譯器 - 指定的初始化不會與C89編譯器工作):

%{ 
#include<stdio.h> 

struct stackoperator 
{ 
char stack[10]; 
int top; 
}; 

struct stackoperand 
{ 
int stack[10][2]; 
int top; 
}; 
struct stackoperator operator = { .top = -1 }; 
struct stackoperand operand = { .top = -1 }; 
int num=0; 
static void push(int num,int flag); 
static int pop(void); 
static int precedence(char a,char b); 
%} 

%% 

[0-9] {num=num*10+(*yytext-'0');push(num,1);} 
[-+*/] { 
     if(precedence(operator.top,*yytext)) { 
      char ch=pop(); 
      push(ch,0); 
      operand.stack[operand.top][1]=1; 
     } 
     push(*yytext,0); 
    } 
[ \t] ; 
[\n]  { 
     char ch; 
     while(operator.top!=-1) 
     { 
      ch=pop(); 
      push(ch,0); 
     } 
     int i=0; 
     while(i<=operand.top) 
     { 
      if(operand.stack[operand.top][1]==1) 
       printf(" %c ",operand.stack[operand.top][0]); 
      else 
       printf(" %d ",operand.stack[operand.top][0]); 
     } 
    } 
%% 

static void push(int num,int flag) 
{ 
    if(flag) 
    {  operand.top++; 
     operand.stack[operand.top][0]=num; 
     operand.stack[operand.top][1]=0; 
    } 
    else 
     operator.stack[++operator.top]=num; 
} 

static int pop(void) 
{ 
    return operator.stack[operator.top--]; 
} 

static int precedence(char a,char b) 
{ 
    if(operator.top==-1) 
     return 0; 

    if((a=='*'||a=='/') && (b=='+'||b=='-')) 
     return 1; 
    else 
     return 0; 
} 
+0

是的,我已經這樣做了,但問題仍然存在! – mawia 2009-11-09 20:06:45

+0

但是錯誤可能改變了......無論如何,有一個完整的編譯位 - 被Solaris C++和GNU GCC所接受。它是否起作用完全是另一個問題。 – 2009-11-09 20:19:32

+0

非常感謝各位回覆。 我的錯誤是初始化結構聲明中的變量。 謝謝! – mawia 2009-11-09 21:33:54

2

這是針對你原來一個差異。

--- orig.l  2009-11-09 14:55:47.414002041 -0500 
+++ kk.l  2009-11-09 14:54:53.386385539 -0500 
@@ -1,14 +1,15 @@ 
%{ 
    #include<stdio.h> 
%} 
+  int precedence(char a,char b); 
     struct stackoperator{ 
       char stack[10]; 
-    int top =-1; 
+    int top; 
     }; 

     struct stackoperand{ 
       int stack[10][2]; 
-    int top =-1; 
+    int top; 
     }; 
     struct stackoperator operator; 
     struct stackoperand operand; 
@@ -29,6 +30,7 @@ 
     } 
[ \t] ; 
[\n]  { 
+    char ch; 
       while(operator.top!=-1) 
       { 
         ch=pop(); 
+0

嘿爲什麼重新定義結構內的頂部,似乎沒有任何邏輯。 – mawia 2009-11-09 20:13:44

+0

你有'int top = -1;'裏面的結構,並使海灣合作委員會4.4.1未能編譯程序。我猜你正在使用g ++或任何其他C++編譯器。 – Gonzalo 2009-11-09 20:18:16

+0

而且這是一個通用差異 - 刪除前綴爲' - '的行,並添加前綴爲'+'的行。 – 2009-11-09 20:30:52