I'm working on writing a simple preprocessor for a compi开发者_如何转开发ler. Below is an edited snippet of my code:
%{
#include <string.h>
#include <hash_map>
#include "scanner.h"
#include "errors.h"
struct eqstr {
bool operator()(const char* s1, const char* s2) const {
return strcmp(s1, s2) == 0;
}
};
std::hash_map<const char*, char*, hash<const char*>, eqstr> defs; // LINE 28
%}
// Definitions here
%%
// Patterns and actions here
%%
When I compile I get the following error:
dpp.l:28: error: expected constructor, destructor, or type conversion before ‘<’ token
Any ideas what might be wrong with this? I pretty much copied and pasted this line from the sgi documentation.
You need std::hash
rather than just hash
, since you have no using
statement that will pull it into scope. Also, the default std::hash<const char *>
will hash the pointer directly, which won't work for this use -- you need a hash function that hashes the c-string pointed at. You need to define your own specialization of hash
, or your own hashing function -- the latter is probably better.
精彩评论