gpt4 book ai didi

c# - 编译的 C# 是否使用反向波兰表示法?

转载 作者:行者123 更新时间:2023-11-30 14:46:50 25 4
gpt4 key购买 nike

我对反向波兰表示法很感兴趣,我读到它用于机器级别的计算,一些语言(如 F#)正在使用它,同时编译器也将 Infix 转换为 Postfix(反向波兰表示法)。

但是我不确定这是否适用于每个编译器,更具体地说是 C#。

此外,当我们讨论这个主题时,我看过这个视频 - https://www.youtube.com/watch?v=7ha78yWRDlE这解释了使用汇编代码的 Postfix。汇编代码是否使用 Postfix?

如果答案有点愚蠢,我很抱歉,但我以前没有使用过机器代码,所以我有点一头雾水。

最佳答案

考虑这个 C# 程序:

int x = 10;
int y = 100;
int z = 11;
int r = (x + y) * z - (y + x);
Console.WriteLine(r);

如果编译它然后查看编译后的汇编,你会看到这个(注意它不是汇编语言而是 IL - 中间语言,它只会在运行时编译为 native 汇编语言):

IL_0001: ldc.i4.s     10 // x variable
IL_0003: stloc.0 // store in slot 0

IL_0004: ldc.i4.s 100 // y variable
IL_0006: stloc.1 // store in slot 1

IL_0007: ldc.i4.s 11 // z variable
IL_0009: stloc.2 // store in slot 2

IL_000a: ldloc.0 // x
IL_000b: ldloc.1 // y
IL_000c: add // +
IL_000d: ldloc.2 // z
IL_000e: mul // *
IL_000f: ldloc.1 // y
IL_0010: ldloc.0 // x
IL_0011: add // +
IL_0012: sub // -
IL_0013: stloc.3 // V_3
IL_0014: ldloc.3 // V_3
IL_0015: call void [mscorlib]System.Console::WriteLine(int32)
IL_001b: ret

如您所见,我们的中缀表示法确实被 C# 编译器“转换”为反向抛光(后缀)表示法,所以

(x + y) * z - (y + x)

成为

x y + z * y x + -

关于c# - 编译的 C# 是否使用反向波兰表示法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47568366/

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