2

When I generate my parser with bison, I obtain this warning:

warning: stray `@'

But that is because I have some legal Objective-C code containing @, for instance this is one of the rules having the warning:

file : axiom production_rule_list    { NSLog(@"file"); }
     ;

Is there any risk to use @ in the code? If not, how to tell bison that it is a legitimate use of @?

Thanks in advance.

Zaphod
  • 6,758
  • 3
  • 40
  • 60
  • Check out this thread: http://forums.macrumors.com/showthread.php?t=711554 – Nate Chandler Dec 29 '12 at 18:28
  • Thanks but the problem is not during gcc compiling, but when I generate the parser with bison... – Zaphod Dec 29 '12 at 18:49
  • 1
    possible duplicate of [Integrating Bison/Flex/Yacc into XCode](http://stackoverflow.com/questions/6296758/integrating-bison-flex-yacc-into-xcode) – rob mayoff Dec 29 '12 at 20:36
  • @robmayoff My problem is just the bison warning, not the integration of Flex and Bison into Xcode. – Zaphod Dec 29 '12 at 21:55
  • @Zaphod But the bison warning is resolved with the information in the linked post. You just have to rename your yacc file to use the `.ym` extension. – Nikolai Ruhe Jan 04 '13 at 17:31
  • @NikolaiRuhe My files are already named with the `.ym` extension, and the warning is still here because I do not use Xcode to compile it but a command line script. My question, by the way, never mentioned Xcode but `bison`. So the integration of `flex` and `bison` in Xcode (right click an so on) is not my concern. – Zaphod Jan 04 '13 at 22:42

3 Answers3

2

The message is just a warning. You can ignore it. If you're using Xcode, it won't even show you the warning in its Issue Navigator.

Rename your Bison input file to have a .ym extension instead of a .y extension. That tells Xcode that it's a grammar with Objective-C actions.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Definitely the best answer if true – Jesse Black Dec 29 '12 at 20:41
  • My files are named `.ym` but I launch bison by a script: `bison --defines=system.define.h -o system.yacc.m system.ym` not using Xcode. – Zaphod Dec 29 '12 at 21:38
  • Ok. Well. But how does Xcode tell bison it's Ocjective-C? There's no bison command line I know for that, is there? Or does Xcode use its own bison? – Zaphod Dec 29 '12 at 21:50
  • There's no special Bison flag. It just warns about the `@` but passes it through anyway. Xcode doesn't do anything special; the `.ym` extension just tells it to compile the output of Bison as Objective-C instead of as C. – rob mayoff Dec 29 '12 at 22:06
2

If you want to suppress the warning, you can use a #define AT @.

The code in the braces is just copied, apart from replacing the $… sequences with the code to give the relevant token. This appears to work fine with Objective-C, although if you're using ARC, you might need to do some digging (or just add extra blocks (in the C sense)) to make sure that objects are freed as soon as possible.

user1567459
  • 121
  • 1
0

As per the documentation in Actions - Bison 2.7, it appears that the code between the curly braces is expected to be C code. As such I doubt that you can use objective-c constructs there.

However you could create an external C function to do the work for you like:

Logit(char* message)
{
  NSLog(@"%s",message);
}

And use that in the Bison action

file : axiom production_rule_list    { Logit("file"); }
     ;
Peter M
  • 7,309
  • 3
  • 50
  • 91
  • Ok, thank you for this information. I will stick to C within the curly braces. But it's only too bad, because putting Objectve-C is working in the generated parser... – Zaphod Dec 29 '12 at 19:51
  • But the `NSLog` still expects an `NSString` as format, so it should be `NSLog(@"%s",message)`. – Martin R Dec 29 '12 at 20:10
  • This answer is wrong--you can use objective-c inside the braces. The message is just a warning. – nielsbot Aug 08 '14 at 21:50