Thread: [HELP] writing my own compiler

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    [HELP] writing my own compiler

    OK, so i'm writing my own compilier that will compile a script that controls a robot. An example of that script would be.

    Code:
    #Example Program
    RESET
    Forward 3
    pause 1
    headlights ON
    Headlights on
    reverse 3
    pause 2
    headlights off
    TurnRight 4
    TURNLEFT 4
    pAuSe 3
    # Do this three times in a row
    LOOP 3
         FORWARD 2
         Headlights ON
         Pause 2
         RESET
    End Loop
    RESET
    I kind of know how to go about doing this so far I have this much.

    - reads the file(script) to a series of strings
    - changes the entire script to lower case
    - tokenize the string
    - debug it by checking to make sure everything is in the right place

    my current problem....well it's not really a problem, but it's annoying. my code is kind of repatitive and i can't seem to think of a good function to replace the duplicating parts. The repeating part is:

    Code:
                 if (strcmp(tokenPtr, "reset") == 0)
                   printf ("RESET\n");
                 else if (strcmp(tokenPtr, "forward") == 0) 
                   printf ("FORWARD\n");
                 else if (strcmp(tokenPtr, "reverse") == 0)
                   printf ("REVERSE\n");
                 else if (strcmp(tokenPtr, "turnleft") == 0)
                   printf ("TURN LEFT\n");
                 else if (strcmp(tokenPtr, "turnright") == 0)
                   printf ("TURN RIGHT\n");
                 else if (strcmp(tokenPtr, "headlights") == 0)
                   printf ("HEADLIGHTS\n");
                 else if (strcmp(tokenPtr, "pause") == 0)
                   printf ("PAUSE\n");
                 else if (strcmp(tokenPtr, "loop") == 0)
                   printf ("LOOP\n");
                 tokenPtr = strtok(NULL, " ");
    I use it once when checking the script for errors, and i'll be using it again when i actually run the script once it is error free. Any suggestions?
    Last edited by bgmrk; 05-21-2008 at 10:10 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Strtok can help in tokenizing strings. However, it also damages the original input string. If that is a concern to you, then I have my own customized strtok you can try.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    4
    I would love to use it, but sadly everything has to be my own work in it.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, do you understand pointers and arrays?
    Those are essential to making your own tokenizer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    4
    i have been taught both, i understand arrays no prob. Pointers i am OK with.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The way strtok works is that it searches a string until it finds, say, a space. Then it replaces the space with '\0', thus truncating the string. Then you can copy the string using strcpy or appropriate string to number functions to get your data.
    Knowing pointers and arrays, it should not be difficult.

    My own strtok is an example of how it's done. You can have a look at it if you're stuck.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    4
    will do! thanks a bunch!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No problem. If you have problems with the code or get stuck on something you don't understand, check back and we'll give you some help!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I'd also suggest two things: Use a switch statement instead of lots of else ifs and there is a function called toupper() which will make a char into it's uppercase equivalent (or leave it as it is if it's already uppercase). Why not do that one first (it'll take care of anything like "RESET" and "FORWARD") then use a switch and search for the unusual ones like "TURN LEFT" -> "turnleft"), where you're adding or removing spaces.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, but switches doesn't work on string literals.
    A way to do that would be hash tables, but perhaps that's a little too much (too complex)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or, make a list or your "commands", e.g.:
    Code:
    const char *commands[] = { "forward", ... }; 
    
    for(i = 0; i < sizeof(commands)/sizeof(commands[0]); i++)
       if (strcmp(commands[i], your_str) == 0) break;
    ...
    The next extension of that is to have more of the parsing built into the table:
    Code:
    enum argtype { None, OnOff, Steps };
    struct command {
       char *cmd;
       enum argtype args;
    };
    
    const struct commands[] = {
    { "reset", None },
    { "headlights", OnOff }, 
    { "forward", Steps },
    ... 
    };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  3. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  4. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 06:42 AM
  5. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 11:28 AM