Example #1
0
void read_grammar(char *filename)
{
  char buffer[1000];
  FILE *file;


  init_lexicons();
  if(GDEBUG) fprintf(stderr,"Initialised lexicons\n");

  init_grammar();
  if(GDEBUG) fprintf(stderr,"Initialised grammar\n");

  strcpy(buffer,filename);
  strcat(buffer,".nts");
  file = fopen(buffer,"r");
  read_nonterminals(file);

  file = fopen(buffer,"r");
  read_nonterminals2(file);
  if(GDEBUG) fprintf(stderr,"Loaded non-terminals\n");

  strcpy(buffer,filename);
  strcat(buffer,".lexicon");
  file = fopen(buffer,"r");
  read_lexicon(file);
  if(GDEBUG) fprintf(stderr,"Loaded lexicon\n");

  strcpy(buffer,filename);
  strcat(buffer,".grm");
  file = fopen(buffer,"r");
  read_grm(file);
  if(GDEBUG) fprintf(stderr,"Loaded grammar\n");
}
Example #2
0
int
main (int argc, char *argv[])
{
  int sentence_cnt, new_seed, i, file_flag, sent_flag, seed_flag;
  int handle;
  
  new_seed = 4951;
  sentence_cnt = 4;
  file_flag = 0;
  seed_flag = 0;
  sent_flag = 0;
  handle = STDOUT_FILENO;

  for (i = 1; i < argc; i++)
    {
      if (strcmp (argv[1], "-h") == 0)
        usage (0, NULL);
      else if (strcmp (argv[i], "-s") == 0)
	{
	  if (seed_flag++)
	    usage (-1, "Can't have more than one seed");
	  if (++i >= argc)
	    usage (-1, "Missing value for -s");
	  new_seed = atoi (argv[i]);
	}
      else if (strcmp (argv[i], "-n") == 0)
	{
	  if (sent_flag++)
	    usage (-1, "Can't have more than one sentence option");
	  if (++i >= argc)
	    usage (-1, "Missing value for -n");
	  sentence_cnt = atoi (argv[i]);
	  if (sentence_cnt < 1)
	    usage (-1, "Must have at least one sentence");
	}
      else if (strcmp (argv[i], "-f") == 0)
	{
	  if (file_flag++)
	    usage (-1, "Can't have more than one output file");
	  if (++i >= argc)
	    usage (-1, "Missing value for -f");

          /* Because files have fixed length in the basic Pintos
             file system, the 0 argument means that this option
             will not be useful until project 4 is
             implemented. */
	  create (argv[i], 0);
	  handle = open (argv[i]);
          if (handle < 0)
            {
              printf ("%s: open failed\n", argv[i]);
              return EXIT_FAILURE;
            }
	}
      else
        usage (-1, "Unrecognized flag");
    }

  init_grammar ();

  random_init (new_seed);
  hprintf (handle, "\n");

  for (i = 0; i < sentence_cnt; i++)
    {
      hprintf (handle, "\n");
      expand (0, daGrammar, daGLoc, handle);
      hprintf (handle, "\n\n");
    }
  
  if (file_flag)
    close (handle);

  return EXIT_SUCCESS;
}