• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Expression parsing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Expression parsing


  • Subject: Re: Expression parsing
  • From: Allan Odgaard <email@hidden>
  • Date: Sun, 18 Jul 2004 05:37:46 +0200

On 18. Jul 2004, at 1:46, Theodore H. Smith wrote:

I want to parse simple expressions, mainly with the contstants pi and phi in it. So I can enter things like "pi/3", or "phi+2", into some text fields. Whats a good way to do this?

How "simple" is the grammar going to be? If you're going to extend it over time, allow grouping using parenthesis, precedence etc. then the "good way" is to write the tokenizer using lex/flex and/or the grammar with yacc/bison (all available with man files on your system -- I believe that there is even an example of a calculator in the flex man file, which you could just use).

If you do not like the preprocessing faces you'll introduce into your build process with lex/yacc, then there is also Spirit: http://spirit.sourceforge.net/ -- this is a C++ library where you can write the grammar in an approximated EBNF form, and it'll generate the code compile-time through the use of templates, quite cool! ;) But it'd then require you to use ObjectiveC++ (which is not always well received, don't ask me why).

I'm thinking of just parsing it, but I'm wondering if theres a built in class for this kind of thing?

The closest you get with Cocoa is NSScanner, which you can use as a tokenizer, but that'd be a brute-force tokenizer a la:

struct token_t
{
enum type_enum { phi, pi, plus, integer, ... };

type_enum type;
int value; // only if 'type == integer'
};

token_t next_token (NSScanner* s)
{
int value;
if([s scanString:@"phi" intoString:nil)
return (token_t){ token_t::phi };
else if([s scanString:@"pi" intoString:nil)
return (token_t){ token_t::pi };
else if([s scanString:@"+" intoString:nil)
return (token_t){ token_t::plus };
...
else if([s scanInteger:&value])
return (token_t){ token_t::integer, value };
else
return (token_t){ token_t::end_of_tokens };
}

But you'd still need to write a parser to build the parse tree based on the tokens -- something I'd probably write myself but critizie everyone else for not using standard tools to solve the job! ;)

Unrelated, it'd be cool to have parsing capabilities in the language, similar to Rebol <http://rebol.com/docs/core23/rebolcore-15.html>, as this problem comes up so often, but I guess Spirit is a start, and at least it seems we get regex in C++0x... ;)
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Expression parsing (From: "Theodore H. Smith" <email@hidden>)

  • Prev by Date: Cocoa, OpenGL, and Display Lists
  • Next by Date: A Couple of Daemon Questons
  • Previous by thread: Re: Expression parsing
  • Next by thread: Re: Expression parsing
  • Index(es):
    • Date
    • Thread