Перемещение линии 3 в строке 16.
Кроме того, необходимо удалить инициализаторами из деклараций структуры - по крайней мере, для С (но компилятор C++ не думаю, что много его).
struct stackoperator
{
char stack[10];
int top =-1;
};
To:
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;
}
Retagged Лекс, как прогибается тег обычно относится к языку Adobe Flex, а не программа гибкого Unix, которая является для ЛЕКС языка –
@mawia: Вы много обязаны люди принимали ответы и повышали голос. Вы не дали ни одного согласия; вы не дали ни единого голосования (или проголосовавшего). –