$1 - has no declared type

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

I try to declare a rule in my yacc but i get the following error :
 $1 of `goal' has no declared type

Thanks in advance for any help !
Question by:unknown_
  • 4
  • 4

Author Comment

ID: 24065070

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYDEBUG 1
extern int  yyparse();
 extern int yylex();
 extern int line  ;
extern FILE *yyin;	
int yywrap()
    return 1;
void yyerror( const char *msg )
  printf("Line %d: %s\n",line, msg); 
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> ORR
%token <sval> AND
%token <sval> ANDD
/* keywords */
%token <sval> IF
%token <sval> ELSE
%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 <ival> factor 
%type <sval> ifstatement
%type <sval> whilestatement
%type <sval> dostatement
%type <sval> variable_list
%type <sval> variable 
goal	: statement_list SEMICOLON  {printtree ($1, 1);}
statement_list: { /*empty*/ }
                | statement_list statement
statement : declaration_statement
          | ifstatement 
          | whilestatement
          | variable '=' expression SEMICOLON 
          | dostatement 
declaration_statement : INT variable_list SEMICOLON {printf("INT_VARIABLE\n");}
                      | FLOAT variable_list SEMICOLON {printf("FLOAT_VARIABLE\n");}
expression		:	term '+' term {printf("ADDITION\n");} {$$ = make_operator($1, '+', $3);}
|	term '-' term {printf("SUBTRACTION\n");} {$$ = make_operator ($1, '-' ,$3);}
|	term {printf("\n");}  {$$ = $1;}   ;
term			:	factor '*' factor  {printf("MULTIPLICATION\n");} {$$ = make_operator($1,'*', $3);}
|	factor '/' factor  {printf("DIVISION\n");} {$$ = make_operator($1, '/', $3);} 
|	factor {printf("\n");}  {$$ = $1;}  ;
factor			:	variable {printf("rule 7\n");} 
|	INTEGER {printf("integer\n");} {$$ = make_number($1);} 
|	FLOAT {printf("float\n");}  {$$ = make_number($1);}
|	STRING {printf("string\n");}  {$$ = make_variable($1);}
|	'(' expression ')' {$$ = $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");};
dostatement : DO '(' expression ')' WHILE '(' expression ')' SEMICOLON   {printf("rule DO_STATEMENT\n");} ;
returnstatement : RETURN { } SEMICOLON   {printf("rule RETURN_STATEMENT\n");} ;
| RETURN factor SEMICOLON {printf("rule : RETURN 2\n");} 
variable : VARIABLE {printf("rule: variable2\n");} {$$= make_variable($1); };
variable_list : variable {printf("rule : variable1\n");}  {$$= make_variable($1); }
|  variable_list ',' variable {printf("rule: multvar\n");} {$$ = make_variable($1);} //	variable ',' variable_list {printf("rule: var,var\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");
 do {
 }while (!feof(yyin));

Open in new window

LVL 40

Accepted Solution

mrjoltcola earned 500 total points
ID: 24065095
Look at the rule for "goal" and tell me what $1 represents. That should give you the answer to what you need to do.

LVL 40

Expert Comment

ID: 24065135
Have you considered the book "Lex & Yacc" by John Levine? I have a hardcopy of it, I recommend it. You can spend 2 days reading it and answer a lot of your questions. Or read it online, parts of it are available at least.,M1
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 24065138
$1 represents statement_list

I still can't understand why its wrong
LVL 40

Expert Comment

ID: 24065176
So how do you declare what type statement_list is to yacc? The same way as the rest of the tokens and rules.
LVL 40

Expert Comment

ID: 24065185
The error is telling you exactly what is wrong. I don't see any type declaration for statement_list, do you?

Author Comment

ID: 24065237
ok , you're right there is no type declaration for statement_list
but the point is how can i call printtree otherwise ?  

LVL 53

Expert Comment

ID: 24089417
How about doing that in the main for example ?

Author Closing Comment

ID: 31566486
Can you please explain me how can i create a printout of my parse tree ?

Thanks in advance!


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Directions API to Map URL -C#? 3 37
Add different cell to otherwise similiar row 4 45
C# winforms programmitically move panels 6 44
Why use this lambda? 12 37
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

837 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