我正在尝试将 YACC 与 xcode 一起使用,这是 native 支持的,为此我发现了 this运行良好的示例。我的想法是构建自己的语法,因此我开始尝试该项目,看看它是否能支持我所需要的。因此,我创建了以下界面:
.h
#import <Foundation/Foundation.h>
@interface HYPLangNodeNP : NSObject
@end
.m
#import "HYPLangNodeNP.h"
@implementation HYPLangNodeNP
@end
是的,这只是一个空的实现。然后我对语法做了两处更改:
1) 添加 HYPLangNodeNP 导入
%{
#import "MessageBlocks.h"
#import "HYPLangNodeNP.h"
int yylex(void);
void yyerror(char *s);
%}
2) 将 HYPLangNodeNP 添加到类型 %union
%union {
float value;
NSString *identifier;
HYPLangNodeNP *node;
}
项目无法编译,未知类型名称“HYPLangNodeNP”
。有谁知道怎么解决吗?
编辑:
我不知道 YACC 版本,但它是 Xcode 6.3 附带的任何版本。以下是我得到的日志:
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.
最佳答案
我已经看过这个并得到了一些答案,但不幸的是,在可用的时间内没有完整的工作示例。我对 lex 和 yacc 非常熟悉,但之前没有使用过 Objective-C。恐怕这是我第一次进行 Objective-C 练习,正是我在这方面的弱点让我失望了。也许您在该领域的更好知识将使您能够完成任务。
正如 @Ewan Mellor 所暗示的,问题的第一部分在 bison 手册中进行了解释*,其中表明 yacc 对于某些语言/编译器以错误的顺序生成代码。对于 Objective-C 来说也是如此,这就是导致您收到编译错误的原因。具体来说,这意味着 yacc 的 %union
构造很难与 Objective-C 对象结合使用(正如您所发现的)。
还有另一种方法可以解决这个问题,如(末尾)this article所示。 .
其中一个使用 YYSTYPE
宏来替换 yacc 使用的类型,而不是 %union
。
我做了以下更改来使用此方法:
在 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
在 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;
}
在 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; }
在 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.
我仍然遇到 Objective-C 编译错误,所以我并不比你更进一步,但我会继续努力......但可能它对你没有用。
<小时/> * 请参阅 this section 的最后一段:This section has been concerned with explaining the advantages of the four Prologue alternatives over the original Yacc Prologue.
它解释了 yacc 代码排序的弱点以及 bison 如何使用 %code
指令克服该弱点。 Xcode 中内置的是 yacc 而不是 bison。可以在系统上用 bison 替换 yacc 来克服这个缺陷。
关于ios - "Unknown type name"使用 YACC 和 xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30327604/