سؤال
كنت أقدم برنامج LEX بسيط للغاية (مجرد برنامج تمهيدي). ولكن على تجميع Lex.yy.c، أحصل على هذا الخطأ على النحو التالي:
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.
المحلول
خط نقل الخط 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;
}
نصائح أخرى
هنا هو فرق ضد الأصلي الخاص بك. إنه يعمل على إصلاح جميع المشاكل عند تجميع:
--- 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();
لا تنتمي إلى StackOverflow