为给定的上下文无关语法生成符号字符串(句子)

发布于 2024-12-16 12:02:38 字数 257 浏览 2 评论 0原文

我有一个简单的语法,例如

S::=a S b
S::=[] (empty string)

现在我想为上述语法编写一个解析器,例如

cfg('S', [a,'S',b])

通过最左推导生成一个句子 aaabbb

我不足以处理序言中的 dcg/cfg 。 所以请帮助我解决这个例子,以便我可以继续尝试更大的事情。

I have a simple grammar such as

S::=a S b
S::=[] (empty string)

Now i want to write a parser for the above grammar like

cfg('S', [a,'S',b])

which generates a sentence aaabbb by left most derivation.

I'm not good enough to handle dcg/cfg in prolog.
So pls help me with this example so that i can go ahead and try something bigger.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋顶上的小猫咪 2024-12-23 12:02:38

考虑这个 DCG 代码:

s-->[].
s-->[a],s,[b].

要运行由 DCG 定义的谓词,您应该在末尾添加两个参数:“输入”及其剩余内容。如果您想识别整个列表,只需输入 []。因此,当您运行它时,您会得到:

38 ?- s(C,[]).
C = [] ;
C = [a, b] ;
C = [a, a, b, b] ;
C = [a, a, a, b, b, b] ;
C = [a, a, a, a, b, b, b, b] ;
...

如果您想要某种“返回”字符串,您可以将其添加为额外的参数。要在 dcg 子句中编写 prolog 代码,您可以使用 {}:

s('')-->[].
s(S)-->
    [a],s(SI),[b],
    { atomic_list_concat([a,SI,b],S)}.

,您会得到:

40 ?- s(R,X,[]).
R = '',
X = [] ;
R = ab,
X = [a, b] ;
R = aabb,
X = [a, a, b, b] ;
R = aaabbb,
X = [a, a, a, b, b, b] ;
R = aaaabbbb,
X = [a, a, a, a, b, b, b, b] ;
R = aaaaabbbbb,
...

我们生成了此语法可识别的所有字符串;通常你只想检查语法是否可以识别字符串。为此,您只需将其作为输入:

41 ?- s([a,b],[]).
true 

42 ?- s([a,b,b],[]).
false.

请注意,我们将 S::=[] 规则放在第一位,否则如果您要求生成所有解决方案,序言将陷入无限循环。在更复杂的语法中解决这个问题可能并不容易。要获得解决方案,您可以使用 length/2:

?- length(X,_),s(X,[]).
X = [] ;
X = [a, b] ;
X = [a, a, b, b] ;
X = [a, a, a, b, b, b] ;
X = [a, a, a, a, b, b, b, b] 

即使您的代码是:

s-->[].
s-->[a],s,[b].

Consider this DCG code:

s-->[].
s-->[a],s,[b].

to run a predicate you defined by DCG you should add two more arguments at the end: the "input" and what it's left. If you want to recognize the whole list you simply put []. So, when you run it you get:

38 ?- s(C,[]).
C = [] ;
C = [a, b] ;
C = [a, a, b, b] ;
C = [a, a, a, b, b, b] ;
C = [a, a, a, a, b, b, b, b] ;
...

If you wanted some sort of "return" string you could add it as an extra arg. To write prolog code in a dcg clause you use {}:

s('')-->[].
s(S)-->
    [a],s(SI),[b],
    { atomic_list_concat([a,SI,b],S)}.

and you get:

40 ?- s(R,X,[]).
R = '',
X = [] ;
R = ab,
X = [a, b] ;
R = aabb,
X = [a, a, b, b] ;
R = aaabbb,
X = [a, a, a, b, b, b] ;
R = aaaabbbb,
X = [a, a, a, a, b, b, b, b] ;
R = aaaaabbbbb,
...

we generated all the strings that are recognized by this grammar; usually you just want to check if a string is recognized by the grammar. to do that you simply put it as input:

41 ?- s([a,b],[]).
true 

42 ?- s([a,b,b],[]).
false.

note that we put the S::=[] rule first otherwise prolog would fall in a infinite loop if you asked to generate all the solutions. This problem might not be trivial to solve in more complex grammars. To get the solutions you can use length/2:

?- length(X,_),s(X,[]).
X = [] ;
X = [a, b] ;
X = [a, a, b, b] ;
X = [a, a, a, b, b, b] ;
X = [a, a, a, a, b, b, b, b] 

even if your code is:

s-->[].
s-->[a],s,[b].
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文