"Unknown type name" 将 YACC 与 xcode 结合使用

"Unknown type name" using YACC with xcode

我正在尝试将 YACC 与 xcode 一起使用,它是本地支持的,为此我找到了 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;
}

使用 Unknown type name '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 以错误的顺序为某些代码生成代码languages/compilers。 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来克服这个不足。