How to write a parser in C?

parser code for a program..c programming

  • I was wondering if any of you pro's would like to help me write a parser code for my program..here is an example of the code from my instructor: I would really like to learn so can you use comments when you help me parser example [root@win186821wks c]# more parser.c main(int argc, char** argv) { int age,month,maxargs,i; char name[80]; /*(NOTE: this may look overkill but please notice how easy it will be to add another command line option, or to add another 50 options! If there is something you don't undestand, please mail me questions. I added the "main" stuff above and the printf() at the end, just so you can compile and run the program to see how it works. */ /* current command line arguments: -n name User's first name ("Sean" is default) -m month integer month (default is "1" for January) -a age integer age (default is "39" :) ) */ maxargs = 7; /* check for too many command line arguments */ if (argc > maxargs) { printf("Usage: %s [-n name -m month -a age ]\n",argv[0]); exit(1); } /* set default values */ strcpy(name,"Sean"); month = 1; age = 39; for (i=1;i<argc;i+=2) { /* check for valid flags (first char should be '-' for flags) */ if (argv[i][0]!='-') { printf("bad option character...should be \"-\"\n"); exit(2); } /* check for valid flag value and take appropriate action */ switch (argv[i][1]) { case 'n': strcpy(name,argv[i+1]); break; case 'm': month = atoi(argv[i+1]); break; case 'a': age = atoi(argv[i+1]); break; default: printf("bad option...should be \"n\" or \"m\" or \"a\"\n"); exit(3); break; } } printf("age = %d, month = %d, name = %s\n",age,month,name); } and here is an example of my code: #include <stdio.h> #include <stdlib.h> main(int argc, char *argv[]) { double sum_intensities = 0; int total_values = 0; int total_nonzero_values = 0; double avg_values = 0; double avg_nonzero = 0; float new_value[400]; int result; FILE *datafile; char *filename; int img_rows; int i; int size; /* SET THE filename VARIABLE */ filename=(argv[1]); img_rows=(atoi(argv[2])); size = (img_rows*img_rows); /* open the file */ datafile = fopen(filename, "r"); /* this is the loop. */ result = 1; /* fread will return 0 on error or end of file. */ result = fread(&new_value, sizeof(float), size, datafile); for (i = 0; i < size; i++) { /* update the statistics with the new value in new_value */ if (result == size) { total_values++; sum_intensities += new_value[i]; if (new_value[i] > 0) { total_nonzero_values++; } } else { printf("invalid file format (result = '%d')\n", result); exit(0); } } avg_values= sum_intensities / total_values; avg_nonzero= sum_intensities / total_nonzero_values; printf ("avg_values %e\n",avg_values); printf ("avg_nonzero %e\n",avg_nonzero); printf ("total_values %e\n",total_values); printf ("total_nonzero_values %e\n",total_nonzero_values); }

  • Answer:

    Hey derekwtp-ga, Okay, here is a stab at the parser code. It allows you to set either the filename or the number of image rows. If they are not specified, it sets default values. dogbite-ga #include <stdio.h> #include <stdlib.h> /* a structure to hold our arguments */ struct arguments { char filename[1024]; /* the -f option */ int img_rows; /* the -n option */ }; /***************************************** * Function: parseArguments * Inputs: a structure to hold the argument values, and * the number of args on the command line, and * a list of those arguments * Notes: populate the arguments structure *****************************************/ void parseArguments(struct arguments *arguments, int argc, char *argv[]) { int maxargs = 5; int i; /* check for too many arguments */ if (argc > maxargs) { printf("Usage: %s [-f imagefile -n imagerows]\n", argv[0]); exit(1); } /* first set default values */ strcpy(arguments->filename, "image.001"); arguments->img_rows = 10; /* now loop through the command line args */ for (i = 1; i < argc; i += 2) { /* check if the first character is a dash. i.e. -f or -n */ if (argv[i][0] != '-') { printf("bad option character...should be \"-\"\n"); exit(2); } /* check for valid flag value and take appropriate action */ switch (argv[i][1]) { case 'n': arguments->img_rows = atoi(argv[i+1]); break; case 'f': strcpy(arguments->filename,argv[i+1]); break; default: printf("bad option...should be \"n\" or \"f\"\n"); exit(3); break; } } return; } main(int argc, char *argv[]) { double sum_intensities = 0; int total_values = 0; int total_nonzero_values = 0; double avg_values = 0; double avg_nonzero = 0; float new_value[400]; int result; FILE *datafile; int i; int size; /* declare and allocate memory for the structure */ struct arguments *myargs; myargs = malloc(sizeof(struct arguments)); /* set the arguments based on command line options */ parseArguments(myargs, argc, argv); /* calculate the size of the square image */ size = (myargs->img_rows * myargs->img_rows); /* open the file */ datafile = fopen(myargs->filename, "r"); if (datafile == NULL) { printf("Could not open file '%s'\n", myargs->filename); exit(4); } result = 1; /* fread will return 0 on error or end of file. */ result = fread(&new_value, sizeof(float), size, datafile); for (i = 0; i < size; i++) { /* update the statistics with the new value in new_value */ if (result == size) { total_values++; sum_intensities += new_value[i]; if (new_value[i] > 0) { total_nonzero_values++; } } else { printf("invalid file format (result = '%d')\n", result); exit(0); } } avg_values= sum_intensities / total_values; avg_nonzero= sum_intensities / total_nonzero_values; printf ("avg_values %e\n",avg_values); printf ("avg_nonzero %e\n",avg_nonzero); printf ("total_values %e\n",total_values); printf ("total_nonzero_values %e\n",total_nonzero_values); free(myargs); }

derekwtp-ga at Google Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.