1

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 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @legoscia your good nature in taking the time to answer my question has saved some of my hair from being pulled out :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.