Erlang **异常错误:没有函数子句匹配

时间:2016-01-07 14:27:47

标签: parsing exception erlang

大家好,我有一个定理证明器解析器的气质。我有一个模块,它先前将字符串标记为即时输入:[{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}],
                                       [])

1 个答案:

答案 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的两个出现更改为其他内容,例如PropLogicOp,该条款将接受两个不同的值。