help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Compiling Reverse Polish Calculator Example


From: Marc Mendiola
Subject: Compiling Reverse Polish Calculator Example
Date: Fri, 26 Oct 2012 14:13:38 -0700

To whom it may concern,

I am currently new to GNU Bison and thus, have only  read the manual briefly.  
I am trying to compile a working example of a parser in order to help my 
understanding of the material.  Therefore, I went through the rpcalc example 
provided online.  I copied the code verbatim to a new file named "rpcalc.y." 
However, when I try to generate the parser using "bison rpcalc.y," I receive 
the following errors.

rpcalc.y:1.1: invalid character: `\377'
rpcalc.y:1.2: invalid character: `\376'
rpcalc.y:1.4: invalid character: `'
rpcalc.y:1.5: invalid character: `'
rpcalc.y:1.6: invalid character: `'
rpcalc.y:1.7: invalid character: `'
rpcalc.y:1.8: invalid character: `'
rpcalc.y:1.8: invalid character: `/'
rpcalc.y:1.9: invalid character: `'
rpcalc.y:1.9: invalid character: `*'
rpcalc.y:1.10: invalid character: `'
rpcalc.y:1.11: invalid character: `'
rpcalc.y:1.11: syntax error, unexpected identifier

Is this a problem with my installation of bison or the actual code?  As far as 
I know, I have installed bison correctly, but perhaps there is a patch that I 
missed.  For your reference, I'll paste the contents of my file below.  But 
again, it is copied almost word-for-word from the manual.  I would appreciate 
any help you can provide.  Thank you!

Best regards,

Marc 

     /* Reverse polish notation calculator.  */
     
     %{
       #define YYSTYPE double
       #include <math.h>
        #include <ctype.h>
      #include <stdio.h>
       int yylex (void);
       void yyerror (char const *);
     %}
     
     %token NUM
     
     %% /* Grammar rules and actions follow.  */
    input:
       /* empty */
     | input line
     ;
     
     line:
       '\n'
     | exp '\n'      { printf ("%.10g\n", $1); }
     ;
     
     exp:
       NUM           { $$ = $1;           }
     | exp exp '+'   { $$ = $1 + $2;      }
     | exp exp '-'   { $$ = $1 - $2;      }
     | exp exp '*'   { $$ = $1 * $2;      }
     | exp exp '/'   { $$ = $1 / $2;      }
     | exp exp '^'   { $$ = pow ($1, $2); }  /* Exponentiation */
     | exp 'n'       { $$ = -$1;          }  /* Unary minus    */
     ;
     %%
     
     /* The lexical analyzer returns a double floating point
        number on the stack and the token NUM, or the numeric code
        of the character read if not a number.  It skips all blanks
        and tabs, and returns 0 for end-of-input.  */
     
    
     int
     yylex (void)
     {
       int c;
     
       /* Skip white space.  */
       while ((c = getchar ()) == ' ' || c == '\t')
         continue;
       /* Process numbers.  */
       if (c == '.' || isdigit (c))
         {
           ungetc (c, stdin);
           scanf ("%lf", &yylval);
           return NUM;
         }
       /* Return end-of-input.  */
       if (c == EOF)
         return 0;
       /* Return a single char.  */
       return c;
     }
     
          int
     main (void)
     {
       return yyparse ();
     }

         
     
     /* Called by yyparse on error.  */
     void
     yyerror (char const *s)
     {
       fprintf (stderr, "%s\n", s);
     }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]