使用DCG解析变量 [英] Parse Variables Using DCG

查看:77
本文介绍了使用DCG解析变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Prolog的DCG表示法将以大写字母开头的序列解析为变量时遇到麻烦.例如,如果我有字符串

I am having trouble parsing sequences that begin with capital letters into variables using Prolog's DCG notation. For instance, if I have the string

f a X y Z X

和一个DCG解析此字符串,可以用任何方法将每个大写字母解析为唯一的Prolog变量.例如,将Y解析为变量,将每个X解析为变量?预期的应用程序将是构建函子

and a DCG that parses this string, is there any way to parse each capitalized letter into a unique Prolog variable. E.g., parse Y to a variable and each X to a variable? The intended application would be to build the functor

T = f(a,X,y,Z,X)

通过以该语句结尾的DCG规则

via a DCG rule ending with the statement

{T =.. [Head|Args]}

推荐答案

也许您正在寻找term_to_atom/3:

?- term_to_atom(Term, 'f(a,X,y,Z,X)').
Term = f(a, _G304, y, _G306, _G304).

这篇关于使用DCG解析变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆