如何通过编程(动态)创建Raku语法?

发布于 2025-02-08 05:11:29 字数 2018 浏览 2 评论 0原文


假设语法G具有两种作品……

  1. S→
  2. λ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 …

  1. S → λ
  2. 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 技术交流群。

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

发布评论

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

评论(1

瀞厅☆埖开 2025-02-15 05:11:29

当然,使用元模型。与每种基本类型的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.

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