Java 词法分析器和解析器 [英] Java Lexer and Parser

查看:61
本文介绍了Java 词法分析器和解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xtext 为 Java 编写一个新的编辑器.我想让解析器解析主类并用实际代码替换方法调用.

I am writing a new editor for java using Xtext. I want parser to parse the main class and replace the method call by actual code.

例如

Class Test {                                                           
           public static void main(String[] args ){ 
                 System.out.println("Virag");                            
                 method();                                                        
           }                                                           
           public static method(){             
                System.out.println("Purnam");                                    
           }                                                                
}

解析后我想返回如下所述的文档.

After parsing I want to return a document like mentioned below.

Class Test {                                                           
           public static void main(String[] args ){ 
                 System.out.println("Virag");                            
                 System.out.println("Purnam");                                                        
           }                                                         

}

我在词法分析器和解析器中通过返回方法体而不是方法来实现这一点.但是后来在编辑器中,文本区域被更改并且在编辑器中执行的任何编辑都会出错.文档中的字符位置出错.如何解决这个问题?

I achieved this in lexer and parser by return of method body instead of method. But later in editor, text region gets changed and any edit performed in editor goes wrong. Character positions in documents are going wrong. How to fix this problem?

推荐答案

您可能想要使用 代码生成 Xtext 的概念,用于将 DSL 的源代码转换为内联方法主体的新人工制品.

You probably want to use the Code Generation concept of Xtext to transform the source code of your DSL into a new artefact where the methods' body are inlined.

这篇关于Java 词法分析器和解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆