enum and typedef help

Discussion in 'Mac Programming' started by perrien, Jul 2, 2010.

  1. perrien macrumors newbie

    Apr 26, 2008
    I'm writing a program (Cocoa, Obj-C) where I need to classify words by their part of speech. I know this probably needs to be done via enums and typedefs but am unable to figure out how to get it all working.

    Where are some good examples of how they work? I'm having problems understanding the usage difference between X and Y in the following:

    enum X {noun, adjective, verb} Y;

    Also, "google" for instance, can be a noun or a verb, how would I set that up and test it?

    partOfSpeech theWordGoogle = noun & verb;

    if (theWordGoogle == noun)
    // do something
    if (theWordGoogle == verb)
    // do something else

    etc. Any pointers in the right direction would be helpful.
  2. Bill McEnaney macrumors 6502

    Apr 29, 2010
    In some contexts,"Google" is a noun. In other contexts, it's a verb. So you'll need context to help you identify "Google's" part of speech. Identifying a word's part of speech can be challenging, even when you're writing in Prolog, a language designed for natural language processing.

    Link is a long C program for parsing English sentences. You type a sentence, the computer parses it, and then it draws a diagram showing the part of speech of every word along with other information.

    I don't mean to discourage you. But were I to write a parser, I would write it in a language for logic programming, Prolog or Mercury, say. If logic programming is new to you, you'll need to learn to think very declaratively about the problem you need to solve. The syntax of Prolog and Mercury will be strange, too, because it differs almost completely from the syntax of any C-like language.

    I'll do some research because because I probably can find a program you can use as something like a design pattern.
  3. gnasher729 macrumors P6


    Nov 25, 2005
    I'd start by getting a good book about C.

    In "enum X {noun, adjective, verb} Y;" you are declaring an enumerated type named X with three values noun = 0, adjective = 1, and verb = 2, and a variable Y of type X.

    The "&" operator is a bitwise and - that won't be useful to you (again, get a good book about C). Then look at the "bitwise or" operator "|". That might come useful.
  4. perrien thread starter macrumors newbie

    Apr 26, 2008
    The more overriding question here is about the enum and typedef though. I am writing a parser of sorts but I'll take care of the logic and work there. Specifically I'm having questions on how typedefs and enums work, not just for this project but many others also.

    I'll try and look into some C books but was hoping for some online (more correctly, free) options.
  5. Bill McEnaney macrumors 6502

    Apr 29, 2010
    Here's some information about defining enumerated types in C (http://msdn.microsoft.com/en-us/library/whbyts4t(VS.80).aspx). The document says that an integer corresponds to each element of an enumerated type. Zero corresponds to the first element of that type, 1 corresponds to the second element of it, and so forth. Maybe, if you decide not to use enumerated types, you could define constants instead.

    #define NOUN 'n'
    #define VERB 'v'
    #define ADJECTIVE 'a'
    #define PREPOSITION 'p'

  6. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Exactly what do you mean by "how typedefs and enums work"? Are you looking for an explanation of how to use them, or are you looking for an explanation of the mechanics of what the compiler is doing with them?

    There are any number of C tutorials that cover typedef and enum. These would explain how to use them.

    Example search terms:
    C tutorial enum

    Example search result:

    For the mechanics of what the compiler is doing, I suggest starting with the language spec, because that will outline the syntax and semantics in excruciating detail. Then maybe take a look at a compiler's source.
  7. subsonix macrumors 68040

    Feb 2, 2008
    For the enums I would just do like this and be done with it:

    enum {noun, verb, adjective};
    For combinations in assignments use the bitwize or | operator as suggested, '&' might become useful if you need to query those bits later. There is plenty of more indepth information about it available on the internet and in C books.
  8. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Use a short int, #define some values that are powers of 2 so each value corresponds to a bit in the short. You have 16 bits with your short, so unless you go nuts with the parts of speech this should be fine. With an enum with one value that uses 0, you can't & and | things, and if you did the result wouldn't be a valid enum value. You could make the first three enum members valued at 0(for none), 1, 2, 4, for each single part of speech then build out the other permutations (2 to the power of the number of options), but it seems like a pain. I'd just use an integer type as a set of bits with #defines.

  9. subsonix macrumors 68040

    Feb 2, 2008
    That's completely true, I did sort of add the last bit after the enums. To make them work together the enums have to be in the power of 2 with 1 as the lowest legal number.
  10. chrono1081 macrumors 604


    Jan 26, 2008
    Isla Nublar
    This thread makes me feel like a bad person because I never use enums or typedefs :eek:

Share This Page