Skip to content
A demo project showing the use of yacc and lex (bison, flex) in a Xcode project
Objective-C C
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
Parser Test.xcodeproj
Parser Test

Parser Test with Xcode

Xcode can handle bison and flex definition files. Xcode can even compile the generated files as Objective-C making it easier to bridge a parser into Objective-C. Pretty nice I think.

This demo shows a simple calculator. tokenizer.lm contain the parts for divinding the character stream into parts for the parser which is in the parser.ym file. The added m on the extension is that detail that makes Xcode compile the generated files as Objective-C files.

To make it a little bit more interesting I added identifiers that is dynamically handled between the tokenizer and parser. In the tokenizer you can see that it will be set as a retained string. The parser only knows about pi at the moment and everything else will have the value 0.0

There are two possible places for a identifier string to be released, either in the last part of this rule

number : INTEGER { $$ = $1; }
| FLOAT { $$ = $1; }
| IDENTIFIER { if ([$1 isEqualToString:@"pi"]) $$ = M_PI; else $$ = 0.0; [$1 release]; }

or the defined destructor

%destructor { [$$ release]; } IDENTIFIER 

The defined destructor is used if the parser need to clean up some intermediate handling, by error or not.

One caveat I stumbled upon was that I couldn't get "[[" to work in the tokenizer, which seem to use that for some internal marker. So instead I use "[ [" which may look strange. The line in the tokenizer is this

[a-zA-Z]+ { yylval.identifier = [ [NSString stringWithFormat:@"%s", yytext] retain]; return IDENTIFIER; }

Now I wish I had a task to solve with my own language...

You can’t perform that action at this time.