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

查看:191
本文介绍了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和wll,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 + StringTemplate

I already get this hint: Decompose expression in base operation: ANTLR + StringTemplate

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

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).此外,我可以具有产生vector的函数或表达式,以及产生常数的其他函数或表达式,并且在分析表达式时必须考虑到这一点(即vector 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天全站免登陆