tags:

views:

80

answers:

2

I was making this very simple lex program (just an introductory program). But on compiling lex.yy.c, I am getting this error as:

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

I could not make any reason for this error as I already have defined top in the specified structure. Can you see any reason for it?

Code is posted at http://pastebin.com/d5f059c1d

A: 

Move line 3 to line 16.

You also need to remove the initializers from the structure declarations - at least for C (but the C++ compiler didn't think much of it either).

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

To:

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

In the actions, you also need to declare 'ch'.

You also need to declare your functions - I made them static. This compiles (assuming you have a C99 compiler - the designated initializers won't work with C89 compilers):

%{
#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;
}
Jonathan Leffler
yeah i had done that but the problem persisted!!
mawia
But the errors probably changed...anyway, there's a complete compiling bit of code - accepted by Solaris C++ and GNU GCC. Whether it works is another issue altogether.
Jonathan Leffler
Thanks a lot every body for reply.it was mistake on my part to intialize the variables inside structure declaration.thanks!
mawia
+2  A: 

Here is a diff against your original. It fixes all the problems when compiling:

--- 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();
Gonzalo
hey why are redefining the top inside the structure.it does not seems to make any logic.
mawia
You had 'int top = -1;' inside the structure and that made gcc 4.4.1 fail to compile the program. I guess you were using g++ or any other C++ compiler.
Gonzalo
And that is a universal diff - the line prefixed '-' is removed and the line prefixed '+' is added.
Jonathan Leffler