1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

error: "integer out of range '$3' "yacc file

Discussion in 'C' started by cool_yogi, Sep 9, 2009.

  1. cool_yogi

    cool_yogi New Member

    Joined:
    Aug 28, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I'm getting a error in my yacc programme .
    the error is "integer out of range in '$3' "
    i have attached the code please check it and help me.
    it is in the line 77
    ifst: 'if' '(' expr ')' 'then' '{' expr ';' '}' else '{' expr ';' '}' {$$=makeifnode("if",$3,$7,$12);}//ERROR IS SHOWING HERE
    ;

    Code:
    %{
    #include <stdio.h>
    #include <stdlib.h>
    #include "syntree.h"
    #include "symtab.h"
    //#include "globals.h"
    #define NODE struct node*
    NODE makeleaf(int);
    NODE makebleaf(char*);
    NODE makeid(struct table*);
    NODE makenode(char*, NODE, NODE);
    NODE makeifnode(char* a, NODE, NODE, NODE);
    int yylex(void);
    void yyerror(char *);
    void protraverse(struct node* a);
    void evaluate(struct node* a);
    %}
    
    %union
    {
    	int num;
    	//char arr[10];
    	struct node* nodeptr;
    	struct table* tptr;
    };
    %start prog
    %token <num> NUMBER 
    %token <tptr> ID
    %type <nodeptr> expr stmtlist assignment stmt reads writes ifst
    %right '='
    %left '<' '>' GE LE NE EE
    %left '+' '-' 
    %left '*' '/'
    
    %%
    prog:		declsec '{' stmtlist '}'	{protraverse($3);printf("\n");evaluate($3);exit(0);}
    		;
    
    declsec:	'decl' decllist 'enddecl'	
    		;
    
    decllist:	decl
    		|decl decllist
    		;
    
    decl:		type idlist ';'
    		;
    
    type:		'int'
    		|'bool'
    		;
    
    idlist:		ID		
    		|ID ',' idlist
    		;
    
    
    
    stmtlist:	stmt stmtlist		{$$=makenode("s",$1,$2);}
    		|			{$$=NULL;}
    		;
    
    stmt:		assignment 		{$$=makenode("s",$1,NULL);}
    		|reads	   		{$$=makenode("s",$1,NULL);}
    		|writes	   		{$$=makenode("s",$1,NULL);}
    		|ifst				{$$=makenode("s",$1,NULL);
    		;
    
    assignment:	ID '=' expr ';'		{$$=makenode("=",makeid($1),$3);}		
    		;
    
    reads:		'read' '(' ID ')'	';'	{$$=makenode("r",makeid($3),NULL);}
    		;
    
    writes:		'write' '(' expr ')' ';'	{$$=makenode("w",$3,NULL);}
    		;
    ifst:			'if' '(' expr  ')' 'then' '{'  expr  ';' '}' else '{' expr ';' '}'		{$$=makeifnode("if",$3,$7,$12);}//ERROR IS SHOWING HERE
    		;
    					
    expr: 	  NUMBER		{$$=makeleaf($1);}
    	  |ID			{$$=makeid($1);}
    	  |'true'		{$$=makebleaf("true");}
    	  |'false'		{$$=makebleaf("false");}
    	  |expr '+' expr	{$$=makenode("+",$1,$3);}
    	  |expr '-' expr	{$$=makenode("-",$1,$3);}
    	  |expr '*' expr	{$$=makenode("*",$1,$3);}
    	  |expr '/' expr	{$$=makenode("/",$1,$3);}
    	  |expr  LE expr	{$$=makelnode("<=",$1,$3);}
    	  |expr  GE expr	{$$=makelnode(">=",$1,$3);}
    	  |expr '>' expr	{$$=makelnode(">",$1,$3);}
    	  |expr '<' expr	{$$=makelnode("<",$1,$3);}
    	  |expr  NE expr	{$$=makelnode("!=",$1,$3);}
    	  |expr  EE expr	{$$=makelnode("==",$1,$3);}
    	  ;
    %%
    
    void yyerror(char *s)
    { 
        fprintf(stderr, "%s\n", s); 
    } 
    
    int main(void) 
    { 
        yyparse(); 
        return 0; 
    }
    
     

Share This Page