使用 Parse::RecDescent 进行打印和串联
我正在测试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有完全明白你的问题。您仅将
STRING
定义为标记,尚未为其指定任何语义操作。下面的规则(出现在教程中)告诉我们如何处理令牌。instruction
规则表示一条指令可以是赋值指令,也可以是打印指令。print
和表达式
并打印表达式的结果。'='
和表达式,并将表达式的结果赋给%main::VARIABLE
哈希中的名称。expression
有两个操作,一个用于调用&main::expression
的复合表达式,另一个用于从%main:: 检索指定值的变量变量哈希。
您刚刚创建了一个令牌类型,但没有规则依赖于该令牌。此外,我了解了您想要做什么的要点,但还不足以为我为您想要做的事情创建语义操作提供指导。
由于不知道您到底想将指令放在哪里,我只是将以下情况添加到
表达式
中:这意味着它会查找精确的字符串
'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.instruction
rule says an instruction is either a assignment or a print instruction.print
and anexpression
and prints the result of the expression.'='
, 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
: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 toinstruction
and defined it as:And I added to the examples:
which prints
$v
.