[erlang-questions] problem making my first parser

Roelof Wobben r.wobben@REDACTED
Sat Sep 26 09:31:31 CEST 2015


Op 26-9-2015 om 09:08 schreef zxq9:
> On Saturday 26 September 2015 09:01:30 Roelof Wobben wrote:
>> Hello,
>>
>> I try to make a very easy parser to practice to make a json parser in
>> the future.
>> As first I try to make a parser which can calculate some basic things.
>>
>> So far I have this :
>>
>> -module(number_parser).
>>
>> -export([scan/1]).
>>
>> scan(String) ->
>>       scan(String, []).
>>
>> scan([], List) ->
>>       List;
>>
>> scan([Head | Rest], List_parse) when Head >= $0, Head =< $9 ->
>>        scan(Rest, [(Head - 48) | List_parse]);
>>
>> scan([Head | Rest], List_parse) when Head =:= "+" ->
>>      scan(Rest, [Head | List_parse] ).
>>
>>
>> but when I try this code on scan("1+1") I see this error message :
>>
>> ** exception error: no function clause matching
>> number_parser:scan("+1",[1]) (number_parser.erl, line 8)
>>
>> Which does not make sense. +1 makes as far as I know [+, 1] so I have a
>> head and tail so the last clause schould work.
>>
>>
>> Why do I then see this error message ?
> What happens if you try `[1] >= $0, [1] =< $9.`?
>
> How about `[1] =:= "+".` ?
>
> What are you actually comparing here?

A list with a string/char.   But I check the tail of a list which 
contains a char with + or 0 - 9.

> Do you care about plus signs as strings, or do you care about plus as a symbol that represents an operation?

At this point I only care about the plus sign as a string. Later on the 
parse function I will make a operation based on this.


Roelof




More information about the erlang-questions mailing list