Thread: make a preprocessor

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    10

    make a preprocessor

    Hello everybody, i'm a computing student and i'm working in making a php compiler.

    I have to make something like a preprocess to treat the #include sentences contained in the php scripts. I'm working with LEX/YACC and now i'm trying to make a function that can copy the code from a source file to another.

    Doing this, if the source file is large, i obtain a "segment violation".

    And I don't know how to append the read text instead of overwrite it...

    Code:
     #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	FILE *fuente;
    	FILE *destino;
        fuente = fopen(argv[1], "r"); 
    	destino = fopen("prueba.php", "w");
    
    	char *linea;
    	int tam = 999;
    
        if (fuente == NULL) {
           printf("El fichero fuente no existe\n");
           return(-1);
           }
        else{ 
       while (!feof(fuente)){
    	   fgets(linea,tam,fuente);
    	   fputs(linea,destino);	
    	   printf("%s\n",linea);
       }
        
       fclose(fuente);
       fclose(destino);
    
        }   
    
    }
    I'll be very pleased if someone could give me an idea or if anybody could tell me something about preprocessing that.

    Thanks!!!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char *linea;
    int tam = 999;
    fgets(linea,tam,fuente);
    You are telling to fgets that the linea pointer points to the buffer able to store 999 bytes
    But actually it contains some random adress.
    so when fgets starts writing there or GPF occurs or some random memory of your process is overwritten
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And I don't know how to append the read text instead of overwrite it...
    You should read about file open modes... i think "a" will suite you
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while (!feof(fuente))
    And read the FAQ on why using feof() to control a loop is bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What's to say that argv[1] isn't NULL? You should check the number of command-line parameters passed to the program. And whether or not the files could be opened.

    Also, declaring variables in the middle of a block is C99.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    I'checked all your comments (thank you guys) and I think my problem actually is about the memory and what vart has written...

    How can I resolve that? Do you know about a good guide or FAQ or something similar about files in C?

    Thank you very much

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why read the file in the first place? You can tell LEX to use a different source temporarily. The documentation contains a section on that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    Quote Originally Posted by CornedBee
    Why read the file in the first place? You can tell LEX to use a different source temporarily. The documentation contains a section on that.
    I've read this documentation and it seems too dificult and I have no much time, but it's ok, I've found a solution with a simple memory allocation.

    Thank you all, see ya

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  2. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  3. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  4. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM