Thread: #define question

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Thanks Elysia, you mention interpreter and parser.
    I have thought about this but are not really sure what it is.

    I think an interpreter can work in such a way that it goes through the "own" code and change "own syntax" to C++ syntax or if pointers could be involved in any way.

    Is an interpreter a separat application that change "own" code to "C++ code" that you will use in the Main program. Am I thinking right ?

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An interpreter is basically an application that parses some text and evaluates it and does something with it.
    Javascript and PHP are, for example, interpreted languages, since a parser parses the code and translates it into executable code.

    You could just allow your "workers" to write pseudo code or some criterias or whatever and then parse and do something with this. This is an interpreter.
    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. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think dudeomandude has the right idea. Build a simple GUI that you can enter some criteria in, and let that run.

    But you can make it more complex by building a parser for a simple (or complex) language.

    Using #define to create a criteria in a header file that is later compiled into an application is absolutely the wrong way to solve the problem. It is going to be error prone for one thing. Imagine what happens if someone does a typo in the macro definition, and then the compile goes wrong - are they going to understand the error message of this??
    Code:
    #define Criteria Number == 5;
    --
    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.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    That sounds very interesting actually. I have to ask anyway.
    If I take this just as an example:

    If I have this:
    Code:
    int Number1 = 5;
    int Number2 = 6;
    And then I will write this text below in a textBox:
    Where the C++ code will look like this: if (parsed text in textBox) {Action;}

    if
    Code:
    (Number1 == 5 && Number2 == 6)   //This is written inside a textBox
    {
    Action;
    }

    So I might wonder if it is possible to use a textBox and from there parse out every word and convert the 5 and 6 to ints etc.
    Last edited by Coding; 03-12-2008 at 03:07 PM.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, that can be done.

    What may be a little bit difficult is to translate "Number1" so that it gets compared with the variable Number1 in your code. But it's all doable.

    --
    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.

  6. #21
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    What technique should I use to do this. I dont really know what the first step could be to do.

    Because the "criteria" written in the textBox has to be replaced with executable code.
    For me it sounds like I have to have somekind of "list with emty spaces" where I put the parsed code in something like this below. I dont know if I am thinking right here and then let the criteria always go through this recognicible list.

    I just dont know where the parsed code will be put and recogniced within the C++ code otherwise.
    Code:
    Number1
    == 
    5
    &&
    Number2
    == 
    6
    Last edited by Coding; 03-12-2008 at 03:31 PM.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you want an easy parser, the easiest way would be to require an exact layout. Say for example, you want to parse this:
    if (Number1 == 1)

    First you would extract all characters until the whitespace (between "if" and "("), lookup a function through a map and call it. This is the "if" parser function.
    The if parser function would then potentially check for a "(", otherwise scream parse error.
    Then it would extract everything until whitespace (between "Number1" and "=="). It may interpret this as a variable.
    It then parses until a new whitespace and, in this example, finds "==". Looks up via a map and calls "equality function". Equality function reads the number until ")" (or potentially up until a newline), and then checks if a this variable equals this value and return true or false.
    The "if function" return true if the "action" inside the if should be executed or not and the main parser behaves accordingly.

    This is an example of how to write a parser, but I'm too lazy to write any pseudo code right now, sorry.
    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.

  8. #23
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    No problem! Thanks, this is anyway letting me know the picture of how it could be done. I will check up how maps works as I dont know that and begin one step at a time.
    So it is kind of communication beween parsed values etc to be recogniced in the map.

    Using a method like this, will it slow down the running of the code for example if you read from .txt files and this criteria is refering
    to values red in the files ?
    Last edited by Coding; 03-12-2008 at 03:53 PM.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The maps makes it easier and, probably faster, as well. Otherwise you could continue parsing and make functions or big if nests, but that's nasty. And a parser can be quite slow.
    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.

  10. #25
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    This is leading me in on the right track, I will read about maps now

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Coding View Post
    Using a method like this, will it slow down the running of the code for example if you read from .txt files and this criteria is refering
    to values red in the files ?
    Reading from files is always a little slower, but it won't make much of a performance impact for this, I would think.

    On a side note, C++ offers an implementation of a map in std::map.
    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.

  12. #27
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I tried to write std::map in my code but couldnīt find this member.
    Perheps I have to include something.
    Last edited by Coding; 03-12-2008 at 04:13 PM.

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Include <map>
    Also be aware that you're treading into the Managed/Native boundary again. std::map is for native and not managed, but if you do this on C++ .NET, you'll find yourself with String^ a lot of which won't work at all with std::map. I'm sure there is a managed map, but I don't know its name.
    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.

  14. #29
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    That worked, yes I am aware of the String^
    I wrote this and found it: map::map

    Perheps this is the managed map.

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is it not located under std? If it isn't, then it's not C++'s map, so it might well be a managed map.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  2. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  3. macro (#define) question
    By Darrok in forum C++ Programming
    Replies: 30
    Last Post: 12-20-2001, 05:01 PM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM