- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 perl 脚本的新手。我正在编写脚本来读取 excel 文件并以 C 编程语法放入文本文件。
所以我的 excel 表中有如下字符串:
If ((Myvalue.xyz == 1) Or (Frmae_1.signal_1 == 1)) Then a = 1
else a = 0;
我必须将其转换为:
a = (((Myvalue.xyz == 1) || (Frmae_1.signal_1 == 1))?1:0)
这在 perl 中如何处理?
最佳答案
我不认为在代码字符串中使用正则表达式是一个特别好的主意。您输入的语法看起来不太特别,所以我们可以使用 Marpa 解析它,使用如下语法
:default ::= action => [values]
:start ::= StatementList
:discard ~ ws
StatementList ::= <Expression>+ separator => <op semicolon> bless => Block
Expression ::=
('(') Expression (')') assoc => group action => ::first
| Number bless => Number
|| Ident bless => Var
|| Expression ('==') Expression bless => Numeric_eq
|| Expression ('=' ) Expression bless => Assign
|| Expression ('Or') Expression bless => Logical_or
|| Conditional
Conditional ::=
('If') Expression ('Then') Expression
bless => Cond
| ('If') Expression ('Then') Expression ('Else') Expression
bless => Cond
Ident ~ ident
Number ~ <number int> | <number rat>
word ~ [\w]+
ident ~ word | ident '.' word
<number int> ~ [\d]+
<number rat> ~ <number int> '.' <number int>
ws ~ [\s]+
<op semicolon> ~ ';'
然后:
use Marpa::R2;
my $grammar = Marpa::R2::Scanless::G->new({
bless_package => 'Ast',
source => \$the_grammar,
});
my $recce = Marpa::R2::Scanless::R->new({ grammar => $grammar });
$recce->read(\$the_string);
my $val = $recce->value // die "No parse found";
my $ast = $$val;
一旦我们有了 AST,将它编译成类似 C 的表示就不会太复杂。可以通过一些思考来分解出具有“优化”过程的常见分配。
然而,展示如何做到这一点相当冗长,所以我将所有深入的内容都放在这个 blogpost 中。 .然后我们可以定义一个递归遍历树并发出类 C 代码的方法,例如
package Ast::Var;
...;
sub compile { my $self = shift; $self->name } # no modification needed
package Ast::Logical_Or;
...;
sub compile {
my $self = shift;
# C's "||" operator, plus parens to specify precedence
"(" . $self->l->compile . "||" . $self->r->compile . ")";
}
package Ast::Cond;
...;
sub compile {
my $self = shift;
return sprintf '(%s ? %s : %s)',
$self->cond->compile,
$self->then->compile,
$self->else->compile;
}
等对于所有其他 AST 节点类型。
关于perl - 脚本 : Read condition written in words and converting into C - ternary operator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17275748/
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题吗? 更新问题,以便 editing this post 提供事实和引用来回答它. 关闭 2 年前。 Improve
任何人都有记住标准三元语法的好技巧吗? 具体是否为“?”或':'优先。这些年来,我一直在退缩。 最佳答案 您要检查的条件有点像一个问题,因此问号位于第一位。 x > 0 ? 1 : 0 将此语句视为三
我正在使用DLR在C#中实现语言解释器,并且在使用三元运算符时遇到了一些麻烦。至此,我已经实现了基本的函数声明/调用,如下所示: F := (x) -> x + 1 F(1) # returns
如何扩展原本用于二元运算符以支持条件三元运算符 ("a ? b : c") 的调车场算法?我在这里没有看到这个问题的答案,但我有一个,所以我发布了它。 最佳答案 我的做法是添加三个新的运算符: >“?
我正在寻找一种优雅的方式来选择方法签名(重载)并根据条件传递参数。我有一个导入器,它要么生成要导入的最新文件,要么采用显式数据路径。 目前我的代码如下所示: if (string.IsNullOrEm
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
这个问题有点抢先,因为 php 7 还没有发布。我很好奇在三元运算符的上下文中如何处理飞船运算符。 所以如果我有一个前宇宙飞船的三元表达式,例如: $foo = 1; $bar = 0; echo '
在 PowerShell 中执行此操作的正确方法是什么? $action = if ($args.Length > 0) { $args[0] } else { Read-Host 'Action'
与标准 if-else 语句相比,?: 运算符有哪些优点和缺点。最明显的是: 条件?:运算符 处理直接值比较和赋值时更短、更简洁 似乎不像 if/else 结构那么灵活 标准 If/Else 可以应用
如何将多个值赋给三元运算符?那不可能吗?我这样试过,但出现错误: size === 3 ? ( var val1=999, var val2=100; ) : 0; 和 size === 3 ?
目前我正在使用: input = input ? input : 'splash'; 我知道这太冗长了。 执行此操作的更短方法是什么? 最佳答案 您可以使用“逻辑或分配”并使其更短: input =
三元运算符通常只是哲学讨论的主题:是否 a=b>5?1:0; 对来说更具可读性、更快、更酷 if(b>5) { a=1; } else {a=0;} (带或不带花括号)我通常不在乎。我喜欢我的三元运算
我想这是一个非常基本的问题,我只是想知道如何阅读这段代码: return someboolean ? new someinstanceofsomething() : someotherboolean
与标准 if-else 语句相比,?: 运算符的优点和缺点是什么。显而易见的是: 条件 ?: 运算符 在处理直接值比较和赋值时更短、更简洁 似乎不如 if/else 结构灵活 标准 If/Else 可
这个问题在这里已经有了答案: How to write ternary conditional operator? (33 个答案) 关闭6年前。 所以在 java 中我们有三元运算符 (?),它有时
我正在使用 React.js 开发一个项目,但我在使用 eslint 时遇到了错误: 不要嵌套三元表达式 这是我的代码: const MyApp = () => { return ( <> {(var
我正在使用 #version 120 将 WebGL GLSL 着色器移植到 OpenGL GLSL 桌面. 是否有任何扩展可以增加对 ?: 的支持?三元运算符? 像: #extension GL_E
有问题的行是 return pFile.exists() ?真:空;。由于它不会引发任何编译错误,对此有何解释。它最终引发了 NPE。 import java.io.File; public clas
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 7 年前。 Improv
ESLint 在我的 JS 模块中告诉我此错误消息:错误 no-unneeded-ternary Unnecessary use of Conditional Expression for defau
我是一名优秀的程序员,十分优秀!