ANTLR + StringTemplate - 创建 JavaLike 语言并将其翻译成 PLSql、C 和 C++ [英] ANTLR + StringTemplate -Create a JavaLike language and translate it into PLSql, C and C++

查看:39
本文介绍了ANTLR + StringTemplate - 创建 JavaLike 语言并将其翻译成 PLSql、C 和 C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在尝试实现一个翻译器.由于它变得越来越复杂,我将尝试更好地解释我想要实现的内容.

Hello I am trying to implement a translator. Since it is coming more and more complicated I will try to explain better what I'd like to implement.

我需要指定一种新的类似 java 的语言.这种语言必须实现一个java方法的所有结构:变量声明、表达式、条件表达式、括号表达式等等......该语言将使用向量、常量和布尔值.它具有不同的功能:log、avg、sqrt 以及 sum、diff、shift 等.这种语言必须翻译成plsql和其他语言.所以定义的方法将成为 StoredProcedure 或 c++ 函数或其他任何东西.我还需要考虑数学约束,例如运算符的优先级(+、-、*、/、<<、>> 等等...).

I need to specify a new java like language. This language must implement all structure of a java method: variable declaration, expression, conditional expression, parenthesis expressions and so on... The language will work with vectors, constants and booleans. It has different function: log, avg, sqrt as wll as sum, diff, shift and so on. This language must be translated into plsql and other languages. So the method defined will become a StoredProcedure or a c++ function or whatever. I need to consider also the math constraints such as priority of operators (+,-,*,/, <<, >> and so on...).

我已经得到这个提示:在基本操作中分解表达式: ANTLR + 字符串模板

我需要知道完成任务的最佳解决方案.我想我必须以流水线方式使用您的所有解决方案,但我不想对解决方案使用试错法.

I need to know the best solution for achieving my task. I suppose I have to use all your solution in a pipelined fashion, but i don't want to use a trial and error method for the solution.

我尝试了不同的(单独的)解决方案,但将所有解决方案放在一起对我来说很难.

I tried different (separated) solutions, but putting all together is hard for me.

我的最后一个问题是分离向量和常量之间的表达式以及向量和向量之间的表达式.事实上,使用 plsql 我有不同的功能来处理这些情况.即表达式 vactor1+5(或 5+vector1)必须被翻译成 PKG_FUN.constant_sum(cursor1, 5) 而 vector1+vector2 必须被翻译成 PKG_FUN.vector_sum(vector1, vector2).此外,我可以拥有产生向量的函数或表达式,而其他产生常量的函数或表达式在分析表达式时必须考虑到这一点(即向量 a = vector1 +((5+var2)*ln(vector2)*2)^2).

My last problem is to separate an expression between vector and constant and an expression between vector and vector. In fact using plsql I have different function for handling these situations. i.e. an expression vactor1+5 (or 5+vector1) must be translated like PKG_FUN.constant_sum(cursor1, 5) instead vector1+vector2 must be translated as PKG_FUN.vector_sum(vector1, vector2). Moreover I can have functions or expressions that produce vector and other that produces constant and this must be considered when analyzing an expression (i.e. vector a = vector1 +((5+var2)*ln(vector2)*2)^2).

这种语言的一个例子可以是:

An example of this language can be:

DEFINE my_new_method(date date_from, date date_to, long variable1, long variable2){
   vector result;
   vector out1;
   vector out2; 
   int max = -5+(4);    

   out1 = GET(date_from, date_to, variable1, 20);
   out2 = GET(date_from, date_to, variable2);

   if(avg(out1) > max)
   {
       result = sqrt(ln(out2) + max)*4; 
   }else
   {
       result = out1 + ln(out1) + avg(out2);
   }

       for(int i=0; i<result.length ; i++)
       {
          int num = result.get(i);
          result.set(num*5, i);
       }

       return result;

}

我应该把它翻译成 plsql、c 或 c++ 或其他语言.

I should translate it in plsql, c or c++ or other languages.

任何帮助将不胜感激.

推荐答案

您需要的是类型推断".对于每个表达式,您需要知道其操作数的类型,以及每个运算符符号的结果类型.

What you need is "type inference". For every expression, you need to know the types of its operands, and the types of the results of each operator symbol.

您可以通过几个步骤获得:

You get this by a few steps:

1) 通过构建一个符号表来记录变量作用域中声明实体的类型

1) by building a symbol table that records the type of declared entity in your variable scopes

2) 通过遍历每个表达式,计算叶节点的类型:对于表达式,在您的语言中,至少所有常量值都是标量,并且任何标识符都具有您可以在符号表中查找的类型.对于大多数语言,在给定操作数类型的情况下,可以根据运算符的语言规则计算运算符结果的类型.(某些语言需要通过约束传播来计算类型).计算完所有这些类型后,您需要将每个树节点与其类型相关联(或者至少能够根据需要计算节点的类型).

2) by walking each expression, computing the types of the leaf nodes: for expressions, in your language at least all constant values are scalars, and any identifier has type you can look up in the symbol table. For most languages, the type of an operator result can be computed from the language rules for the operator, given its operand types. (Some language require the types to computed by constraint propagation). Having computed all of these types, you need to associate each tree node with its type (or at least be able to compute the type for a node on demand).

利用这些计算出的类型信息,您可以区分不同的运算符(例如,向量上的 +、向量第一个操作数和标量第二个操作数的 + 等),从而选择要生成的目标语言结构.

With this computed type information, you can differentiate between different operators (e.g, + on vectors, + with vector first operand and scalar second, etc.) and so choose which target language construct to generate.

ANTLR 不为您提供任何构建和管理符号表或计算类型信息的支持,除了为您提供一棵树.获得树和所有类型信息后,您可以选择使用哪个字符串模板来生成代码,从而为您提供即时风格的翻译器.所以这样做只是出很多汗.(做一个即时翻译器有一个缺点:你最好在那个地方生成你想要的确切代码,因为你没有机会优化生成的结果,这可能意味着对树进行大量的案例分析来选择什么生成).

ANTLR doesn't offer you any support in building and managing symbol tables, or in computing the type information, other than offering you a tree. Once you have the tree and all the type information, you can choose which string template to use to generate code, giving you and on-the-fly style translator. So doing this is just a lot of sweat. (Doing an on-the-fly translator has a downside: you better generate the exact code you want in that place, because you have no chance to optimize the generated result, and that likely means huge case analyses of the tree to choose what to generate).

我们的 DMS 软件再工程工具包确实为您提供了构建符号表的额外支持,以及使用其属性语法评估器计算树的推理,以及编写显式转换的额外方法,可以轻松地以此类类型查找为条件.转换从源语言的树映射到目标语言的树.然后,您可以更简单"地翻译到目标语言,并使用额外的显式转换在目标语言中应用优化.这可以大大简化翻译过程.

Our DMS Software Reengineering Toolkit does offer you such additional support for constructing symbol tables, and for computing inferences over trees with its attributed grammar evaluators, along with additional means to write explicit transformations, easily made conditional on such type lookups. The transformations map from a tree in the source language, to a tree in the target language. You can then so "simpler" translations to the target language, and apply optimizations in the target language using additional explicit transforms. This can greatly simplify the translation process.

但无论如何,对于有经验和背景的人来说,为一种语言(更不用说 3)构建一个完整的翻译器是一项艰巨的工作.您提出这个问题的事实表明您可能不了解与分析和转换代码相关的许多问题.我建议你在继续之前阅读一本好的编译器书(例如,Aho/Ullman/Sethi编译器"),否则你很可能会遇到其他类似的问题.

But in any case, building a full translator for one language (let alone 3) is a lot of work, for those that have experience and background for doing it. The fact that you have asked this question suggests you likely don't understand lots of issues related to analyzing and transforming code. I suggest you read a good compiler book (e.g., Aho/Ullman/Sethi "Compilers") before you proceed, or you are likely to run into other troubles like this.

这篇关于ANTLR + StringTemplate - 创建 JavaLike 语言并将其翻译成 PLSql、C 和 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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