大家好,我有一个定理证明器解析器的气质。我有一个模块,它先前将字符串标记为即时输入:[{bracket,open},{prop,a},{logicOp,'和'},{prop,b},{bracket,close}]到解析器有一个内部函数调用。这是代码:
parse([])-> [];
parse(FullList) ->
parseClauses(FullList,[],[]).
parseClauses([{bracket, open}| RestOfList], StackList, ParsedList) ->
parseClauses(RestOfList,
StackList ++ [{bracket, open}],
ParsedList);
parseClauses([{prop, Any},{logicOp, Any}| RestOfList], StackList, ParsedList) ->
parseClauses(RestOfList,
StackList ++ [{logicOp, Any},{prop, Any}],
ParsedList);
parseClauses([{bracket, close}, {logicOp, Any}| RestOfList],StackList,ParsedList) ->
parseClauses(RestOfList,
StackList ++ [{bracket, close}],
[{logicOp, Any}] ++ ParsedList);
parseClauses([{bracket, close}|RestOfList], StackList, ParsedList) ->
parseClauses(RestOfList,
StackList++[{bracket, close}],
ParsedList);
parseClauses([], Stack, Parsed) -> Parsed ++ Stack.
像这样在终端上运行代码并得到错误:
tokeniser:parse([{bracket,open},
{prop,a},
{logicOp,'and'},
{prop,b},
{bracket,close}]).
** exception error: no function clause matching tokeniser:parseClauses([{prop,a},{logicOp,'and'},{prop,b},{bracket,close}],
[{bracket,open}],
[])
答案 0 :(得分:5)
从错误消息中,您可以看出正在调用该函数:
tokeniser:parseClauses([{prop,a},{logicOp,'and'},{prop,b},{bracket,close}],
[{bracket,open}],
[])
此几乎符合此条款:
parseClauses([{prop, Any},{logicOp, Any}| RestOfList], StackList, ParsedList) ->
但由于Any
在参数列表中使用了两次,因此该子句仅在两个值相同时才匹配。在此次通话中,它们不同:a
和'and'
。
您可以将Any
的两个出现更改为其他内容,例如Prop
和LogicOp
,该条款将接受两个不同的值。