gpt4 book ai didi

parsing - 为不同数量的规则指定通用语法 Action

转载 作者:行者123 更新时间:2023-12-02 14:16:28 24 4
gpt4 key购买 nike

我正在尝试为一个简单的 DSL 编写一个解析器,其中有十几个格式为 <statementName> <param1> <param2> ... ; 的语句。 ,其中参数数量不同。由于语句的结构非常相似(都匹配语句名称字符串,后跟一系列由名称给出的标记),并且生成的结果的结构非常相似(都存储语句名称和参数的哈希值),我想知道如何指定所需的结果结构,而不必为每个语句操作重复自己。

操作类的伪代码将帮助我指定这样的结果结构:

class FooActions {

method *_stmt ($/) {
@result[0] = make string of statement name $/[0];
@result[1] = make hash of $/[1..] with the keys being the name of the rule
at index (i.e. '"var"' for `<var=identifier>` and `"type"` for `<type>`, etc.) and
values being the `.made` results for the rules at index (see below);
return @result;
}

method identifier ($/) { return ~$/ }

method number ($/) { return +$/ }

method type ($/) { return ~$/ }
}

测试文件:

use v6;
use Test;
use Foo;
my $s;
$s = 'GoTo 2 ;';
is_deeply Foo::FooGrammar.parse($s).made, ('GoTo', {pos => 2});
$s = 'Set foo 3 ;';
is_deeply Foo::FooGrammar.parse($s).made, ('Set', {var => 'foo', target => 3});
$s = 'Get bar Long ;';
is_deeply Foo::FooGrammar.parse($s).made, ('Get', {var => 'bar', type => 'Long'});
$s = 'Set foo bar ;';
is_deeply Foo::FooGrammar.parse($s).made, ('Set', {var => 'foo', target => 'bar'});

语法:

use v6;
unit package Foo;

grammar FooGrammar is export {
rule TOP { <stmt> ';' }
rule type { 'Long' | 'Int' }
rule number { \d+ }
rule identifier { <alpha> \w* }
rule numberOrIdentifier { <number> || <identifier> }

rule goto_stmt { 'GoTo' <pos=number> }
rule set_stmt { 'Set' <var=identifier> <target=numberOrIdentifier> }
rule get_stmt { 'Get' <var=identifier> <type> }
rule stmt { <goto_stmt> || <set_stmt> || <get_stmt> }
}

最佳答案

此方法将每个语句类型表示为 Proto-regex并使用syms以避免重复语句关键字(GoTo等)。

单个语句没有操作方法。这些在下一个级别( TOP )处理,它使用 caps匹配的方法,将其转换为哈希值。

<sym> capture 用于额外关键字。该行的其余部分转换为哈希值。解决办法如下:

语法和 Action :

use v6;
unit package Foo;

grammar Grammar is export {
rule TOP { <stmt> ';' }
token type { 'Long' | 'Int' }
token number { \d+ }
token identifier { <alpha>\w* }
rule numberOrIdentifier { <number> || <identifier> }

proto rule stmt {*}
rule stmt:sym<GoTo> { <sym> <pos=.number> }
rule stmt:sym<Set> { <sym> <var=.identifier> <target=.numberOrIdentifier> }
rule stmt:sym<Get> { <sym> <var=.identifier> <type> }
}

class Actions {
method number($/) { make +$/ }
method identifier($/) { make ~$/ }
method type($/) { make ~$/ }
method numberOrIdentifier($/) { make ($<number> // $<identifier>).made }
method TOP($/) {
my %caps = $<stmt>.caps;
my $keyw = .Str
given %caps<sym>:delete;
my %args = %caps.pairs.map: {.key => .value.made};
make ($keyw,%args, );
}
}

测试:

use v6;
use Test;
use Foo;
my $actions = Foo::Actions.new;
my $s;
$s = 'GoTo 2 ;';
is-deeply Foo::Grammar.parse($s, :$actions).made, ('GoTo', {pos => 2});
$s = 'Set foo 3;';
is-deeply Foo::Grammar.parse($s, :$actions).made, ('Set', {var => 'foo', target => 3});
$s = 'Get bar Long ;';
is-deeply Foo::Grammar.parse($s, :$actions).made, ('Get', {var => 'bar', type => 'Long'});
$s = 'Set foo bar ;';
is-deeply Foo::Grammar.parse($s, :$actions).made, ('Set', {var => 'foo', target => 'bar'});

关于parsing - 为不同数量的规则指定通用语法 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49104358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com