使用 Parse::RecDescent 进行打印和串联

发布于 2024-09-16 02:38:45 字数 823 浏览 1 评论 0原文

我正在测试 P::RD 教程 中的语法为了发展我自己的语法。 我还没有弄清楚如何打印字符串声明并将“$”附加到它的前面。 例如,“STRING sDir”应打印出“$sDir”。很简单,只需执行 $string =~ s/STRING /\$/ 即可,但是如果有赋值呢?例如。 “STRING sDir = aNewDir”。

这是语法,

OP       : m([-+*/%])      # Mathematical operators
INTEGER  : /[-+]?\d+/      # Signed integers
VARIABLE : /\w[a-z0-9_]*/i # Variable
STRING   : /STRING/i       # String declaration

expression : INTEGER OP expression
          { return main::expression(@item) }
          | VARIABLE OP expression
          { return main::expression(@item) }
          | INTEGER
          | VARIABLE
          { return $main::VARIABLE{$item{VARIABLE}} }

我开始认为正则表达式就足够了,但想知道如何为逗号分隔的声明创建一个复杂的语法,例如“STRING,foo,bar” - > $foo; $酒吧;

I am testing the grammar from P::RD tutorial in order to develop my own grammar.
I haven't figured out how to print a string declaration and append '$' to the front of it.
For example "STRING sDir" should print out "$sDir". It is simple enough to just do a $string =~ s/STRING /\$/, but what about the case where there is assignment? eg. "STRING sDir = aNewDir".

Here is the grammar

OP       : m([-+*/%])      # Mathematical operators
INTEGER  : /[-+]?\d+/      # Signed integers
VARIABLE : /\w[a-z0-9_]*/i # Variable
STRING   : /STRING/i       # String declaration

expression : INTEGER OP expression
          { return main::expression(@item) }
          | VARIABLE OP expression
          { return main::expression(@item) }
          | INTEGER
          | VARIABLE
          { return $main::VARIABLE{$item{VARIABLE}} }

I am starting to think that regex will suffice, but want to know how to create a complex one for comma separated declarations such as "STRING, foo, bar" -> $foo; $bar;

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

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

发布评论

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

评论(1

青衫儰鉨ミ守葔 2024-09-23 02:38:50

我没有完全明白你的问题。您仅将 STRING 定义为标记,尚未为其指定任何语义操作。下面的规则(出现在教程中)告诉我们如何处理令牌。

print_instruction : /print/i expression
                  { print $item{expression}."\n" }
assign_instruction : VARIABLE "=" expression
                   { $main::VARIABLE{$item{VARIABLE}} = $item{expression} }

instruction : print_instruction
            | assign_instruction

startrule: instruction(s /;/)
  • 底部规则表示我们正在寻找一个或多个指令,以分号分隔。
  • instruction 规则表示一条指令可以是赋值指令,也可以是打印指令。
  • 打印指令采用单词print表达式并打印表达式的结果。
  • 赋值指令采用变量、文字 '=' 和表达式,并将表达式的结果赋给 %main::VARIABLE 哈希中的名称。
  • expression 有两个操作,一个用于调用 &main::expression 的复合表达式,另一个用于从 %main:: 检索指定值的变量变量哈希。

您刚刚创建了一个令牌类型,但没有规则依赖于该令牌。此外,我了解了您想要做什么的要点,但还不足以为我为您想要做的事情创建语义操作提供指导。

由于不知道您到底想将指令放在哪里,我只是将以下情况添加到表达式中:

         | 'STRING' /[\p{Alpha}_]\w*/
         { return '

这意味着它会查找精确的字符串'STRING',后跟标识符序列。所以你可以像这样使用它:

print STRING vvv

并获取$vvv


编辑

如果我正确理解您的评论,我向 instruction 添加了 string_instruction 规则,并将其定义为

string_instruction : 'STRING' /[\p{Alpha}_]\w*/
                   { print "\$item[2]"; }

:我在示例中添加了:

print "STRING v\n";  $parser->startrule( "STRING v" );

打印 $v

. $item[2] }

这意味着它会查找精确的字符串'STRING',后跟标识符序列。所以你可以像这样使用它:

并获取$vvv


编辑

如果我正确理解您的评论,我向 instruction 添加了 string_instruction 规则,并将其定义为

:我在示例中添加了:

打印 $v

I'm not completely following your question. You've only defined STRING as a token, you haven't given it any semantic actions. It's the rules below--which appear in the tutorial--which tell us what to do with the tokens.

print_instruction : /print/i expression
                  { print $item{expression}."\n" }
assign_instruction : VARIABLE "=" expression
                   { $main::VARIABLE{$item{VARIABLE}} = $item{expression} }

instruction : print_instruction
            | assign_instruction

startrule: instruction(s /;/)
  • The bottom rule says that we're looking for one or more instructions, separated by a semi-colon.
  • The instruction rule says an instruction is either a assignment or a print instruction.
  • The print instruction takes the word print and an expression and prints the result of the expression.
  • The assignment instruction takes a variable, literal '=', and an expression and assigns the result of the expression to the name in the %main::VARIABLE hash.
  • expression has two actions, one for a compound expression which calls &main::expression and one for variables which retrieves the named value from the %main::VARIABLE hash.

You have just created a token type, but no rule depends on that token. Further more, I get a gist of what you want to do, but not enough that I can give guidance on creating semantic actions for what you want to do.

Not knowing where exactly you wanted to put the instruction, I just added the following case to expression:

         | 'STRING' /[\p{Alpha}_]\w*/
         { return '

It means it looks for the exact string 'STRING' followed by an identifier sequence. So you can use it like so:

print STRING vvv

And get $vvv.


EDIT:

If I understand your comment correctly, I added a string_instruction rule to instruction and defined it as:

string_instruction : 'STRING' /[\p{Alpha}_]\w*/
                   { print "\$item[2]"; }

And I added to the examples:

print "STRING v\n";  $parser->startrule( "STRING v" );

which prints $v.

. $item[2] }

It means it looks for the exact string 'STRING' followed by an identifier sequence. So you can use it like so:

And get $vvv.


EDIT:

If I understand your comment correctly, I added a string_instruction rule to instruction and defined it as:

And I added to the examples:

which prints $v.

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