I'm trying to use YACC with xcode, which is natively supported, and for that effect I found this sample which is working nicely. My idea is to build my own grammar, so I started experimenting with the project to see if it would support what I need. As such, I created the following interface:
.h
#import <Foundation/Foundation.h>
@interface HYPLangNodeNP : NSObject
@end
.m
#import "HYPLangNodeNP.h"
@implementation HYPLangNodeNP
@end
Yes, it's just an empty implementation. Then I made just two changes to the grammar:
1) Add the HYPLangNodeNP import
%{
#import "MessageBlocks.h"
#import "HYPLangNodeNP.h"
int yylex(void);
void yyerror(char *s);
%}
2) Add HYPLangNodeNP to the type %union
%union {
float value;
NSString *identifier;
HYPLangNodeNP *node;
}
The project fails to compile with Unknown type name 'HYPLangNodeNP'
. Does anybody know how to solve?
Edit:
I don't know the YACC version, but it's whatever version ships with Xcode 6.3. The following is the log I get:
CompileC /Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Intermediates/Parser\ Test.build/Debug-iphonesimulator/Parser\ Test.build/Objects-normal/i386/ViewController.o Parser\ Test/ViewController.m normal i386 objective-c com.apple.compilers.llvm.clang.1_0.compiler cd "/Users/RafaelSoares/Google Drive/Hype/ParserTest-master " export LANG=en_US.US-ASCII export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin" /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x objective-c -arch i386 -fmessage-length=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -std=gnu99 -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-implicit-atomic-properties -Wno-receiver-is-weak -Wno-arc-repeated-use-of-weak -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wno-empty-body -Wuninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-shorten-64-to-32 -Wpointer-sign -Wno-newline-eof -Wno-selector -Wno-strict-selector-match -Wno-undeclared-selector -Wno-deprecated-implementations -DDEBUG=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.3.sdk -fexceptions -fasm-blocks -fstrict-aliasing -Wprotocol -Wdeprecated-declarations -g -Wno-sign-conversion -fobjc-abi-version=2 -fobjc-legacy-dispatch -mios-simulator-version-min=8.3 -iquote /Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Intermediates/Parser\ Test.build/Debug-iphonesimulator/Parser\ Test.build/Parser\ Test-generated-files.hmap -I/Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Intermediates/Parser\ Test.build/Debug-iphonesimulator/Parser\ Test.build/Parser\ Test-own-target-headers.hmap -I/Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Intermediates/Parser\ Test.build/Debug-iphonesimulator/Parser\ Test.build/Parser\ Test-all-target-headers.hmap -iquote /Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Intermediates/Parser\ Test.build/Debug-iphonesimulator/Parser\ Test.build/Parser\ Test-project-headers.hmap -I/Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Products/Debug-iphonesimulator/include -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -I/Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Intermediates/Parser\ Test.build/Debug-iphonesimulator/Parser\ Test.build/DerivedSources/i386 -I/Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Intermediates/Parser\ Test.build/Debug-iphonesimulator/Parser\ Test.build/DerivedSources -F/Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Products/Debug-iphonesimulator -include /Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Intermediates/PrecompiledHeaders/Parser\ Test-Prefix-gydtbjgblnmtxcahwzhxkvmnznwf/Parser\ Test-Prefix.pch -MMD -MT dependencies -MF /Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Intermediates/Parser\ Test.build/Debug-iphonesimulator/Parser\ Test.build/Objects-normal/i386/ViewController.d --serialize-diagnostics /Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Intermediates/Parser\ Test.build/Debug-iphonesimulator/Parser\ Test.build/Objects-normal/i386/ViewController.dia -c /Users/RafaelSoares/Google\ Drive/Hype/ParserTest-master\ /Parser\ Test/ViewController.m -o /Users/RafaelSoares/Library/Developer/Xcode/DerivedData/Parser_Test-ahryrrrgjyqtbhenhxphagqdgppf/Build/Intermediates/Parser\ Test.build/Debug-iphonesimulator/Parser\ Test.build/Objects-normal/i386/ViewController.o
In file included from /Users/RafaelSoares/Google Drive/Hype/ParserTest-master /Parser Test/ViewController.m:5: /Users/RafaelSoares/Google Drive/Hype/ParserTest-master /parser.ym:14:5: error: unknown type name 'HYPLangNodeNP' HYPLangNodeNP * nodeNP; ^ /Users/RafaelSoares/Google Drive/Hype/ParserTest-master /parser.ym:15:5: error: unknown type name 'HYPLangNodeVP' HYPLangNodeVP * nodeVP; ^ /Users/RafaelSoares/Google Drive/Hype/ParserTest-master /parser.ym:16:5: error: unknown type name 'HYPLangNode' HYPLangNode * node; ^ /Users/RafaelSoares/Google Drive/Hype/ParserTest-master /parser.ym:17:5: error: unknown type name 'HYPLangSentence' HYPLangSentence * sentence; ^ /Users/RafaelSoares/Google Drive/Hype/ParserTest-master /Parser Test/ViewController.m:240:34: warning: unused variable 'child' [-Wunused-variable] NSMutableArray * child = [childsVP[i] getChilds]; ^ 1 warning and 4 errors generated.
I have looked at this and have some answers, but unfortunately, not a complete working example in the time available. I am very familiar with lex & yacc but have not worked with Objective-C before. This was my first Objective-C exercise I'm afraid, and it was my weakness in that part that let me down. Perhaps your better knowledge in that area will allow you to complete the task.
The first part of the problem, as hinted at by @Ewan Mellor, is explained in the bison manual* where it indicates that yacc generates code in the wrong order for some languages/compilers. This is true for Objective-C which is what causes the compilation error you are receiving. This means, specifically, that the %union
construct of yacc is difficult to use in conjunction with Objective-C objects (as you have discovered).
There is another way of solving this problem as shown (at the end of) this article.
One uses the YYSTYPE
macro to replace the type used by yacc instead of the %union
.
I made the following changes to use this method:
In MessageBlocks.h:
@interface HYPLangNodeNP : NSObject
@end
@interface YYresultType : NSObject
- (float) value;
- (NSString *) identifier;
- (HYPLangNodeNP *)node;
- (void) setvalue: (float)input;
- (void) setidentifier: (NSString *)input;
- (void) setnode: (HYPLangNodeNP *)input;
@end
In MessageBlocks.m:
@implementation HYPLangNodeNP : NSObject
int dummy;
@end
@implementation YYresultType : NSObject
float value;
NSString *identifier;
HYPLangNodeNP *node;
- (float) value {
return value;
}
- (NSString *) identifier {
return identifier;
}
- (HYPLangNodeNP *) node {
return node;
}
- (void) setvalue: (float)input {
value = input;
}
- (void) setidentifier: (NSString *)input {
identifier = input;
}
- (void) setnode: (HYPLangNodeNP *)input {
node = input;
}
In tokenizer.lm:
[0-9]+\.[0-9]* { [yylval setvalue: [float atof(yytext)]]; return FLOAT; }
[0-9]+ { [yylval setvalue: [float atof(yytext)]]; return INTEGER; }
[a-zA-Z]+ { [yylval setidentifier : [ [NSString stringWithFormat:@"%s", yytext] retain]; return IDENTIFIER; }
In Parser.ym:
%{
#import "MessageBlocks.h"
int yylex(void);
void yyerror(char *s);
#define YYSTYPE YYresultType
%}
/*
%union {
float value;
NSString *identifier;
HYPLangNodeNP *node;
}*/
...
and so on.
I'm still getting Objective-C compilation errors, so I'm no further forward than you were, but I'll keep working on it... but probably it won't be useful to you.
This section has been concerned with explaining the advantages of the four Prologue alternatives over the original Yacc Prologue.
It explains the weakness of the yacc code ordering and how bison overcomes that with the %code
directive. It is yacc and not bison that is built in to Xcode. It would be possible to replace yacc with bison on the system to overcome this deficiency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With