Help with with Lisp Reader Technique

This is a discussion on Help with with Lisp Reader Technique within the C Programming forums, part of the General Programming Boards category; Hi I am developing a game that uses Lisp based text files to store data. Although I can't seem to ...

  1. #1
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35

    Help with the Lisp Reader Technique

    Hi I am developing a game that uses Lisp based text files to store data. Although I can't seem to figure out how to right the code. Here is an example text file that I would like to load. The number of level sets will increase as users make their own with the built in level editor, so I need a way of dynamicly loading all of them.
    Code:
    ;; TuxBlocks Level Sets
    (numofsets 3
      (levelset 1
        (name "Classic")
        (location "classic/classic.tbls")
        (levels 11))
      (levelset 2
        (name "Confusion")
        (location "confusion/confusion.tbls")
        (levels 0))
      (levelset 3
        (name "Insanity")
        (location "insanity/insanity.tbls")
        (levels 1)))
    
    ;; EOF ;;
    Could somebody please help understand how to use the lisp reader technique?
    Last edited by tuxinator; 05-03-2006 at 09:20 PM.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    So your question is, "How do I write a LISP parser"?
    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.

  3. #3
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    Yes.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, that's pretty complicated, especially for someone who can't read binary files.

    I don't know anything about it. There might be a library like libxml2 that lets you parse LISP files.

    Just out of curiosity, why are you using LISP?
    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.

  5. #5
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    Well I'm not using LISP itself. It's more of a LISP based syntax. My reasoning is because it provides an easy to write data storage file. Have a look at the supertux source code. It uses the same syntax that I am using for every non-C textfile that it loads, and it seems to me to be one of the best ways of storing huge amounts of data.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You could use XML for that kind of thing.

    If you really want your own parser . . . I guess I'd use a recursive function that would look for '('s, ')'s, tokens and '='s.
    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.

  7. #7
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    I haven't tried this yet but I think I figured out what I need to do. If anybody is for sure on what to do then please say so. I'll report back as soon as I'm done with this idea. If it works then I will post the code as well.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  8. #8
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Your using this lisp syntax in a very xml-ish way. Id say your best bet is to use xml with a free library for parsing. no need to reinvent the wheel

  9. #9
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    The problem with downloading a free parser is it is usually something you have to install wich would mean putting it as the requirements. So far my game only requires SDL and SDL_image support and nothing else. I hope to keep it this way because the game itself is way to simple of a game to be having a lot of requirements.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  10. #10
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    It would be a bit of work, but reinventing the wheel is always great academically. You could probably do a keyword driven parsing rather than having to read a whole book.
    Basically:
    Code:
    Token input = GetToken();
    
    if (input == TOK_OPAREN)
         push input;
    else if (input == TOK_CLPAREN)
    {
        stackVal = pop;
        if (stackVal != TOK_OPAREN)
            Error("expected open parenthese\n");
    }
    else
    {
        switch(input)
        {
        case TOK_NUMOFSETS:
            if ((input = GetToken()) != TOK_NUMBER)
                Error("expected a number\n");
            else
                numOfSets = input.value;
        break;
    
        case other tokens ... etc.
    
        default:
            Error("Unknown keyword");
        }
    }
    Where GetToken() is something like:
    Code:
    string = GetCharFromFile();
    
    if (string == '(')
        return(TOK_OPAREN);
    else if (string == ')')
        return(TOK_CLPAREN);
    else
    {
        while (not a space and not a ')' and not a '(')
            string += GetCharFromFile();
    
        compare string to your stuff and return the correct token
    }
    Not the most efficient but simple and fairly easy to implement.

    edit:
    That input.value thing, make Token a struct and when you recognize a number just convert the string with atoi and put the result in input.value
    Last edited by valis; 05-04-2006 at 10:03 PM.

  11. #11
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    This seems like an interesting concept. Of course it'll require a few tweaks here and there to get it to work. But I think this should work, at least for what I need it to do. Unfortunately I am to busy with homework to do it at the current moment and it might take me a little bit to get it all coded and tested. So I'll let you know if it worked or not. In the mean time if anybody has any more suggestions or notices flaws with this please post it.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by tuxinator
    The problem with downloading a free parser is it is usually something you have to install wich would mean putting it as the requirements. So far my game only requires SDL and SDL_image support and nothing else. I hope to keep it this way because the game itself is way to simple of a game to be having a lot of requirements.
    Many libraries just require you to link to them, and even if it does require a DLL or something you could distribute the DLL along with SDL.DLL and SDL_Image.DLL and no one would know the difference.
    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.

  13. #13
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    Ok I've got it to work now. Except I can't figure out how to copy the temporary value of the object into a permanent location without the temporary destination being a pointer and without getting a segmentation fault error. Basicly I need to copy the value over to a variable that I will use later on. I have tried using "strcpy" but it gives me a segmentation fault error. By the way the type of the two strings that I am working with is "char *string". Everything I've found so far only works with type "char string[MAX_LENGTH]".
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  14. #14
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35
    Originally Posted by valis
    It would be a bit of work, but reinventing the wheel is always great academically.
    This is exactly why I'm writing my own parser. Is to learn how to do it, not just to get a task done.
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  15. #15
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    Often seg faults occur from simple mistakes, did you accidentally leave out or comment out the code that allocates space for your string before you copy it? Could we have a small chunk of the code to look over?
    Also, are you referring to the strings following stuff like 'location' and 'name'?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lisp Reader Technique
    By tuxinator in forum Game Programming
    Replies: 2
    Last Post: 05-04-2006, 12:34 AM
  2. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM
  3. Lisp
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 08-26-2003, 12:41 PM
  4. Lisp
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-18-2002, 03:41 PM
  5. IDEA: LISP Interpreter
    By ygfperson in forum Contests Board
    Replies: 4
    Last Post: 09-13-2002, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21