tags:

views:

66

answers:

2

Hello,

I'm trying to use Bison to compile(i don't know if this is the correct word to use), but when i try to compile this source code:

%{
#define YYSTYPE double
#include <math.h>
#include <stdio.h>
%}
%token NUM
%%
input:    /* empty */
        | input line
;

line:     '\n'
        | exp '\n'  { printf ("\t%.10g\n", $1); }
;

exp:      NUM             { $$ = $1;         }
        | exp exp '+'     { $$ = $1 + $2;    }
        | exp exp '-'     { $$ = $1 - $2;    }
        | exp exp '*'     { $$ = $1 * $2;    }
        | exp exp '/'     { $$ = $1 / $2;    }
      /* Exponentiation */
        | exp exp '^'     { $$ = pow ($1, $2); }
      /* Unary minus    */
        | exp 'n'         { $$ = -$1;        }
;
%%

/* Lexical analyzer returns a double floating point 
   number on the stack and the token NUM, or the ASCII
   character read if not a number.  Skips all blanks
   and tabs, returns 0 for EOF. */

#include <ctype.h>
#include <stdio.h>

yyerror(const char *s)

yylex ()
{
  int c;

  /* skip white space  */
  while ((c = getchar ()) == ' ' || c == '\t')  
    ;
  /* process numbers   */
  if (c == '.' || isdigit (c))                
    {
      ungetc (c, stdin);
      scanf ("%lf", &yylval);
      return NUM;
    }
  /* return end-of-file  */
  if (c == EOF)                            
    return 0;
  /* return single chars */
  return c;                                
}

yyerror (s)  /* Called by yyparse on error */
     char *s;
{
  printf ("%s\n", s);
}

main ()
{
  yyparse ();
}

I'm getting some "garbage" in the console(not in a file or something like), take a look: http://pastie.org/650893

Best Regards.

+1  A: 

That's an m4 input file or m4 header. Bison and flex use an ancient unix macro processor utility called m4, and that's what m4 input looks like. (I was able to get m4 -P to eat that file with only warnings.)

Normally, this all runs behind the scenes and is invisible. You seem to be on windows, and in a dos box shell. I'm guessing that you have a real bash console somewhere, perhaps via Cygwin, and I would suggest retrying the bison command in the full gnu environment. It may have less trouble with that. Windows is particularly poor at emulating standard output with streams and who knows what might have happened.

If that doesn't directly help, at least give us more information about your environment, please describe how bison was built or otherwise installed, and perhaps paste the command line you are using.

DigitalRoss
Thanks very much, all is working now!
Nathan Campos
A: 

I also have the same problem. I used bison and got this m4 file. I tried running m4 on bison's output but got the same file. How do I get a C file?

Just asking
I found the problem.I installed bison at D:\Program Files (x86)\GnuWin32\binThe spaces at "Program Files (x86)" caused the problem locating m4.When I reinstalled at D:\GnuWin32\bin it worked.
Just asking