How to do a parser in Prolog?

丶灬走出姿态 提交于 2019-12-07 14:39:16

问题


I would like to do a parser in prolog. This one should be able to parse something like this:

a = 3 + (6 * 11);

For now I only have this grammar done. It's working but I would like to improve it in order to have id such as (a..z)+ and digit such as (0..9)+.

parse(-ParseTree, +Program, []):-
    parsor(+Program, []).

parsor --> [].
parsor --> assign.
assign --> id, [=], expr, [;].
id --> [a] | [b].
expr --> term, (add_sub, expr ; []).
term --> factor, (mul_div, term ; []).
factor --> digit | (['('], expr, [')'] ; []).
add_sub --> [+] | [-].
mul_div --> [*] | [/].
digit --> [0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9].

Secondly, I would like to store something in the ParseTree variable in order to print the ParseTree like this:

PARSE TREE:
assignment
    ident(a)
    assign_op
    expression
        term
            factor
                int(1)
            mult_op
            term
                factor
                    int(2)
        add_op
        expression
            term
                factor
                    left_paren
                    expression
                        term
                            factor
                                int(3)
                        sub_op
                        ...

And this is the function I'm going use to print the ParseTree:

output_result(OutputFile,ParseTree):- 
    open(OutputFile,write,OutputStream),
    write(OutputStream,'PARSE TREE:'), 
    nl(OutputStream), 
    writeln_term(OutputStream,0,ParseTree), 
    close(OutputStream).

writeln_term(Stream,Tabs,int(X)):-
    write_tabs(Stream,Tabs), 
    writeln(Stream,int(X)).
writeln_term(Stream,Tabs,ident(X)):-
    write_tabs(Stream,Tabs), 
    writeln(Stream,ident(X)).
writeln_term(Stream,Tabs,Term):-
    functor(Term,_Functor,0), !,
    write_tabs(Stream,Tabs),
    writeln(Stream,Term).
writeln_term(Stream,Tabs1,Term):-
    functor(Term,Functor,Arity),
    write_tabs(Stream,Tabs1),
    writeln(Stream,Functor),
    Tabs2 is Tabs1 + 1,
    writeln_args(Stream,Tabs2,Term,1,Arity).

writeln_args(Stream,Tabs,Term,N,N):-
    arg(N,Term,Arg),
    writeln_term(Stream,Tabs,Arg).
writeln_args(Stream,Tabs,Term,N1,M):-
    arg(N1,Term,Arg),
    writeln_term(Stream,Tabs,Arg), 
    N2 is N1 + 1,
    writeln_args(Stream,Tabs,Term,N2,M).

write_tabs(_,0).
write_tabs(Stream,Num1):-
    write(Stream,'\t'),
    Num2 is Num1 - 1,
    write_tabs(Stream,Num2).

writeln(Stream,Term):-
    write(Stream,Term), 
    nl(Stream).

write_list(_Stream,[]). 
write_list(Stream,[Ident = Value|Vars]):-
    write(Stream,Ident),
    write(Stream,' = '),
    format(Stream,'~1f',Value), 
    nl(Stream), 
    write_list(Stream,Vars).

I hope someone will be able to help me. Thank you !


回答1:


Here's an enhancement of your parser as written which can get you started. It's an elaboration of the notions that @CapelliC indicated.

parser([]) --> [].
parser(Tree) --> assign(Tree).

assign([assignment, ident(X), '=', Exp]) --> id(X), [=], expr(Exp), [;].

id(X) --> [X], { atom(X) }.

expr([expression, Term]) --> term(Term).
expr([expression, Term, Op, Exp]) --> term(Term), add_sub(Op), expr(Exp).

term([term, F]) --> factor(F).
term([term, F, Op, Term]) --> factor(F), mul_div(Op), term(Term).

factor([factor, int(N)]) --> num(N).
factor([factor, Exp]) --> ['('], expr(Exp), [')'].

add_sub(Op) --> [Op], { memberchk(Op, ['+', '-']) }.
mul_div(Op) --> [Op], { memberchk(Op, ['*', '/']) }.

num(N) --> [N], { number(N) }.

I might have a couple of niggles in here, but the key elements I've added to your code are:

  • Replaced digit with num which accepts any Prolog term N for which number(N) is true
  • Used atom(X) to identify a valid identifier
  • Added an argument to hold the result of parsing the given expression item

As an example:

| ?- phrase(parser(Tree), [a, =, 3, +, '(', 6, *, 11, ')', ;]).

Tree = [assignment,ident(a),=,[expression,[term,[factor,int(3)]],+,[expression,[term,[factor,[expression,[term,[factor,int(6)],*,[term,[factor,int(11)]]]]]]]]] ? ;

This may not be an ideal representation of the parse tree. It may need some adjustment per your needs, which you can do by modifying what I've shown a little. And then you can write a predicate which formats the parse tree as you like.

You could also consider, instead of a list structure, an embedded Prolog term structure as follows:

parser([]) --> [].
parser(Tree) --> assign(Tree).

assign(assignment(ident(X), '=', Exp)) --> id(X), [=], expr(Exp), [;].

id(X) --> [X], { atom(X) }.

expr(expression(Term)) --> term(Term).
expr(expression(Term, Op, Exp)) --> term(Term), add_sub(Op), expr(Exp).

term(term(F)) --> factor(F).
term(term(F, Op, Term)) --> factor(F), mul_div(Op), term(Term).

factor(factor(int(N))) --> num(N).
factor(factor(Exp)) --> ['('], expr(Exp), [')'].

add_sub(Op) --> [Op], { memberchk(Op, ['+', '-']) }.
mul_div(Op) --> [Op], { memberchk(Op, ['*', '/']) }.

num(N) --> [N], { number(N) }.

Which results in something like this:

| ?- phrase(parser(T), [a, =, 3, +, '(', 6, *, 11, ')', ;]).

T = assignment(ident(a),=,expression(term(factor(int(3))),+,expression(term(factor(expression(term(factor(int(6)),*,term(factor(int(11)))))))))) ? ;



回答2:


A recursive rule for id//0, made a bit more generic:

id --> [First], {char_type(First,lower)}, id ; [].

Building the tree could be done 'by hand', augmenting each non terminal with the proper term, like

...
assign(assign(Id, Expr)) --> id(Id), [=], expr(Expr), [;].
...

id//0 could become id//1

id(id([First|Rest])) --> [First], {memberchk(First, [a,b])}, id(Rest) ; [], {Rest=[]}.

If you're going to code such parsers frequently, a rewrite rule can be easily implemented...



来源:https://stackoverflow.com/questions/27878382/how-to-do-a-parser-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!