I have attached the latest listing of the program in question. It is actually parse.y but had to be uploaded as parse.txt. When I run
Bison now, then the messages I get from that are...
parse.y: warning: 8 useless nonterminals and 30 useless rules
parse.y:52.1-7: fatal error: start symbol grammar does not derive any
sentence. The 2nd message is the most crucial at the moment.

I believe ( could be wrong) that The particular code it refers to is...
%%
grammar: domains
constants
predicates
;

My intention is that it reads a set of domains...
typically each one is to a line of a format...
D0 11 0 10 // domainName, number_of_values, floor_value, ceil_value
...
Then reads a set of constants...
typically each one to a line of a format...
X0 D0 9

Then reads and processes a set of predicates, each one to a line, which
may appear as...
or(lt(mul(X0,X1),X2),ge(abs(sub(X3,X4)),X5))

However, I have no precedent or pro-forma for such as 'grammar' above. I
think that the domains constants predicates sequence I've written will
read all the
domains, then all the constants then all the predicates without allowing
any regression through any of them (eg a domain in the constants).

That the start symbol 'grammar' cannot derive any sentence only suggests
to me that the grammar high level nonTerminal cannot access it's
subsidiary terminals and non-terminals, but the exact cause is not yet
clear to me.

The classic example such as p75 of lex & yacc...[1995 Levine et al]
statement_list: statement '\n'
statement_list statement '\n'
;
...doesn't shed much light on my codes attached below, where I must read a
group of domains, followed by a group of constants, followed by a group of
predicates.

Attachments
/* Development NOTES
Check DEMO P 13, "line:"  and abouts... chk txt open
*/
/* parse.y for the project language */
%{			/* C DECLARATIONS */
#include <string.h>	/* if needed for symlook()  and strstr()*/
#include <math.h>	/* for abs, min max etc */
#include <stdio.h>  	/* for printf etc */
#include <stdlib.h>
#include "symtable.h"   /* symbol table, and its contributors */

char* strdup( const char *);
extern FILE *yyin;

%}
			/* THIS IS BASICALLY A DECLARATION OF THE DATATYPES TO BE USED */
%union {		/* this becomes yylval ! */
  int val; 		/* */
  char *str;
  struct symtab *symp;
}


%token <val> NUM 	/* integral constant. */ 
%token <symp> NAME  	/* Domain/IntegralConstant name. */ 
%type <val> exp 	/* determines that exp is an int */


%token MAX		/* to be called or directly executed*/
%token MIN
%token ABS
%token XOR

%token PWR
%right PWR 

%token COMMA		/* lowest precedence thru to highest below... */
			/* There is no ASSIGN operator */
%left OR
%left AND
%left EQ NE          	/* equal to, not equal	*/
%left LT LE GT GE    	/* lessThan, lessThanOrEQto, greaterThan,grThanOrEQto */

%left ADD SUB        	/* addition, subtraction */
%left MUL DIV  MOD      /* multiply, divide, modulus */
%right NOT            	/* ! */
%nonassoc NEG	     	/* NEGATION. Bison Manual 35 has it %left */
%token L_BRAC R_BRAC	/* left parenthesis, r parenthesis */
			/*... highest precedence is bottom of list above */

%%
grammar:  
	  domains	
	  grammar constants 	
	  grammar predicates
	;				
domains:  domain                    /* the list of all the DOMAINS */
	| domains domain
	;
domain:	NAME NUM NUM NUM 	       {
printf("\ndoing DOMAIN %s declaration\n", $1); 
/* CHECK FOR THE NAME IN THE SYMBOL TABLE, if duplicate, advise and quit */
	symlook($1);
	strcpy($1,symtab->name);
	symtab->$1.domain_or_constant = DOMAIN;
	symtab->$1.domain_and_const_vals.number_of_contiguous_values = $2;
	symtab->$1.domain_and_const_vals.lowest_value = $3;
	symtab->$1.domain_and_const_vals.highest_value = $4;
       }
	;
constants:  constant			/* the list of all the CONSTANTS */
	|   constants constant 
	;
constant:	NAME NAME NUM 		       {  /* CONSTANT found */
printf("\ndoing  CONSTANT %s declaration\n", $1); 
/* CHECK FOR THE NAME IN THE SYMBOL TABLE, if duplicate, advise and quit */
	strcpy($1,symtab->name);
	symtab->$1.domain_or_constant = CONSTANT;
	/*strcpy($2,symtab->$1.domain_and_const_vals.domain_ref); */
	symtab->$1.domain_and_const_vals.domain_ref = strdup($2);
	symtab->$1.domain_and_const_vals.constant_val = $3;
	};
predicates: exp                /* this is the list of PREDICATES */
      | predicates exp { 	
	  if(!predicate_count && failures)
	  {
	    printf("%d declaration error%c\n",failures,failures>1?'s':'');
	    yyerror("Terminating program \n");
	    exit(1); 
	  }
	  if(!predicate_count++) 
	    ;
	    /*here try to print the predicates line, using a parallel file access 
		 we have completed an analytical statement and have a result */ 
	    printf("\n %s\n\n",($2 == 0) ? "FALSE" : "TRUE");
	};

exp :/*line 98*/MAX '(' exp ',' exp ')' { $$ = (($3 > $5) ? $3 : $5 );} /* line 98 */
	|	MIN '(' exp ',' exp ')' { $$ = ($3 < $5) ? $3 : $5 ;}
	|	ABS '(' exp ')'  	{ $$ = ($3 < 0) ? $3 * (-1) : $3 ;}
	|	XOR '(' exp ',' exp ')' { $$ = (($3 || $5) && !($3 && $5)); }
	|	PWR '(' exp ',' exp ')' { $$ = pow($3 , $5);}


	|	OR  '(' exp ',' exp ')' { $$ = ($3 || $5);}
	|	AND '(' exp ',' exp ')' { $$ = ($3 && $5);}
	|	EQ  '(' exp ',' exp ')' { $$ = ($3 == $5);}
	|	NE  '(' exp ',' exp ')' { $$ = ($3 != $5);}

	|	LT  '(' exp ',' exp ')' { $$ = ($3 < $5);}
	|	LE  '(' exp ',' exp ')' { $$ = ($3 <= $5);}
	|	GT  '(' exp ',' exp ')' { $$ = ($3 > $5);}
	|	GE  '(' exp ',' exp ')' { $$ = ($3 >= $5);}

	|	ADD '(' exp ',' exp ')' {$$ = $3 + $5; }
	|	SUB '(' exp ',' exp ')' {$$ = $3 - $5; }
	|	MUL '(' exp ',' exp ')' {$$ = $3 * $5; }
	|	DIV '(' exp ',' exp ')' {if($5 == 0)
					    {
						yyerror("Division by zero\n");
					        YYABORT;
					    }
					   else
					     $$ = $3 / $5; 
					  }
	|	MOD '(' exp ',' exp ')' {$$ = ($3 % $5);}
	|	NOT '(' exp ')'          {$$ = !($3);}
	|	NEG '(' exp ')'  %prec NEG        {$$ = $3 * (-1); }
	;


%%
/*/////////////////////////////////////////////////////////////////////////*/
/* look up a symbol table entry, if not present then add it */
/*int yyerror(char *error);*/

struct symtab * symlook(char *s)
{
  struct symtab *sp;
  thisFailed = 0;   // reset the flag for this identifier.
  for(sp = symtab;  sp < &symtab[NUM_SYMS]; sp++)
  {				/* is variable identifier already here ? */
    if(sp->name && !strcmp(sp->name, s)) /* s already exists */
    {
      printf("%s is a duplicate Domain or Constant name.\n", s);
      failures++; 
    }
  } /* make provision for either dynamic alloc for symbol records, or */
    if(!thisFailed)
    {
      sp->name = strdup(s); /* strdup automatically allocates space, is ...[80] a problem? */
      return sp;
    } /* else go to the next record. */
    else
      return null;
}

/*/////////////////////////////////////////////////////////////////////*/

int yyerror(char *error)
{
 printf("%s\n", error);
 return(1);
}
/*////////////////////////////////////////////////////////////////////*/
void init_symtable()
{
  struct symtab * sp;
  for(sp = &symtab[0]; sp < &symtab[NUM_SYMS]; sp++)
    sp->name = '\0'; /* start the name fields with nulls */
}
/*////////////////////////////////////////////////////////////////////*/
void no_infile()
{
  printf("Error opening input file\n");
  printf("usage: project input_filename\n\n");
  exit(1);
}
/*////////////////////////////////////////////////////////////////////*/
void do_copyright()
{
  printf("\n\nproject Compiler (c) 2009\n\n");
}
/*////////////////////////////////////////////////////////////////////*/
int main(int argc, char *argv[])
{
 /* yydebug = 1; */         /* print debug data for each token/value */
printf("\nV.1 started\n");
  do_copyright();
  if(argc>1)
  {
    FILE *file;
    file = fopen(argv[1], "r");
    if(!file)
    {
      fprint(stderr,"Could not open %s, terminating program\n",argv[1]);
      exit(1);
    }
    yyin = file;
  }
   printf("\nInitialising symbol table\n\n");
   init_symtable();
  printf("\nCalling yyparse()/*, there should be debug outputs*/\n\n");
  yyparse();
  printf("\nCompleted yyparse()\n\n");
  exit(0);
}

I'm not exactly sure what the error means, but I believe you mean

grammar:  
	  domains	
	  constants 	
	  predicates
	;

or

grammar:  
	  domains	
	 | grammar constants 	
	 | grammar predicates
	;

depending upon if you want just one set of constants followed by one set of predicates, or just one set of domains followed by any number of constants and predicates.

I'm not exactly sure what the error means, but I believe you mean

grammar:  
	  domains	
	  constants 	
	  predicates
	;

or

grammar:  
	  domains	
	 | grammar constants 	
	 | grammar predicates
	;

depending upon if you want just one set of constants followed by one set of predicates, or just one set of domains followed by any number of constants and predicates.

Thanks for answering Marc,
Firstly a list of uninterrupted domain data are read,
then a list of uninterrupted constants are read,
then a list of uninterrupted predicates are read,
the formats for each above are as described in the initial message.
None of the domains or constants or predicates can appear out of place in the described sequence described in this message above.

I understand the Yacc code you wrote, but am surprised that the Bison code described after could be different. The Yacc code FORCES sequence, the Bison code would appear to not do so because of the |. It looks to me as though the Bison code could be any sequence of- all domains or all constants or all predicates with no forced sequence.

The requirement I am trying to fulfill is just one set of domains, then THEY'RE finished, then one set of constants, then THEY'RE finished, then the set of predicates, at completion of which is the end of the program.

Thanks, Joe, Cork, Ireland

The yacc/bison language varianced was just testing if daniweb put any syntax highlighting for either language.

Yes, according to your requirements you want the "yacc" one.

This article has been dead for over six months. Start a new discussion instead.