如何通过编程(动态)创建Raku语法?
假设语法G具有两种作品……
- S→
- λS→Raku中的ASB
,一个人将如何以编程方式创建此语法(即动态,在运行时动态)?
目的是在运行时创建一个Raku程序 - 一种Raku语法,该语法可能是静态写的,因为……
grammar Parser
{
token TOP { <S> }
token S { '' | 'a' <S> 'b' }
}
给出了我问题的第一个答案,我正在尝试动态创建静态写入的内容……
grammar Parser
{
token TOP { <S> }
} # end grammar Parser
但是,我尝试了……
constant Parser := Metamodel::GrammarHOW.new_type( name => 'Parser' ) ;
Parser.^add_method('TOP', my method TOP(Parser:) { <S> }) ;
Parser.^compose; # }
say Parser.HOW.^name ;
say Parser.^methods(:local) ;
但是,答复是………
Perl6::Metamodel::GrammarHOW
(TOP)
而不是希望……
Perl6::Metamodel::GrammarHOW
(token TOP { <S> } BUILDALL)
add_method
应该如何添加top
token (后来,其他令牌,例如s
令牌)?
经过更多的工作,我相信我可能有一个解决方案……
constant Parser := Metamodel::GrammarHOW.new_type( name => 'Parser' ) ;
Parser.^add_method( 'TOP', my token TOP { <S> } ) ;
Parser.^add_method( 'S', my token S { '' | 'a' <S> 'b' } ) ;
Parser.^compose ;
say Parser.HOW.^name ;
say Parser.^methods( :local ) ;
say Parser.parse: 'aabb' ;
输出是……
Perl6::Metamodel::GrammarHOW
(token TOP { <S> } token S { '' | 'a' <S> 'b' })
「aabb」
S => 「aabb」
S => 「ab」
S => 「」
我已经编码了 static parser
的版本> static 解析器
与上面显示的输出相似的输出是……
(token TOP { <S> } token S { '' | 'a' 'a' <S> 'b' 'b' } BUILDALL)
我不确定我buildall
在我的中丢失了动态 >创建解析器
。我不了解bueldall
,在线搜索时找不到太多。
Suppose grammar G has two productions …
- S → λ
- S → aSb
In Raku, how would one create this grammar programmatically (i.e., dynamically, at runtime)?
The goal is to have a Raku program create — at runtime — a Raku grammar that might be written statically as …
grammar Parser
{
token TOP { <S> }
token S { '' | 'a' <S> 'b' }
}
Given the first answer to my question, I am trying to dynamically create what would be statically written as …
grammar Parser
{
token TOP { <S> }
} # end grammar Parser
I have tried …
constant Parser := Metamodel::GrammarHOW.new_type( name => 'Parser' ) ;
Parser.^add_method('TOP', my method TOP(Parser:) { <S> }) ;
Parser.^compose; # }
say Parser.HOW.^name ;
say Parser.^methods(:local) ;
However, the reply is …
Perl6::Metamodel::GrammarHOW
(TOP)
… rather than the hoped-for …
Perl6::Metamodel::GrammarHOW
(token TOP { <S> } BUILDALL)
How should add_method
be invoked to add the TOP
token (and later, other tokens such as the S
token)?
After more work, I believe that I may have a solution …
constant Parser := Metamodel::GrammarHOW.new_type( name => 'Parser' ) ;
Parser.^add_method( 'TOP', my token TOP { <S> } ) ;
Parser.^add_method( 'S', my token S { '' | 'a' <S> 'b' } ) ;
Parser.^compose ;
say Parser.HOW.^name ;
say Parser.^methods( :local ) ;
say Parser.parse: 'aabb' ;
Output is …
Perl6::Metamodel::GrammarHOW
(token TOP { <S> } token S { '' | 'a' <S> 'b' })
「aabb」
S => 「aabb」
S => 「ab」
S => 「」
I had coded a static version of Parser
and for that static Parser
similar output to that shown above had been …
(token TOP { <S> } token S { '' | 'a' 'a' <S> 'b' 'b' } BUILDALL)
I am not sure about the fact that BUILDALL
is missing from my dynamically created Parser
. I do not understand BUILDALL
and did not find much when searching on-line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,使用元模型。与每种基本类型的HOWS(高阶工作)相同,都有语法语法。不幸的是,有关元模型的信息没有很多。有 Masak < /a>提到语法,仅此而已。但是,查看代码,它本质上是一个类。如果您查看
classhow
示例,示例,并使方法是令牌,课程是语法。通常,元编程是一个迄今未被广泛覆盖的主题。可惜。
Using the metamodel, of course. Same as there are HOWs (Higher Order Working) for every basic type, there's a GrammarHOW for grammars. Unfortunately, there's not a whole lot of information on the metamodel; there's this article by Masak which mentions the GrammarHOW, and that's that. However, looking at the code, it's essentially a class; you're probably OK if you look at the
classHOW
examples, and make methods be tokens and classes be grammars.Metaprogramming, in general, is a subject that's not been extensively covered so far. Which is a pity.