简体   繁体   English

如何在JVM上执行它? (带有Antlr的Lexer和Parser)

[英]How Execute it On JVM ? (Lexer & Parser with Antlr)

I want to create own programming language on JVM 我想在JVM上创建自己的编程语言

for first step I try to write very simple statement (Adding eg 3+7) 第一步,我尝试编写非常简单的语句(添加例如3 + 7)

so I Created a lexer and a parser with Antlr 所以我用Antlr创建了一个词法分析器和解析器

grammar gr;
formula : Digit Add Digit Equal;
Digit   
  :  '0'..'9'  
  ;  
Add     : '+';
Equal   : '=';    
WS : [\t\r\n]+ -> skip ;

then 然后

Antlr generated Lexer,Parser and Listener Antlr生成了Lexer,Parser和Listener

I used them 我用过

import java.io.IOException;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;

public class gr {
    public static void main(String[] args) throws IOException {

        String str = "7+8=";        
        ANTLRInputStream input = new ANTLRInputStream(str);     
        grLexer lexer = new grLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        grParser parser = new grParser(tokens);
        ParseTree tree = parser.formula();
        System.out.println(tree.toStringTree(parser));  
    }
}

Result : 结果:

(formula 7 + 8 =)

now my problem is Here !!! 现在我的问题是在这里!

How I can execute this line (7 + 8 =) on JVM ??? 我如何在JVM上执行这一行(7 + 8 =)? (get Result 15) (获得结果15)

seems I must use ASM library ? 似乎我必须使用ASM库? Yes ??? 是的???

If your answer is yes please help me for very simple sample 如果您的回答是肯定的,请帮助我提供非常简单的示例

please guide me how use JVM for create my own language 请指导我如何使用JVM创建自己的语言

From your text I assume that you are not very familiar with how the CPU executes statements, which is the basic knowledge necessary to write your own programming language. 根据您的文字,我认为您不太了解CPU如何执行语句,这是编写您自己的编程语言所必需的基本知识。

To get that you should first spend some time with a real assembler language , which you can do online or offline. 为此,您首先应该花一些时间使用真正的汇编语言 ,您可以在线或离线进行汇编 I strongly recommend to read some tutorials on this as this is anything but trivial. 我强烈建议您阅读一些有关此的教程,因为这并不重要。 Learning Assembler will give you a much better understanding what the CPU actually does when it has to execute some code. 学习汇编器将使您更好地了解CPU在必须执行某些代码时的实际作用。

You don't have to become an Assembler expert, but once you have some understanding of it, it becomes suddenly very clear what to do to get the JVM - essentially a CPU emulator - to execute your code: you need to compile it into a form that the JVM can execute, then simply tell it to do that. 您不必成为汇编专家,但是一旦您对它有了一些了解,就会突然很清楚地知道如何使JVM(本质上是CPU仿真器)执行代码:您需要将其编译为一个JVM可以执行的形式,然后简单地告诉它执行该操作。

Edit: Seems I guessed wrong. 编辑:似乎我猜错了。 ;) ;)

Java bytecode instruction listings: Wikipedia Java字节码指令清单: Wikipedia
Devailed VM spec: Oracle 无效的VM规格: Oracle

Have a look at this other question dealing with emitting and running Java bytecode. 看一下另一个有关发出和运行Java字节码的问题。

How to emit and execute Java bytecode at runtime? 如何在运行时发出和执行Java字节码?

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

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