Thread: Reading a C code text into an array

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    Reading a C code text into an array

    So, i have this program for correcting syntax errors written in C, from the K&R book.

    Here is the code below.

    My question is, how can i modify this code so that the program does this:

    1. I enter the program in console mode - after that i write in the name of the C project which i want to check for syntactic errors.

    2. Then, the program puts characters in an array and checks for syntactic errors (the part of the code i dont have to change).

    Im using a Dev-C++ compiler and sadly a Windows Vista OS.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        /*
        Write a program to check a C program for rudimentary syntax errors 
        like unbalanced parentheses, brackets and braces. Don't forget about quotes,
         both single and double, escape sequences, and comments. (This program is hard
          if you do it in full generality.)
        */
         
    
          
          enum { normalno, navodnici1, navodnici2, komentar };
          char array[100];
          int a=0;
          int x=0;
          int state=normalno;
          int pom=0;
          int okrugle;
          int uglate;
          int viticaste;
          okrugle=uglate=viticaste=0;
          int single_quote, double_quote, comment;
          single_quote = double_quote = comment=0;
          int pom5=0;
          enum {........, non_comment};
          int stanje;
          stanje=non_comment;
          
          
          while((a=getchar())!=EOF)
          {
             array[x]=a;
             x++;
          }
          
          array[x]='\n';
          x++;
          array[x]='\0';
          
          pom=x;
          
          for(x=0; x<pom; x++)
          {
                   
              if(array[x]=='/' && array[x+1]=='*')
              {
                  state=komentar;
                  comment++;
              }
              else if(array[x]=='*' && array[x+1]=='/')
              {
                  state=normalno;
                  comment--;
              }
              
              else if(state==navodnici1 && array[x]=='"' && state!=komentar)
              {
                  state=normalno;
              }
              else if(array[x]=='"' && state!=komentar)
              {
                  state=navodnici1;
              }
              else if(state==navodnici2 && array[x]=='\'' && state!=komentar)
              {
                   state=normalno;
              }
              else if(array[x]=='\'' && state!=komentar)
              {
                   state=navodnici2;
              }
              
              if(array[x]=='"' && stanje==comment)
              {
                 double_quote--;
                 stanje=non_comment;
              }
              if(array[x]=='"' && stanje==non_comment)
              {
                 double_quote++;
                 stanje=comment;
              }
              
              if(state==normalno)
              {
                 if(array[x]=='(')
                 okrugle++;
                 if(array[x]==')')
                 okrugle--;
                 if(array[x]=='[')
                 uglate++;
                 if(array[x]==']')
                 uglate--;
                 if(array[x]=='{')
                 viticaste++;
                 if(array[x]=='}')
                 viticaste--;
              }         
              
          }
          
          printf("%s", array);
          
          if(okrugle==0 && uglate==0 && viticaste==0)
          {
               printf("Nema sintaktickih gresaka.\n");
          }
          if(okrugle>0)
          printf("Okrugle zagrade se ne poklapaju.\n");
          if(uglate>0)
          printf("Uglate zagrade se ne poklapaju.\n");
          if(viticaste>0)
          printf("Viticaste zagrade se ne poklapaju.\n");
          if(comment>0)
          printf("Komentari se ne poklapaju.\n");
          if(double_quote>0)
          printf("Dvostruki navodni znakove se ne poklapaju");
          
          
      system("PAUSE");	
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by Tool View Post
    Title: Reading a C code text into an array
    [...]
    My question is, how can i modify this code so that the program does this:

    1. I enter the program in console mode - after that i write in the name of the C project which i want to check for syntactic errors.

    2. Then, the program puts characters in an array and checks for syntactic errors (the part of the code i dont have to change).
    Im kind of confused. The title suggests something about "reading something into an array", which is described in 2), which you say you dont have to do. So do you have to do 1)? Or is it 1) you dont have to do, and you have to do 2)?

    Also, isnt this code already reading characters into an array and checking it for basic C-syntax?

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Yeah it's reading characters into an array, but from the console; i enter the program in console mode, write in the text, which the program then stores to the array after i give him the EOF signal (ctrl+z).

    What i want to know is how i can modify the program so that when you enter it, it asks you to enter the name of the C project you want to check (which is stored somewhere on the disk), and after you write for example syntax.c it stores the characters from that project to the array.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    after i give him the EOF signal
    Its a boy? Congratulations!

    how i can modify the program so that when [...] you write for example syntax.c it stores the characters from that project to the array.
    So you want to read from a file, rather than from the command line (except of course the name of the file). Aside, its good to say "file" instead of "project", because "project" means nothing (what is a "project"?).

    Basically, instead of reading from the command line, you read from a file! Instead of making so many calls to read one character at a time, I would recommend to just read a block of data, i.e. strings. One of fgets - C++ Reference or fscanf - C++ Reference should work (both of the links have examples).

    Of course youll have to change the size of your array from 100, to whatever the filesize is (if you want to read in the entire file at once, which is probably a good idea when doing parsing/syntax checking--it might be slower but makes things less complicated). You can use fseek - C++ Reference and ftell - C++ Reference to determine the size of the file. Some code/pseudocode:
    Code:
    char filename[some size];
    // read in string and save to filename
    char * fileBuffer;
    FILE * file;
    file = //open filename for read
    int filesize;
    filesize = // determine filesize (and make sure to rewind file marker back to start of file)
    
    fileBuffer = // allocate filesize bytes of memory
    //use one of the functions I described above (or whatever method) to read in file and save to
    // "fileBuffer"--make sure not to exceed filesize bytes
    
    // continue rest of program, but use 'fileBuffer' instead of whatever variable you were using before
    
    // close file
    Last edited by nadroj; 11-01-2009 at 11:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. reading integers into arrays from text files
    By c_beginner in forum C Programming
    Replies: 6
    Last Post: 08-05-2004, 11:42 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM