error: invalid operands to binary

Posted on 2009-03-29
Last Modified: 2012-05-06

I try to compile my yacc grammar but i get the following errors :

error: invalid operands to binary *
error: invalid operands to binary /

Thanks in advance for any help !
Question by:unknown_

Author Comment

ID: 24015024
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define YYDEBUG 1

extern int  yyparse();
extern FILE *yyin;      

int yywrap()
    return 1;

void yyerror(const char *s)
    printf("\n%*s\n%*s\n", s);

enum treetype {operator_node, number_node, variable_node};
 typedef struct tree {
   enum treetype nodetype;
   union {
     struct {struct tree *left, *right; char operator;} an_operator;
     int ival;
     char sval;
   } body;
 } tree;
 static tree *make_operator (tree *l, char o, tree *r) {
   tree *result= (tree*) malloc (sizeof(tree));
   result->nodetype= operator_node;
   result->body.an_operator.left= l;
   result->body.an_operator.operator= o;
   result->body.an_operator.right= r;
   return result;
 static tree *make_number (int n) {
   tree *result= (tree*) malloc (sizeof(tree));
   result->nodetype= number_node;
   result->body.ival= n;
   return result;
 static tree *make_variable (char v) {
   tree *result= (tree*) malloc (sizeof(tree));
   result->nodetype= variable_node;
   result->body.sval= v;
   return result;
 static void printtree (tree *t, int level) {
 #define step 4
   if (t)
     switch (t->nodetype)
       case operator_node:
        printtree (t->body.an_operator.right, level+step);
        printf ("%*c%c\n", level, ' ', t->body.an_operator.operator);
        printtree (t->body.an_operator.left, level+step);
       case number_node:
        printf ("%*c%d\n", level, ' ', t->body.ival);
       case variable_node:
        printf ("%*c%c\n", level, ' ', t->body.sval);

%start goal

%union {
      int ival;
      char * sval;
        tree* btree;
%token <sval> IDENTIFIER
%token <sval> VARIABLE
%token <ival> INTEGER
 /*%token <ival> FLOAT*/
%token <sval> STRING
%token <sval> LITERAL
%token <sval> UNKNOWN
%token <sval> PLUS
%token <sval> MINUS
%token <sval> TIMES
%token <sval> SLASH
%token <sval> LPAREN
%token <sval> RPAREN
%token <sval> SEMICOLON
%token <sval> COMMA
%token <sval> EQL
%token <sval> OR
%token <sval> OR2
%token <sval> AND
%token <sval> AND2
/* keywords */
%token <sval> IF
%token <sval> ELSE
%token <sval> statement
%token <sval> DO
%token <sval> INT
%token <sval> RETURN
%token <sval> VOID
%token <sval> FLOAT
%token <sval> WHILE
%type <btree> goal
%type <ival> expression
%type <ival> term
%type <sval> factor
%type <sval> ifstatement
%type <sval> whilestatement
%type <sval> dostatement
%type <sval> variable_list
%type <sval> variable


goal                  :
variable '=' expression SEMICOLON {printf("rule : VAR = Expression\n");}
|      ifstatement SEMICOLON
|      whilestatement SEMICOLON
|       dostatement SEMICOLON
|      INT variable_list SEMICOLON {printf("rule: INT_VARIABLE\n");}      
|      FLOAT variable_list SEMICOLON   {printf("rule: FLOAT_VARIABLE\n");} {printtree ($1, 1);} ;

expression            :      term '+' term {printf("rule 1\n");} {$$ = make_operator ($1, '+', $3);}
|      term '-' term {printf("rule 2\n");} {$$ = make_operator ($1, '-', $3);}
|      term {printf("rule 3\n");}  {$$ = $1;}   ;

term                  :      factor '*' factor  {printf("rule 4\n");} {$$ = $1 * $3;}
|      factor '/' factor  {printf("rule 5\n");} {$$ = $1 / $3;}
|      factor {printf("rule 6\n");}  {$$ = $1;}  ;

factor                  :      variable {printf("rule 7\n");} {$$ = make_variable ($1);}
|      INTEGER {printf("rule 8\n");} {$$ = make_number ($1);}
|      FLOAT {printf("rule 9\n");} {$$ = make_number ($1);}
|      STRING {printf("rule 10\n");}
|      '(' expression ')' {printf("rule 11\n");} {$$= make_block ($2);};

ifstatement            :      IF '(' expression ')' goal ELSE goal SEMICOLON {printf("rule: IF_ELSE_STATEMENT\n");}
|      IF '(' expression ')' goal {printf("rule: IF_STATEMENT\n");};

whilestatement      :      WHILE '(' expression ')' goal {printf("rule: WHILE_STATEMENT\n");} {$$= make_while ($3, $5);} ;      
dostatement : DO expression WHILE '(' expression ')'   {printf("rule DO_STATEMENT\n");} {$$= make_do ($2, $5);} ;

variable : VARIABLE {printf("rule var2\n");}  {$$= list ($1); /* [$1] */} ;

variable_list : VARIABLE {printf("rule 15\n");} {$$= list ($1); /* [$1] */}
|        variable ',' variable_list {printf("rule 16\n");}  ;


#include "lex.yy.c"

main(int argc,char *argv[])
   printf("Please specify the input file\n");
 FILE *fp=fopen(argv[1],"r");
  printf("couldn't open file for reading\n");
LVL 40

Accepted Solution

mrjoltcola earned 500 total points
ID: 24015077
You have the %type factor declared as sval:

%type <sval> factor

But you are using it as numeric in the "term" rule:

term                  :      factor '*' factor  {printf("rule 4\n");} {$$ = $1 * $3;}
|      factor '/' factor  {printf("rule 5\n");} {$$ = $1 / $3;}
|      factor {printf("rule 6\n");}  {$$ = $1;}  ;

$1  *  $3 is the same as if you wrote:   yylval1.sval  *  yylval2.sval

You can't multiple 2 strings, and your intention here is to treat them has numbers, so define them as such in your %type declarations


Expert Comment

ID: 24018943
Is it just me, or does the printf statement in yyerror look a bit suspect?

printf("\n%*s\n%*s\n", s);

I don't think I have ever seen a "%*s %*s", s statement before.

I can understand something like the code I have supplied, but what on earth does that printf statement do?
 const char* msg = "Hello printf";

 int string_size = strlen (msg);

 printf("msg: %.*s", string_size, msg);

Open in new window

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

LVL 53

Expert Comment

ID: 24019034
HalfAsleep, %*s also works - it provides a minimum width to print the string. However, that printf is missing three parameters :) The output will likely show garbage.

Expert Comment

ID: 24019074
That is what I meant, I could not understand the single string parameter.  For that printf, I would expect 4.

Author Closing Comment

ID: 31564127
Thank you very much !
 Can you please have a look to the print parse tree question:

Im not sure how to work with the printout of the parse tree.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now