本文主要是介绍C99的指定初始化转化为C89?用(a|e)?bnf实现吧,写个玩玩,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用了自己实现的ebnf小库,没有实现捕获组,被迫用lambda捕获了若干临时变量。
熟悉bnf的,把它翻到自己熟悉的工具上,应该不困难。
struct MemberInfo
{string type;
string name;
deque<int> arrSizes;
};
struct InitValue
{
string value;
vector<InitValue> sub;
string name;
// TYPE type;
InitValue(const vector<InitValue> & s, const string & n) : sub(s), name(n){}
InitValue(const string & v, const string & n) : value(v), name(n){}
string str() const
{
return !value.empty() ? value :
"{" + join(adaptors::transform(sub, [](const InitValue & v)
{
return v.str();
}), ", ") + "}"
;
}
void Set(deque<int> offsets, const string & v)
{
int offset = offsets.front();
offsets.pop_front();
if (offsets.empty())
{
assert(sub[offset].sub.size() == 0);
sub[offset].value = v;
}
else
{
assert(value.empty());
sub[offset].Set(offsets, v);
}
}
int FindOffset(deque<int> offsets, const string & n) const
{
if (offsets.size() != 1)
{
int offset = offsets.front();
offsets.pop_front();
return sub[offset].FindOffset(offsets, n);
}
for (int i = 0; i < sub.size(); ++i)
{
if (sub[i].name == n)
{
return i;
}
}
}
};
InitValue MakeArrsInitValue(InitValue curInitValue, const deque<int> & Sizes)
{
for (auto iter = Sizes.rbegin(); iter != Sizes.rend(); ++iter)
{
curInitValue = {vector<InitValue>(*iter, curInitValue), ""};
}
return curInitValue;
}
InitValue StructInitValue(map<string, vector<MemberInfo> > & structInfo, vector<MemberInfo> & memberInfos, const string & memberName)
{
return InitValue(copy_range<vector<InitValue> >(
adaptors::transform(memberInfos, [&structInfo](const MemberInfo & info)
{
auto initValue = (structInfo.count(info.type) == 0) ? InitValue("0", info.name) :
StructInitValue(structInfo, structInfo[info.type], info.name);
return MakeArrsInitValue(initValue, info.arrSizes);
})), memberName);
}
//注意,因为左递归的根因,不可以写成 E -> E T | T;必须是 E -> T*;或者E -> T E | T
// T*理解简单,但是对于数组,求值顺序是从左到右,不符合数组的语义,还需要自己做运算倒置。
// T E | T,T的解析恰好很完美,但是E还是从左到右的。把自己绕进去了。
bool expandC99(const string & in, string & out)
{
vector<string> tmpCurStructName;
static map<string, vector<MemberInfo> > structInfo;
boost::function<void (string)> popCurStructName = [&tmpCurStructName](const string &)
{
tmpCurStructName.pop_back();
};
boost::function<void (string)> popCurStructNameAndRecordTypeName = [&tmpCurStructName, &structInfo](const string &)
{
string TypeName = tmpCurStructName.back();
tmpCurStructName.pop_back();
structInfo[tmpCurStructName.back()].push_back({TypeName, ""});
};
deque<int> tmpArrSizes;
auto arrSize = Words([&tmpArrSizes](const string & s)
{
tmpArrSizes.push_back(lexical_cast<int>(s));
});
SeqParser arr = Seq(AnySpaces, Str("["), AnySpaces, arrSize, AnySpaces, Str("]"));
SeqParser Struct;
auto memberType = Words([&tmpCurStructName, &structInfo](const string & s){structInfo[tmpCurStructName.back()].push_back({s, ""});});
auto memberName = Words([&tmpCurStructName, &structInfo](const string & s){structInfo[tmpCurStructName.back()].back().name = s;});
auto memberArrs = Keep(Any(arr, [&tmpCurStructName, &structInfo, &tmpArrSizes](const string & s)
{
structInfo[tmpCurStructName.back()].back().arrSizes = tmpArrSizes;
tmpArrSizes.clear();
}));
SeqParser member = Seq(AnySpaces,
Or(
Seq(Ref(Struct, popCurStructNameAndRecordTypeName), AnySpaces, memberName, memberArrs),
Seq(memberType, Spaces, memberName, memberArrs)
),
AnySpaces, Str(";"));
Struct =
Seq(Keep(Seq(
Str("struct"), Spaces,
Words([&tmpCurStructName](const string & s){tmpCurStructName.push_back(s);}),
AnySpaces, Str("{"),
More(member),
AnySpaces, Str("}")))
);
InitValue initValue = {"Not Init Exp!", ""};
boost::function<void (string)> popCurStructNameAndMakeStructValue = [&tmpCurStructName, &structInfo, &initValue](const string & s)
{
initValue = StructInitValue(structInfo, structInfo[tmpCurStructName.back()], s);
tmpCurStructName.pop_back();
};
auto typeName = Words([&structInfo, &initValue](const string & s)
{
initValue = (structInfo.count(s) == 0) ? /*数组需要*/ InitValue("0", "") :
StructInitValue(structInfo, structInfo[s], "");
});
auto varArrs = Keep(Any(arr, [&initValue, &tmpArrSizes](const string & s)
{
initValue = MakeArrsInitValue(initValue, tmpArrSizes);
}));
deque<int> tmpInitOffsets;
SeqParser inits;
auto noneInit = Stub([](const string & s){cout << "noneInit" << endl;});
auto valueInit = Words([&initValue, &tmpInitOffsets](const string & s){initValue.Set(tmpInitOffsets, s); ++tmpInitOffsets.back();});
auto baseInit = Or(valueInit, Ref(inits));
auto arrDesignator = Seq(Str("["), AnySpaces, Words([&tmpInitOffsets](const string & s){tmpInitOffsets.back() = lexical_cast<int>(s);}), AnySpaces, Str("]"));
auto structDesignator = Seq(Str("."), AnySpaces, Words([&initValue, &tmpInitOffsets](const string & s){tmpInitOffsets.back() = initValue.FindOffset(tmpInitOffsets, s);}));
int tmpMultiDesignator = 0;
vector<int> tmpMultiDesignators;
auto multiDesignatorStub = Stub([&tmpInitOffsets, &tmpMultiDesignator](const string &)
{
tmpInitOffsets.push_back(0); ++tmpMultiDesignator;
});
auto designatedInit = Seq(Or(arrDesignator, structDesignator),
Any(Seq(multiDesignatorStub, Or(arrDesignator, structDesignator)),
[&tmpMultiDesignator, &tmpMultiDesignators](const string &)
{
tmpMultiDesignators.push_back(tmpMultiDesignator);
tmpMultiDesignator = 0;
}),
AnySpaces, Str("="), AnySpaces, baseInit,
[&tmpInitOffsets, &tmpMultiDesignators](const string &)
{
tmpInitOffsets.resize(tmpInitOffsets.size() - tmpMultiDesignators.back());
tmpMultiDesignators.pop_back();
});
auto oneInit = Or(valueInit,
Ref(inits, [&tmpInitOffsets](const string & s){++tmpInitOffsets.back();}),
designatedInit);
auto moreInit = Seq(oneInit, Any(Seq(AnySpaces, Str(","), AnySpaces, oneInit)), AnySpaces, Optional(Str(",")));
inits = Seq(Str("{", [&tmpInitOffsets](const string &){tmpInitOffsets.push_back(0);}), AnySpaces,
Or(noneInit, moreInit), AnySpaces,
Str("}", [&tmpInitOffsets](const string &){tmpInitOffsets.pop_back();}));
auto defineTypedVar = Seq(typeName, Spaces, Words(), varArrs, AnySpaces, Str("="), AnySpaces, inits, AnySpaces, Str(";"));
auto defineStruct = Seq(Struct, AnySpaces, Str(";", popCurStructName));
auto defineStructVar = Seq(Struct, AnySpaces, Words(popCurStructNameAndMakeStructValue), varArrs, AnySpaces, Str("="), AnySpaces, inits, AnySpaces, Str(";"));
auto Exp = Or(defineTypedVar, defineStruct, defineStructVar);
bool r = Exp.Match(in);
cout << initValue.str() << endl;
return r;
}
BOOST_AUTO_TEST_CASE(TestStruct)
{
string out;
BOOST_CHECK_EQUAL(expandC99("struct A {int a ; int b ; int c ;} ;", out), true);
BOOST_CHECK_EQUAL(expandC99("struct B {int a ; int b [2] ; int c ;} a = {};", out), true);
BOOST_CHECK_EQUAL(expandC99("struct X {A a ; int b ; int c ;} a = {{1}, 3};", out), true);
BOOST_CHECK_EQUAL(expandC99("struct Y {int a ; struct XX {int i;} b ; int c ;} b = {.c = 3};", out), true);
BOOST_CHECK_EQUAL(expandC99("int a[3][2] = {{1, 2}, {3, 4}, [2][1] = 6};", out), true);
BOOST_CHECK_EQUAL(expandC99("X a[3][2] = {[1][0].a.b = 2};", out), true);
BOOST_CHECK_EQUAL(expandC99("X a[3][2] = {[1][0].a = {.b = 2}, [2][0].a.b = 3};", out), true);
}
这篇关于C99的指定初始化转化为C89?用(a|e)?bnf实现吧,写个玩玩的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!