解析器/词法分析器/编程语言问题

发布于 2024-10-20 06:26:29 字数 552 浏览 6 评论 0原文

好吧,我不擅长所有术语和内容,但这是我迄今为止创建的内容:

1:一个逐字符读取源代码文件内容的应用程序。和;

2:一个确定字符/字符串“x”是否是标识符、整数或其他什么的应用程序。

3:另一个应用程序将它们放在一起,并创建另一个文件,该文件基本上包含要发送到我的第四个应用程序的指令,基本上如下:

将 Integer oldValue 的值分配给 Integer newValue。

因此,然后我的第四个应用程序收到此指令,并“编译”它(好吧,在这种情况下,我想说我只想解释它),然后它将创建一个执行以下操作的应用程序:

int newValue = oldValue;

所以,因为我有解析器等已经完成了,我怎样才能将我的指令翻译成导致实际操作的指令,并生成一个EXE文件,这样当我双击该文件时,它就会执行上述操作?

我希望这不会让您感到困惑。

所以,基本上,我想我要问的是:“如何以编程方式从字符串创建“事件”,将其保存到文件并生成 exe?”

Okay, so I'm not good with all the terms and stuff, BUT here's what I have created thus far:

1: An app that reads character by character the contents of a source code file. And;

2: An app that determines whether character/string 'x' is an identifier, or integer, or whatever.

and 3: Another app that puts it all together, and creates another file that basically contains instructions to be sent to my 4th app, basically along the lines of:

Assign the value of Integer oldValue to Integer newValue.

So, then my 4th app receives this instruction, and "Compiles" it (well, in this case I'd say that I just want to interpret it), which will then create an application that does:

int newValue = oldValue;

So, since I have the parser etc already done, how can I translate my instructions into instructions that lead to actual actions, and generate an EXE file, so when I double-click the file, it will perform the above-mentioned action?

I hope this itsn't confusing for you.

So, basically, I guess what I'm asking is: "How can I programmatically create an "Event" from a string, save it to file and generate an exe?"

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

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

发布评论

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

评论(4

枫林﹌晚霞¤ 2024-10-27 06:26:29

有多种选项可用于从指令列表生成 EXE。所有选项都涉及您将指令转换为不同语言的元素。

There are several options to generate an EXE from a list of instructions. All options involve that you transform your instructions to elements of a different language.

_畞蕅 2024-10-27 06:26:29

这是使用我在注释中提供的内容创建简单可执行文件的真实工作示例:

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace AssemblyGenerator
{
    partial class AssemblyGenerator
    {
        static void Main(string[] args)
        {
            string assemblyName = "Sample";
            string exeName = "Sample.exe";

            AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.Save);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(exeName);
            TypeBuilder typeBuilder = moduleBuilder.DefineType(assemblyName, TypeAttributes.Public | TypeAttributes.Class);
            MethodBuilder methodBuilder = typeBuilder.DefineMethod("Main", MethodAttributes.Static | MethodAttributes.Public, typeof(void), null);

            assemblyBuilder.SetEntryPoint(methodBuilder, PEFileKinds.ConsoleApplication);

            ILGenerator ilGenerator = methodBuilder.GetILGenerator();
            // BEGIN method IL.
            ilGenerator.EmitWriteLine("This shows on console output.");
            ilGenerator.Emit(OpCodes.Ret);
            // END method IL.

            typeBuilder.CreateType();
            assemblyBuilder.Save(exeName);
        }
    }
}

您可以按照注释指示在方法块内发出代码。

此外,您还可以使用 LambdaExpression.CompileToMethod 来使用表达式方法而不是ILGenerator

This is a real working sample for a simple executable creation using what I have provided in the comment:

using System;
using System.Reflection;
using System.Reflection.Emit;

namespace AssemblyGenerator
{
    partial class AssemblyGenerator
    {
        static void Main(string[] args)
        {
            string assemblyName = "Sample";
            string exeName = "Sample.exe";

            AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.Save);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(exeName);
            TypeBuilder typeBuilder = moduleBuilder.DefineType(assemblyName, TypeAttributes.Public | TypeAttributes.Class);
            MethodBuilder methodBuilder = typeBuilder.DefineMethod("Main", MethodAttributes.Static | MethodAttributes.Public, typeof(void), null);

            assemblyBuilder.SetEntryPoint(methodBuilder, PEFileKinds.ConsoleApplication);

            ILGenerator ilGenerator = methodBuilder.GetILGenerator();
            // BEGIN method IL.
            ilGenerator.EmitWriteLine("This shows on console output.");
            ilGenerator.Emit(OpCodes.Ret);
            // END method IL.

            typeBuilder.CreateType();
            assemblyBuilder.Save(exeName);
        }
    }
}

You can emit your code inside the method block as indicated by the comments.

Also, you can use an expression using the LambdaExpression.CompileToMethod Method instead of the ILGenerator.

飘然心甜 2024-10-27 06:26:29

当涉及到编译阶段时,最好使用 Reflection发出 API 以发出 .NET IL 并生成 EXE 和 DLL。

我不会在这里详细介绍所有内容,而是向您推荐以下 MSDN 文章 创建一种语言.NET Framework 的编译器,其中包含可为您提供帮助的示例代码。

另一种选择包括生成 C# 代码并使用 CodeDOM 将代码编译为EXE,这将利用 C# 编译器作为代码生成和最终二进制生成之间的中间步骤。

When it comes to the compilation phase your best be is to use the Reflection Emit APIs to emit the .NET IL and generate EXEs and DLLs.

Rather than get into everything over here I would point you to the following MSDN article Create a Language Compiler for the .NET Framework, which includes sample code that should help you on your way.

Another option includes generating C# code and using the CodeDOM to compile the code to an EXE, this would leverage the C# compiler as an intermediate step between your code generation and the final binary generation.

与之呼应 2024-10-27 06:26:29

最简单的方法可能是让您的程序生成某种语言(例如 C)的源代码并将其输出到您将单独编译的文件。这省去了您必须进行代码生成的麻烦,因为代码生成可能相当复杂。

根据任务的复杂性,使用 Yacc(和 Lex)等工具可能会更容易。这些允许您指定如何识别您关心的输入部分和语言的语法,以及识别结构时要输出的内容。您不必为所有 C# 指定语法,只需为您的任务指定您关心的部分即可。

The easiest way is probably to have your program generate source code for some language (e.g. C) and output that to a file which you would compile separately. That saves you the trouble of having to do code generation which can be rather involve.

Depending on the complexity of your task, it may be easier to use a tool such as Yacc (and Lex). These allow you to specify how to identify the parts of input you care about and a grammar for the language, and what is to be output as constructs are identified. You won't have to specify a grammar for all of C#, just the portions you care about for your task.

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