AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77590226
Accepted
librasteve
librasteve
Asked: 2023-12-02 19:50:44 +0800 CST2023-12-02 19:50:44 +0800 CST 2023-12-02 19:50:44 +0800 CST

是否有可能将某个值提升到 Grammar TOP?

  • 772

我有这个 raku 语法:

#!/usr/bin/env raku
use v6.d;

use Grammar::Tracer;

grammar Grammar {
    token TOP {
          <city>      \v
          <state-zip> \v?
    }
    token city {
        ^^ \V* $$
    }
    token state-zip {
        ^^ <state> <.ws>? <zipcode> $$    #<.ws> is [\h* | \v]
    }
    token state {
        \w \w
    }
    token zipcode {
        \d ** 5
    }
}

class Actions {
    method TOP($/) {
        #works
        say "city is ",    $_ with $<city>.made;
        say "state is ",   $_ with $<state-zip><state>.made;
        say "zipcode is ", $_ with $<state-zip><zipcode>.made;

        #doesn't work
        say "state2 is ",   $_ with $<state>.made;
        say "zipcode2 is ", $_ with $<zipcode>.made;
    }

    method city($/)     { make ~$/ }
    method state($/)    { make ~$/ }
    method zipcode($/)  { make ~$/ }
}

my $address = q:to/END/;
Springfield,
IL 62704
END

Grammar.parse($address, :actions(Actions));

效果很好:

TOP
|  city
|  * MATCH "Springfield,"
|  state-zip
|  |  state
|  |  * MATCH "IL"
|  |  zipcode
|  |  * MATCH "62704"
|  * MATCH "IL 62704"
city is Springfield,
state is IL
zipcode is 62704
* MATCH "Springfield,\nIL 62704\n"

token <state-zip>是让状态和邮政编码共存于一行还是跨越多行。请记住,这是一个 MRE,因此我正在寻找不会改变语法的答案,但会改变操作的 make / made 方面。

但是 - 为了简化我的代码,我希望能够从method TOP($/) {}我的示例 state2 和 zipcode2 的操作中访问顶层的州和邮政编码。我一直在尝试将状态和令牌的制作值“提升”到匹配树上 - 例如可能是这样的:

# doesn't work
method TOP($/) {
    make $<state>.made;
    make $<zip>.made;
    ...
}
method state-zip($/) {
    make $<state>.made;
    make $<zip>.made;
}

这可能吗?

parsing
  • 2 2 个回答
  • 62 Views

2 个回答

  • Voted
  1. raiph
    2023-12-02T21:20:50+08:002023-12-02T21:20:50+08:00

    TL;DR您无法修改Match对象的捕获。我认为您可以做三件事: Ⓐ 修改匹配对象的.made属性和/或 Ⓑ 修改$/以使用其快捷方式和/或 Ⓒ 清理它?

    您无法修改Match对象的捕获

    你问的问题是很自然的事情。我记得我自己也想要它。

    在我对如何在语法中改变捕获?),我暗示你不能改变Match对象的捕获。目前在乐(do)也是如此。但我现在想知道是否可以做点什么。我将在本答案的最后一部分中回到这一点。

    修改匹配对象的.made属性

    我现在建议您使用匹配对象的.made属性,method TOP如下所示。

    保留原来的代码:

            #works
            say "city is ",    $_ with $<city>.made;
            say "state is ",   $_ with $<state-zip><state>.made;
            say "zipcode is ", $_ with $<state-zip><zipcode>.made;
    

    插入代码来创建字典,然后make:

            my %hash;        
            %hash<state> = $_ with $<state-zip><state>.made;
            %hash<zipcode> = $_ with $<state-zip><zipcode>.made;
            make %hash if %hash;
    

    保留原始代码,但从匹配对象中提取.made:

            #works
            say "state2 is ",   $_ with $/.made<state>;
            say "zipcode2 is ", $_ with $/.made<zipcode>;
    

    修改$/为使用其快捷键

    注意。本节是关于修改(分配/绑定)符号(变量)$/,以便它引用不同的对象/值。它不是关于修改已分配或绑定到的对象/值$/。

    以下是上面第一部分中的代码与一些调整的组合。这样您的代码仍然可以使用$/常用的快捷方式。

    添加一个is copy特征,以便$/可以重新分配参数:

        method TOP($/ is copy) {
            ...
    

    保持其余代码相同,直到出现以下行make:

            ...
            make %hash if %hash;
    

    $/插入对其有效负载的重新分配(或绑定).made:

            $/ .= made;
    

    现在您的代码可以切换回常用的快捷方式:

            say "state2 is ",   $_ with $<state>;
    

    注意。当您重新分配/绑定时,$/您将覆盖原始绑定到原始匹配对象。所以这停止工作:

            say "city is ",    $_ with $<city>;
    

    在本答案的最后一部分中,我概述了该问题的可能解决方案。

    清理?

    也许Match可以为对象提供新方法,允许用户将别名添加到匹配对象中的现有捕获到其他匹配对象(前提是它们是基于相同的输入字符串构造的)?

    一个更黑客的想法是创建一个新类,它就像一个空的可变类Hash(因此可以向其中添加键),但使用Match绑定到委托属性(has Match $original handles **;)的现有对象进行初始化。后者将通过故障转移来对现有对象进行查找来处理无法匹配哈希中的键的任何查找Match。(我不太确定这是否是正确的handles表达,但想发布此更新,以便我可以去睡觉。)

    脚注

    ¹任何数据都可以分配或绑定到$/,而不仅仅是Match对象。总是会出现$0别名为 to $/[0]、$1to $/[1]、$<foo>to$/<foo>和$<bar>to等的情况$/<bar>,无论分配或绑定了什么内容$/。事实上,多年来我一直在使用$/我脑海中所说的“数据变量”,它是“数据(解)结构”变量的缩写,这样“当前匹配对象”只是“数据变量”之一”的用途很多。

    • 4
  2. Best Answer
    Himanshu Sharma
    2023-12-02T21:28:40+08:002023-12-02T21:28:40+08:00

    你原来的代码:

        #works
        say "city is ",    $_ with $<city>.made;
        say "state is ",   $_ with $<state-zip><state>.made;
        say "zipcode is ", $_ with $<state-zip><zipcode>.made;
    

    创建字典的附加代码,然后make:

        my %hash;        
        %hash<state> = $_ with $<state-zip><state>.made;
        %hash<zipcode> = $_ with $<state-zip><zipcode>.made;
        make %hash if %hash;
    

    您的原始代码,但从匹配对象中绘制.made:

        #works
        say "state2 is ",   $_ with $/.made<state>;
        say "zipcode2 is ", $_ with $/.made<zipcode>;
    
    • 1

相关问题

  • 如何使用 XPath/CSS 获取名称以冒号开头的属性的值/内容?

  • 用于识别指数符号数字和标识符的 ANTLR 语法

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve