1
votes

Hi guys I have the makings of a parser for a Theorem Prover. I have a module which previously tokenises the string so im inputting: [{bracket,open},{prop,a},{logicOp,'and'},{prop,b},{bracket,close}] to a parser which has an calls an inner function. here is the code:

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.

run the code on terminal like so and get error:

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

1 Answers

5
votes

From the error message, you can tell that the function is being called like this:

tokeniser:parseClauses([{prop,a},{logicOp,'and'},{prop,b},{bracket,close}],
                                       [{bracket,open}],
                                       [])

This almost matches this clause:

parseClauses([{prop, Any},{logicOp, Any}| RestOfList], StackList, ParsedList) ->

But since Any is used twice in the argument list, this clause only matches when the two values are the same. In this call, they are different: a and 'and'.

You could change the two occurences of Any to something else, e.g. Prop and LogicOp, and the clause would accept two different values.