无效的预处理令牌错误

时间:2012-12-24 13:45:53

标签: c token c-preprocessor

我正在尝试编译包含Middlc.h的文件Args.c。 Middlc.h错误地给出了以下错误。

enter code here
Middlc.h:23:1: error: pasting "*" and "ArgList" does not give a valid preprocessing token
Middlc.h:24:1: error: pasting "*" and "CandidateList" does not give a valid preprocessing token
Middlc.h:25:1: error: pasting "*" and "Decl" does not give a valid preprocessing token
Middlc.h:26:1: error: pasting "*" and "DeclList" does not give a valid preprocessing token
Middlc.h:27:1: error: pasting "*" and "ExcList" does not give a valid preprocessing token
Middlc.h:28:1: error: pasting "*" and "Field" does not give a valid preprocessing token
Middlc.h:29:1: error: pasting "*" and "FieldList" does not give a valid preprocessing token
Middlc.h:30:1: error: pasting "*" and "Identifier" does not give a valid preprocessing token
Middlc.h:31:1: error: pasting "*" and "IdentifierList" does not give a valid preprocessing token
Middlc.h:32:1: error: pasting "*" and "Interface" does not give a valid preprocessing token
Middlc.h:33:1: error: pasting "*" and "InterfaceList" does not give a valid preprocessing token
Middlc.h:34:1: error: pasting "*" and "Spec" does not give a valid preprocessing token
Middlc.h:35:1: error: pasting "*" and "SymbolTable" does not give a valid preprocessing token
Middlc.h:36:1: error: pasting "*" and "ScopedName" does not give a valid preprocessing token
Middlc.h:37:1: error: pasting "*" and "Type" does not give a valid preprocessing token
Middlc.h:38:1: error: pasting "*" and "TypeList" does not give a valid preprocessing token
Middlc.h:39:1: error: pasting "*" and "FileStack" does not give a valid preprocessing token

即使单个错误得到解决也会非常有用。我可以在此基础上解决其他问题。

以下是代码:

Middlc.h

#ifndef _Middlc_h_
#define _Middlc_h_

/*
 * Main header file for Middlc.
 */

#define PY(x) printf x 

#define new(x) ((x##_t)malloc(sizeof(struct _##x##_t)))
#define TYPE_DECL(x) typedef struct _##x##_t *##x##_t

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef enum { False, True } bool_t;

#define MAX_STRING_LENGTH 1024

TYPE_DECL(ArgList);
TYPE_DECL(CandidateList);
TYPE_DECL(Decl);
TYPE_DECL(DeclList);
TYPE_DECL(ExcList);
TYPE_DECL(Field);
TYPE_DECL(FieldList);
TYPE_DECL(Identifier);
TYPE_DECL(IdentifierList);
TYPE_DECL(Interface);
TYPE_DECL(InterfaceList);
TYPE_DECL(Spec);
TYPE_DECL(SymbolTable);
TYPE_DECL(ScopedName);
TYPE_DECL(Type);
TYPE_DECL(TypeList);
TYPE_DECL(FileStack);

#include "FileStack.h"
#include "SymbolTable.h"

#include "Modes.h"
#include "Decls.h"
#include "Types.h"
#include "Args.h"
#include "Candidates.h"
#include "Exceptions.h"
#include "Fields.h"
#include "Identifiers.h"
#include "Interfaces.h"
#include "Specs.h"
#include "ScopedNames.h"
#include "Parser.h"

#include "Globals.h"
#include "Errors.h"
#endif /* _Middlc_h_ */

2 个答案:

答案 0 :(得分:5)

该行

#define TYPE_DECL(x) typedef struct _##x##_t *##x##_t

应该是

#define TYPE_DECL(x) typedef struct _##x##_t * x##_t

(没有粘贴##)。目前,预处理器尝试将星号“粘合”到SymbolTable_t之类的名称,从而生成不可解析的标识符SymbolTable_t。当你离开##时,编译器将星号解析为一个单独的标记,修复错误。

答案 1 :(得分:2)

您需要删除*和x之间的##。那就是:

#define TYPE_DECL(x) typedef struct _##x##_t * x##_t

为什么你正在做所有这些宏的事情当然是另一回事 - 使用“new”作为宏肯定不好 - 如果你以后想用C ++编译器编译你的代码会发生什么(例如因为一些真的出色的C ++代码)。