Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

parsing - Properly matching a set of tokens against my BASIC grammar

I am working on writing a BASIC interpreter in Prolog. DCGs are a little tricky, which is why I am having trouble here today.

Here is my grammar.

bool --> [true].
bool --> [false].

is_num_char(AD) :- AD = '.'; (atom_codes(AD, [D]), D >= 48, D =< 57).
number --> [].
number --> is_num_char, number.

quotes_on_atom(S) :- atom_chars(S, ['"' | C]), last(C, '"').
string --> quotes_on_atom.

literal --> bool; number; string.
variable --> + literal.

assignment --> string, ['='], expr.

equal --> expr, ['=='], expr.
not_equal --> expr, ['!='], expr.

if --> [if], expr, [then], expr.

for_decl --> [for], assignment, [to], number, [step], number.
for_next --> [next], integer.

expr --> literal; variable; assignment;
        equal; not_equal; if; for_decl; for_next.

Here is my main goal:

main :-
    % expected: [for, [i, '=', 0], to, '5', step, '1']
    trace, phrase(expr, [for, i, '=', '0', to, '5', step, '1']).

Here is the error I get: uncaught exception: error(existence_error(procedure,is_num_char/2),number/0).

A stack trace revealed this:

The debugger will first creep -- showing everything (trace)
      1    1  Call: phrase(expr,[for,i,=,'0',to,'5',step,'1']) ? 
      2    2  Call: expr([for,i,=,'0',to,'5',step,'1'],_335) ? 
      3    3  Call: literal([for,i,=,'0',to,'5',step,'1'],_335) ? 
      4    4  Call: bool([for,i,=,'0',to,'5',step,'1'],_335) ? 
      4    4  Fail: bool([for,i,=,'0',to,'5',step,'1'],_335) ? 
      4    4  Call: number([for,i,=,'0',to,'5',step,'1'],_335) ? 
      4    4  Exit: number([for,i,=,'0',to,'5',step,'1'],[for,i,=,'0',to,'5',step,'1']) ? 
      3    3  Exit: literal([for,i,=,'0',to,'5',step,'1'],[for,i,=,'0',to,'5',step,'1']) ? 
      2    2  Exit: expr([for,i,=,'0',to,'5',step,'1'],[for,i,=,'0',to,'5',step,'1']) ? 
      2    2  Redo: expr([for,i,=,'0',to,'5',step,'1'],[for,i,=,'0',to,'5',step,'1']) ? 
      3    3  Redo: literal([for,i,=,'0',to,'5',step,'1'],[for,i,=,'0',to,'5',step,'1']) ? 
      4    4  Redo: number([for,i,=,'0',to,'5',step,'1'],[for,i,=,'0',to,'5',step,'1']) ? 
      5    5  Call: is_num_char([for,i,=,'0',to,'5',step,'1'],_446) ? 
      5    5  Exception: is_num_char([for,i,=,'0',to,'5',step,'1'],_459) ? 
      4    4  Exception: number([for,i,=,'0',to,'5',step,'1'],_335) ? 
      3    3  Exception: literal([for,i,=,'0',to,'5',step,'1'],_335) ? 
      2    2  Exception: expr([for,i,=,'0',to,'5',step,'1'],_335) ? 
      1    1  Exception: phrase(expr,[for,i,=,'0',to,'5',step,'1']) ? 
uncaught exception: error(existence_error(procedure,is_num_char/2),number/0)
{trace}

It seems that is_num_char is being passed the whole list of tokens, along with something else. I do not understand why this is happening, given that the number rule accepts only one argument. Additionally, it's odd that the token list unifies with number to begin with. It should be unified with for_decl instead. If you are knowledgeable about Prolog DCGs please let me know what I am doing wrong here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...