【问题标题】:Parsing and converting 4Test to Perl解析 4Test 并将其转换为 Perl
【发布时间】:2011-03-29 17:46:22
【问题描述】:

我想将 4Test 脚本转换为 Perl。我一直在 Perl 中使用 Parse::RecDescent,但仍然对这项任务感到不知所措。这是一个例子。

4Test 的一个例子是这样的:

ParseSMSPlans (STRING sDir)
{
STRING sFile;
STRING sDirSMSPlan = sDir + "sms_plans\";
STRING sDirPlan = sDir + "plan\";
STRING sDirDeal = sDir + "deal\";
STRING sDirProduct = sDir + "product\";
STRING sLine, sType, sName;
HFILE hIn;
FILEINFO fiFile;
LIST OF FILEINFO lfInfo = SYS_GetDirContents (sDirSMSPlan);
       ...
}

... 这是我的 Parse::RecDescent 语法

my $grammar = q{

#-----------------identifiers and datatypes-------------------#

    identifier : /[a-z]\w+/
    binops : '+' | '-' | '/' | '*' | '%' | '**'
    lbinops: '!' | '<' | '>' | '>='| '<='| '&&'| '||' | '=='
    integer: /\d+/ {print "hello $item[-1]" if $::debugging;}
    number : /(\d+|\d*\.\d+)/ {print "hello $item[-1]" if $::debugging;}
    string : /"(([^"]*)(\\")?)*"/
    operation : number binops number operation(s?)
    datatype : /[a-zA-Z]\w*/
    definition : datatype expression(s) #{print "hello $item[-1]" if $::debugging;}
                |datatype expression(s) "=" expression(s) #{print "hello $item[-1] = $item[-2]" if $::debugging;}
    statement : ifexp | elsexp | elseifexp |forexp | feachexp | whexp | swcexp


#------------------Expressed Values-----------------------------#
    program : expression
    expression :  number {print $item[1] if $::debugging}
                | integer
                | assignment
                | operation
                | identifier binops expression
                | number binops expression

#------------------Conditionals---------------------------------------#

    ifexp  : 'if' '(' expression(s) ')' '{' expression(s) '}' elsexp(?)
    elsexp : 'else' '{' expression(s) '}'
    elseifexp: 'else' 'if' '(' expression(s) ')' '{' expression(s) '}'
    forexp : 'for' '(' expression ';' expression ';' expression ')' '{' expression(s)  }'
           | 'for' assignment 'to' number expression(s) | 'for' assignment 'to' number '{' expression(s) '}'
    feachexp : 'for each' expression 'in' expression '{' expression(s) '}'
    whexp  : 'while' '(' expression ')' '{' expression(s) '}'
    casest : 'case' expression(s /,/) ':'
    swcexp : 'switch' identifier '{' casest(s) '}' expression(s) 'default'
    assignment : identifier(s) '=' expression
};

所以,我正在考虑将“$”添加到每个变量名中,然后切碎 数据类型。在大多数情况下,我的语法是有效的,尽管我已经完全 测试了它,但只是因为 Parse::RecDescent 有点 我很难理解,我不确定它是否是最好的 完成我的任务的方法……或者最快的,就此而言。

我主要关心的是是否有人认为 PRD 可以处理 我要求它做还是简单(复杂)正则表达式就足够了?我会 感谢任何人可以提供的任何帮助。

【问题讨论】:

  • 你看过 Regexp::Grammars 吗?
  • 您需要一个完整的 4test 解释器还是只是转换几个脚本?

标签: perl parsing interpreter


【解决方案1】:

我可以建议您通过将所有运算符表达式替换为子例程调用来简化您的 4Test 脚本。然后,您仍然可以将它们作为 4Test 脚本运行,从而证明它们有效,但是您将大大简化解析问题 - 运算符表达式比直接过程调用更难解析。到了极限,这个过程会产生几乎可以直接被Perl+一些4Test函数替换例程运行的4Test脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 2019-05-25
    • 2013-01-17
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多