Advertisement

04.26.2006 at 03:49AM PDT, ID: 21827869
[x]
Attachment Details

lex and yacc floating point calculator

Asked by luna621 in C Programming Language

Tags: yacc, lex, calculator

I'm used to compiling (1) a lex definition file and (2) a yacc definition file as follows:

1. lex mylexfile.l
2. yacc -d -l myfile.y
3. gcc y.tab.c lex.yy.c -ly -ll
4. ./a.out

I found this example, and I'm trying to understand it.  What is this BISON business?  Is there a way to separate them into one lex (mylexfile.l) and one yacc file (myfile.y)?  Any help appreciated.
---------------------------------------

/* Infix notation calculator--calc */

%{
#define YYSTYPE double
#define alloca malloc
#include <math.h>
%}

/* BISON Declarations */
%token NUM
%left '-' '+'
%left '*' '/'
%left NEG     /* negation--unary minus */
%right '^'     /* exponentiation        */

/* Grammar follows */
%%
input:    /* empty string */
       | input line
       ;

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

exp:      NUM                    { $$ = $1;         }
        | exp '+' exp             { $$ = $1 + $3;    }
        | exp '-' exp              { $$ = $1 - $3;    }
        | exp '*' exp             { $$ = $1 * $3;    }
        | exp '/' exp              { $$ = $1 / $3;    }
        | '-' exp  %prec NEG  { $$ = -$2;        }
        | exp '^' exp             { $$ = pow ($1, $3); }
        | '(' exp ')'                { $$ = $2;         }
        ;
%%

/* 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>

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;
}

main ()
{
  yyparse ();
}

#include <stdio.h>

yyerror (s)  /* Called by yyparse on error */
     char *s;
{
  printf ("%s\n", s);
}Start Free Trial
 
Loading Advertisement...
 
[+][-]04.26.2006 at 05:36AM PDT, ID: 16543140

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.26.2006 at 03:42PM PDT, ID: 16548739

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.26.2006 at 07:48PM PDT, ID: 16549778

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.27.2006 at 01:14AM PDT, ID: 16550787

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.27.2006 at 01:31AM PDT, ID: 16550852

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.27.2006 at 01:51AM PDT, ID: 16550927

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.27.2006 at 03:31PM PDT, ID: 16557967

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.27.2006 at 10:56PM PDT, ID: 16559626

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: yacc, lex, calculator
Sign Up Now!
Solution Provided By: fridom
Participating Experts: 2
Solution Grade: A
 
 
[+][-]04.27.2006 at 11:16PM PDT, ID: 16559683

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.27.2006 at 11:16PM PDT, ID: 16559685

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.28.2006 at 12:29AM PDT, ID: 16559921

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.28.2006 at 12:39AM PDT, ID: 16559992

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.28.2006 at 01:06AM PDT, ID: 16560094

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32