jldavis76
jldavis76

Reputation: 822

Writing A Prolog Parser

I'm trying to write a simple parser for a grammar. The parser does not need to create a parse tree, only recognize if a sentence matches the grammar. So far, I have the following predicates, using DCG notation:

    program-->[].
    program-->stmt_list.
    stmt_list-->stmt,stmt_list.
    stmt-->[id,:=],expr;[read],[id];[write],expr.
    expr-->term, term_tail.
    term_tail-->add_op,term,term_tail.
    term_tail-->[].
    term-->factor, factor_tail.
    factor_tail-->mult_op, factor, factor_tail.
    factor_tail-->[].
    factor-->[(expr)].
    factor-->[id].
    factor-->[number].
    add_op-->[+].
    add_op-->[-].
    mult_op-->[*].
    mult_op-->[/].

Using a query such as program([read,id],[]). I should be getting a true response, but I am getting a false one. What is missing that is causing this? Thank you for your help.

Upvotes: 1

Views: 1534

Answers (1)

Alexander Serebrenik
Alexander Serebrenik

Reputation: 3577

stmt_list can also be empty. Adding stmt_list --> [] solves the problem.

Upvotes: 1

Related Questions