Re: Bison/Yacc with Objective-C ?
On Mar 12, 2010, at 9:00 AM, steven Hooley wrote: >> Bloomin' magic! Thanks, Filip, it worketh well. Hardy souls all. >> Tom W. > > Is it possible to see an example of how this might be used? I'd like > to understand Bison/Yacc better and i think an example relevant to > Cocoa / Objective-C would really help. > > Thanks, > Steven Steve, The interpreter I am working on is a work in progress, but here is a copy of the yacc/bison file as it exists as I start work on it today. It imports (not includes) the Cocoa Foundation and the semantic actions are written in Objective-C. This file is incomplete as it is only two days old and I am in the process of writing the semantic actions for the statement and expression types now. But you can still see the statement and expression types that will be handled by reading the grammar. The language itself is nearly complete. As it stands now it has only one shift/reduce conflict, that caused by the inherent ambiguity in the if/else statement, but the yacc/bison approach to resolving these conflicts by using shift is the proper handling in this case. (All this means is that the parser will make sure that else statements bind to the closest "then" part.) This is a typical example of using yacc/bison. The language defined is a general purpose programming language. During parsing a semantic tree of the program is built out of Objective-C objects (including the representations of different kinds of statements and expressions, values, types, modules and so on). Building the tree is what the semantic actions (the code with the $$ and $1 things scattered among the rules) do. Once this representation is built, the tree is validated for programming errors as any normal compiler would do (for example, undeclared variables, argument/parameter mismatches, assigning a number to a set, and so on) and then the program is run by an interpreter. The interpreter is similar to, though much simpler than, the Java virtual machine, as it executes the semantic trees as if they were instructions to a virtual machine, while managing the run time stack and the currently active subtree of symbol tables. I am writing the validator and interpreter in parallel with the parser, so there is a lot of stuff not visible here. This is all part of a Cocoa-based genealogical software application, where this part allows users to run their own algorithms on records in their databases or to generate their own specialty reports without having to modify the application itself. The genealogical records (eg, info about events, persons, families, and so on) are all kept in tree structures that can be represented in the application's databases or externally as XML or other types of structured text (GEDCOM is the format often chosen for genealogical software but it suffers from a variety of limitations.) Thus my earlier and not so positive reference to XSLT, which is the "official" (and much harder) way to do this, though I expect that an XSLT "program" would not be able to affect the database holding the records it processes. Needless to say I am extremely pleased that all I had to do was make the suffix to the yacc file be .ym instead of .y to get Xcode to do all the right things. I don't know whether bison itself was modified or whether the Xcode build rules just do the proper renaming, but the fact that someone at Apple worried about it and then made it work, is just one of those little things that makes me so content, one might even say smirkily self-satisfied, to be developing for the Mac. And then the fact that someone on this list (thanks, Filip) knew this was even better, as I have yet to find any actual DOCUMENTATION that states that this would happen! Easter eggs in spring. Tom Wetmore, Chief Bottle Washer, DeadEnds Software // Interpreter.ym // // Created by Thomas Wetmore on 3/10/2010. // Last changed on 3/12/2010. // Copyright 2010 DeadEnds Software. All rights reserved. %{ #import #import "TWInterpreter.h" #import "TWInterpStatement.h" #import "TWInterpExpression.h" #import "TWInterpModule.h" #import "TWInterpLexer.h" // For this parser every semantic type is an Objective-C object. #define YYSTYPE id extern TWInterpreterLexer* lexer; void yyerror (char* message); int yylex (); %} %token TYPE %right ASGNOP %left OROP %left ANDOP %left EQOP %left RELOP %left ADDOP %left MULOP %right UNARYOP %right NOTOP %token IDEN ICONS FCONS SCONS BCONS %token WHILE DO IF ELSE %token BREAK CONTINUE RETURN %% definitions : definition | definitions definition ; definition : declaration { [globalDeclarations addObject: $1]; } | module { [moduleTable setObject: $1 forKey: ((TWInterpModule*)$1).name];
Re: Bison/Yacc with Objective-C ?
> Bloomin' magic! Thanks, Filip, it worketh well. Hardy souls all. > Tom W. Is it possible to see an example of how this might be used? I'd like to understand Bison/Yacc better and i think an example relevant to Cocoa / Objective-C would really help. Thanks, Steven ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Bison/Yacc with Objective-C ?
On 10 Mar 2010, at 12:48, Timothy Stafford Larkin wrote: > You don't need to change the file extension, just a build setting. Change > "Compile Sources As" from "According to file type" to "Objective C". I find that confusing, especially when you have multiple yacc files to maintain. Some of them are in C, some in ObjC, C++, etc With the file-extention you see without opening what kind of language it uses. Filip > > Also, I've found that "@" as in @"String Constant" produces Bison confusion. > I use a #define so that I can write AT"String Constant". > > Tim Larkin > Abstract Tools > > On Mar 9, 2010, at 6:33 PM, Graham Cox wrote: > >> >> On 10/03/2010, at 9:00 AM, Thomas Wetmore wrote: >> >>> Yacc (and Bison) convert yacc files into C files. Instead I would like to >>> generate Objective-C files so I can use Cocoa containers classes and >>> NSString to get Unicode for free. I do see that bison can generate C++ >>> instead of C. Does anyone know whether there is any version of yacc or >>> bison that has been modified to generate Objective-C? Or if there is any >>> other parser generator available that can generate Objective-C. >> >> >> I haven't tried it, so this might be nonsense, but I think that YACC simply >> copies the emitted code from the source file to the final C file. That means >> that could be Objective-C code - you'd only need to change the file >> extension from .c to .m to compile it. As long as the emitted code is the >> only Objective-C and therefore not required to be parsed by YACC, I reckon >> it ought to work. >> >> --Graham >> >> >> ___ >> >> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) >> >> Please do not post admin requests or moderator comments to the list. >> Contact the moderators at cocoa-dev-admins(at)lists.apple.com >> >> Help/Unsubscribe/Update your Subscription: >> http://lists.apple.com/mailman/options/cocoa-dev/tsl1%40cornell.edu >> >> This email sent to t...@cornell.edu > > ___ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > http://lists.apple.com/mailman/options/cocoa-dev/filip%40code2develop.com > > This email sent to fi...@code2develop.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Bison/Yacc with Objective-C ?
You don't need to change the file extension, just a build setting. Change "Compile Sources As" from "According to file type" to "Objective C". Also, I've found that "@" as in @"String Constant" produces Bison confusion. I use a #define so that I can write AT"String Constant". Tim Larkin Abstract Tools On Mar 9, 2010, at 6:33 PM, Graham Cox wrote: > > On 10/03/2010, at 9:00 AM, Thomas Wetmore wrote: > >> Yacc (and Bison) convert yacc files into C files. Instead I would like to >> generate Objective-C files so I can use Cocoa containers classes and >> NSString to get Unicode for free. I do see that bison can generate C++ >> instead of C. Does anyone know whether there is any version of yacc or bison >> that has been modified to generate Objective-C? Or if there is any other >> parser generator available that can generate Objective-C. > > > I haven't tried it, so this might be nonsense, but I think that YACC simply > copies the emitted code from the source file to the final C file. That means > that could be Objective-C code - you'd only need to change the file extension > from .c to .m to compile it. As long as the emitted code is the only > Objective-C and therefore not required to be parsed by YACC, I reckon it > ought to work. > > --Graham > > > ___ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > http://lists.apple.com/mailman/options/cocoa-dev/tsl1%40cornell.edu > > This email sent to t...@cornell.edu ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Bison/Yacc with Objective-C ?
Bloomin' magic! Thanks, Filip, it worketh well. Hardy souls all. Tom W. On Mar 9, 2010, at 6:43 PM, F van der Meeren wrote: > You can just use your objc magic inside the curly braces... All you have to > do is rename your file to *.ym instead of *.y > Then all your files will be named *.m > > To call upon yacc with objc code is currently a no go. Therefor you still > need to call the basic yyparse methods, or add a special directive for the > bison compiler to generate push/pull methods. Those can be created and > deallocated at will and will run next to each other without any problems. > > Filip ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Bison/Yacc with Objective-C ?
On 10 Mar 2010, at 00:51, Graham Cox wrote: > > On 10/03/2010, at 10:46 AM, Jeff Laing wrote: > >> Yacc has to parse the emitted code enough to be able to replace references >> to $$, $1, etc with the corresponding tokens from the recognized grammar >> element. for example, a grammar I have lying around contains: >> [] > >> Yacc changes those $ expressions into references into its value stack. >> >> Thus, Objective-C may trip it up, but then again it may not. > > > I think they are just text substitutions, so it should be OK, e.g. > > { [someObject doSomethingWithValue:$1]; } > > I guess we need a brave soul to try it... ;-) Done and done! It works... Filip > > --Graham > > > ___ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > http://lists.apple.com/mailman/options/cocoa-dev/filip%40code2develop.com > > This email sent to fi...@code2develop.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Bison/Yacc with Objective-C ?
You can just use your objc magic inside the curly braces... All you have to do is rename your file to *.ym instead of *.y Then all your files will be named *.m To call upon yacc with objc code is currently a no go. Therefor you still need to call the basic yyparse methods, or add a special directive for the bison compiler to generate push/pull methods. Those can be created and deallocated at will and will run next to each other without any problems. Filip On 10 Mar 2010, at 00:33, Graham Cox wrote: > > On 10/03/2010, at 9:00 AM, Thomas Wetmore wrote: > >> Yacc (and Bison) convert yacc files into C files. Instead I would like to >> generate Objective-C files so I can use Cocoa containers classes and >> NSString to get Unicode for free. I do see that bison can generate C++ >> instead of C. Does anyone know whether there is any version of yacc or bison >> that has been modified to generate Objective-C? Or if there is any other >> parser generator available that can generate Objective-C. > > > I haven't tried it, so this might be nonsense, but I think that YACC simply > copies the emitted code from the source file to the final C file. That means > that could be Objective-C code - you'd only need to change the file extension > from .c to .m to compile it. As long as the emitted code is the only > Objective-C and therefore not required to be parsed by YACC, I reckon it > ought to work. > > --Graham > > > ___ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > http://lists.apple.com/mailman/options/cocoa-dev/filip%40code2develop.com > > This email sent to fi...@code2develop.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Bison/Yacc with Objective-C ?
On 10/03/2010, at 10:46 AM, Jeff Laing wrote: > Yacc has to parse the emitted code enough to be able to replace references to > $$, $1, etc with the corresponding tokens from the recognized grammar > element. for example, a grammar I have lying around contains: > [] > Yacc changes those $ expressions into references into its value stack. > > Thus, Objective-C may trip it up, but then again it may not. I think they are just text substitutions, so it should be OK, e.g. { [someObject doSomethingWithValue:$1]; } I guess we need a brave soul to try it... ;-) --Graham ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
RE: Bison/Yacc with Objective-C ?
> I haven't tried it, so this might be nonsense, but I think that YACC > simply copies the emitted code from the source file to the final C > file. Yacc has to parse the emitted code enough to be able to replace references to $$, $1, etc with the corresponding tokens from the recognized grammar element. for example, a grammar I have lying around contains: enum_mulable: enum_or_int | '(' enum_expr ')' { $$ = $2; } ; enum_or_int:INTEGER | '-' INTEGER { $$ = -$2; } | IDENT { int value; if (is_enum($1, &value)) { $$ = value; } else { lexerror_nl("%s is not a constant",$1); $$ = 0; } } ; Yacc changes those $ expressions into references into its value stack. Thus, Objective-C may trip it up, but then again it may not. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Bison/Yacc with Objective-C ?
Graham, Definitely not nonsense, and is the solution I will use unless I can find a more direct route. I have done this in the past for YACC to generate C++ on Windows systems. The headache with this solution is that the Xcode has a built-in rule that goes from YACC to C and tries to compile immediately. I will have to learn how to replace the built-in YACC rule with something else. Thanks again for your suggestion, Tom Wetmore On Mar 9, 2010, at 6:33 PM, Graham Cox wrote: > > On 10/03/2010, at 9:00 AM, Thomas Wetmore wrote: > >> Yacc (and Bison) convert yacc files into C files. Instead I would like to >> generate Objective-C files so I can use Cocoa containers classes and >> NSString to get Unicode for free. I do see that bison can generate C++ >> instead of C. Does anyone know whether there is any version of yacc or bison >> that has been modified to generate Objective-C? Or if there is any other >> parser generator available that can generate Objective-C. > > > I haven't tried it, so this might be nonsense, but I think that YACC simply > copies the emitted code from the source file to the final C file. That means > that could be Objective-C code - you'd only need to change the file extension > from .c to .m to compile it. As long as the emitted code is the only > Objective-C and therefore not required to be parsed by YACC, I reckon it > ought to work. > > --Graham > > ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Bison/Yacc with Objective-C ?
On 10/03/2010, at 9:00 AM, Thomas Wetmore wrote: > Yacc (and Bison) convert yacc files into C files. Instead I would like to > generate Objective-C files so I can use Cocoa containers classes and NSString > to get Unicode for free. I do see that bison can generate C++ instead of C. > Does anyone know whether there is any version of yacc or bison that has been > modified to generate Objective-C? Or if there is any other parser generator > available that can generate Objective-C. I haven't tried it, so this might be nonsense, but I think that YACC simply copies the emitted code from the source file to the final C file. That means that could be Objective-C code - you'd only need to change the file extension from .c to .m to compile it. As long as the emitted code is the only Objective-C and therefore not required to be parsed by YACC, I reckon it ought to work. --Graham ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Bison/Yacc with Objective-C ?
I'm porting an old interpreter from a C-based program that used yacc to parse report generating programs and convert them into interpretable programs that could then generate reports. A system akin to XSLT but using a procedural language rather XML as a programming language, ahem. Yacc (and Bison) convert yacc files into C files. Instead I would like to generate Objective-C files so I can use Cocoa containers classes and NSString to get Unicode for free. I do see that bison can generate C++ instead of C. Does anyone know whether there is any version of yacc or bison that has been modified to generate Objective-C? Or if there is any other parser generator available that can generate Objective-C. Thanks, Tom Wetmore, Chief Bottle Washer, DeadEnds Software ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com