简体   繁体   English

java语言添加或修改关键字

[英]Add or modify keywords in java language

I know my question does not seem valid, but it is genuine.我知道我的问题似乎无效,但它是真实的。 When writing java I must use the word import so as to import classes from classpath.在写 java 时,我必须使用import这个词,以便从类路径中导入类。 It is required to user new to initiate certain objects and other keywords in java. My question is whether we have even the slightest ability to improve and this great language by way of defining new keywords and what they do or modifying the exisiting keyword to do the same thing. new用户需要在 java 中启动某些对象和其他关键字。我的问题是,我们是否有能力通过定义新关键字及其作用或修改现有关键字来改进这种伟大的语言,甚至是丝毫能力一样。 For example instead of writing:例如而不是写:

import java.io.File;

What possibility is there to modify the import word to bosnian for example:有什么可能将import词修改为波斯尼亚语,例如:

uvoziti java.io.File;

and it all works the same way.一切都以相同的方式工作。 Please do not close before I get ideas.在我得到想法之前请不要关闭。

One approach that uses a rather sophisticated toolchain and could be considered as an "overkill", but is not as much effort as writing an own compiler or so:一种使用相当复杂的工具链并且可以被认为是“矫枉过正”的方法,但不像编写自己的编译器那样努力:

  • Download ANTLR4 from http://www.antlr.org/download.htmlhttp://www.antlr.org/download.html下载 ANTLR4
  • Download the Java Grammar at https://github.com/antlr/grammars-v4/blob/master/java/Java.g4https://github.com/antlr/grammars-v4/blob/master/java/Java.g4下载 Java 语法
  • Modify the Java Grammar according to your needs...根据需要修改Java语法...
  • Run

    java -classpath "antlr-4.4-complete.jar" org.antlr.v4.Tool Java.g4

    This will generate some files, one of them being JavaLexer.java .这将生成一些文件,其中之一是JavaLexer.java

  • Create a Java Project that contains the ANTLR JAR and the JavaLexer.java创建一个包含 ANTLR JAR 和JavaLexer.java的 Java 项目
  • Create a class like the following, which does the translation:创建一个如下所示的类,它进行翻译:

     import java.io.IOException; import org.antlr.v4.runtime.ANTLRFileStream; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.TokenStream; public class Main { public static void main(String[] args) throws IOException { CharStream s = new ANTLRFileStream("Input.javaX"); JavaLexer lexer = new JavaLexer(s); TokenStream t = new CommonTokenStream(lexer); int i = 1; while (true) { if (t.LA(i) == -1) { break; } if (t.LA(i) == JavaLexer.IMPORT) { System.out.print("import "); } else { System.out.print(t.LT(i).getText() + " "); } i++; } } }

    (of course, this is only an example that only translates the IMPORT token, which was defined in the grammar file to be "uvoziti" . For a more general and flexible translation, one would define the translation in an external file, and probably read this file to create a map Map<Integer, String> that maps JavaLexer.IMPORT to "import" etc...) (当然,这只是一个例子,翻译语法文件中定义的IMPORT token 为"uvoziti" 。对于更通用和灵活的翻译,可以在外部文件中定义翻译,并且可能阅读此文件用于创建映射Map<Integer, String>JavaLexer.IMPORT映射到"import"等...)

  • Create the input file from the example: Input.javaX :从示例创建输入文件: Input.javaX

     uvoziti java.io.File; public class Input { public static void main(String args[]) { File file = null; System.out.println("done"); } }

    When you then run the Main , it will read this input file, eventually find the IMPORT token, and instead of the original text ( uvoziti ) it will print import .当你运行Main ,它会读取这个输入文件,最终找到IMPORT标记,而不是原始文本( uvoziti ),它会打印import

  • The result will be the contents of a Java file, with an awful formatting...结果将是一个 Java 文件的内容,格式很糟糕......

     import java . io . File ; public class Input { public static void main ( String args [ ] ) { File file = null ; System . out . println ( "done" ) ; } }

    but fortuntately, the compiler does not care about the formatting: You may write this output directly to a .java file, and the compiler will swallow it.但幸运的是,编译器并不关心格式:您可以将此输出直接写入.java文件,编译器会吞下它。

As it is described here, it is only a proof of concept.正如这里所描述的,它只是一个概念证明。 For a flexible and generic translation of many (all) keywords, one would have to build some infrastructure around all that.对于许多(所有)关键字的灵活和通用翻译,必须围绕所有这些构建一些基础设施。 The input files should be read automatically ( File.listFiles() , recursively for packages).应该自动读取输入文件( File.listFiles() ,递归地用于包)。 Each of them would have to be translated (using the Map<Integer, String> that I mentioned earlier).它们中的每一个都必须被翻译(使用我之前提到的Map<Integer, String> )。 Then the output files would have to be written and compiled, either with the runtime JavaCompiler , or by manually invoking the javac with Runtime#exec .然后必须使用运行时JavaCompiler或通过使用Runtime#exec手动调用javac来编写和编译输出文件。

But in general, I think that this should be doable within a few hours in the best case, and within one week when considering that everything takes longer than you think.但总的来说,我认为这在最好的情况下应该可以在几个小时内完成,考虑到一切都比你想象的要长,一周内应该可以做到。

Writing an own Java compiler, on the other hand, might take a bit longer, even when you consider that everything takes longer than you think...另一方面,编写自己的 Java 编译器可能需要更长的时间,即使您认为一切都比您想象的要长......

Java doesn't provide any way to redefine keywords. Java 不提供任何重新定义关键字的方法。

If you add or remove keywords to the Java language, then it isn't Java anymore.如果您在 Java 语言中添加或删除关键字,那么它就不再是 Java。

You could write your own language that compiles to Java.您可以编写自己的语言来编译为 Java。 This could be as simple as writing a program that does a string replace of uvoziti for import and then runs it through the javac compiler.这可能就像编写一个程序一样简单,该程序对uvoziti进行字符串替换以进行import ,然后通过javac编译器运行它。

As an option, use something like preprocessor or write your own one, to process java code via replacement of bosnian words to english ones before passing this code to the javac compiler.作为一种选择,在将此代码传递给 javac 编译器之前,使用预处理器之类的东西或编写自己的预处理器,通过将波斯尼亚语单词替换为英语单词来处理 java 代码。

I think this approach should work for your case.我认为这种方法应该适用于您的情况。

Java by itself doesn't help you in this. Java 本身在这方面对您没有帮助。

You might want to pass your code through a pre-processor, but things start to look a bit crazy: Can I have macros in Java source files .您可能希望通过预处理器传递您的代码,但事情开始看起来有点疯狂: 我可以在 Java 源文件中使用宏吗 I've never done something like this, so I'm not sure it will work as intended.我从来没有做过这样的事情,所以我不确定它会按预期工作。

Also, consider that after this change your code is only readable by people understanding bosnian.另外,请考虑在此更改之后,您的代码只能由了解波斯尼亚语的人阅读。

You can't really solve this to work generically;你不能真正解决这个问题来通用; java strictly defines the keywords in its language specification and there is no mechanism in the java language to add keywords (eg macros). java在其语言规范中严格定义了关键字,java语言中没有添加关键字(例如宏)的机制。

A partial solution would be to create a preprocessor that translates your new keywords into plain java.部分解决方案是创建一个预处理器,将您的新关键字转换为纯 java。 Needless to say that this is a pain to integrate into common tool chains and you won't get useful compiler error messages any more for constructs created by the preprocessor.毋庸置疑,将其集成到通用工具链中是一件痛苦的事情,并且对于预处理器创建的构造,您将不再获得有用的编译器错误消息。

One step further would be to write your own compiler;更进一步的是编写自己的编译器; again this integrates poorly with existing toolchains.同样,这与现有工具链的集成很差。 You still don't get proper support from your IDE.您仍然没有从您的 IDE 获得适当的支持。

As fascinating as the idea is;就像这个想法一样迷人; the obstacles make it highly impractical for generic use.这些障碍使得通用用途非常不切实际。

The situation is different in languages that come with a compile time macro language (most assembler langauges have this).在带有编译时宏语言的语言中情况有所不同(大多数汇编语言都有这个)。 C's define is another example. C 的定义是另一个例子。 They still all have the problem that they are preprocessor based, so added constructs are more complicated (only basic syntax checking etc.)它们仍然存在基于预处理器的问题,因此添加的构造更加复杂(只有基本的语法检查等)

JVM and JDK understands Java key words. JVM 和 JDK 理解 Java 关键字。 If you want to change into you language, then you have to change JDK and JVM also.如果你想改变你的语言,那么你也必须改变 JDK 和 JVM。

Just use a preprocessor.只需使用预处理器。 If java doesn't have one, then write one.如果java没有,那就写一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM