c# - Should type information be encoded in parse tree? -


I am working on a project with a small DSL. In the results of this language, to parse a string and parse it in a parse tree, it is implemented as an abstract class named XP, after which there are many general derivative classes like assignments XP, INVQUEXXP, ADISXXP, ET etc., Which are related to the nodes of the pars, which are assignments, function invocations, add and so forth. The project has been implemented in C #.

I am currently considering the implementation of type estimation for this DSL. This means that I want to take an example of EXP class and be able to withdraw some encoding information about different types of nodes in the tree. This type of information depends on a symbol table (types of variables) and function tables (function signatures). In this way, I would like to do something like this:

  typed exposed typed expired = avatar type (xp., Symboltable, functiontable)   

here Expressed typing exemptions, ideally except the type of expression giving expression type, however, the following design problems have been presented:

  1. This type of XP for XP To the successor It will and will simply apply an additional property, Type. However, it will create two parallel succession hierarchy, one for typpedAssignmentExpr, TypedInvokeExpr et cetera and one Expr (AssignmentExpr, InvokeExpr, et cetera). It is inconvenient to maintain, and the problem increases if more of the trees of the pen are needed. I'm not sure how this can be reduced. One possibility would be the bridge design pattern, but I do not think it is capable of solving the problem completely.

  2. Alternatively, EXP can simply apply a type of property, which is then null at the time of creation from the parser, and later the type conclusion is full of algorithm. However, passing the objects with empty fields invites NullReferenceExceptions. TypedExpr The idea will be reduced Besides, the idea of ​​XP Class is to express a per -se tree, type information is not really a part of the tree: typing is context-sensitive, and special symbols and function tables are required.

  3. Third, the Type Peripheral method is only a dictionary & lt; Xpro, Type & gt; Which types information about all the nodes, it would mean that the speakers only represent the representation of the pars tree. The drawback of this is that there is no apparent quality in the construction of the dictionary object that it is specifically linked to the express object near the type concept method.

    I

    My question is, what are the advantages and drawbacks of different ways of this problem? Type of information should be encoded directly into the parse tree, or parallel tree class should be used? Or dictionary solution is best? Is there an accepted "best practice" solution?

    Proceed with option two. This can be considered a best practice.

    The reason for this is that a compiler usually works in many passes (steps, steps). The first one can be parsing, type-resolution one and you can add an optimization pass, a code generation, etc. later. Typically, a single data structure maintains an abstract syntax tree (AST or pers-tree) on the basis of these passes.

    Zero> Objects passing through tap areas, NullReferenceExceptionsÃÆ'à ⠀ ™ Ã⠀ šÃ,Â? You must handle invalid cases in order to validate input / output anyway. Compiler with simple expression processors are very complex things operated by complex rules, which include a high degree of data structure complexity and application logic, which you can not just escape from.

    It is normal for data not being normal for a AST , in addition to the initial construction of AST by Parser, each compilation passes, then manipulates AST, and Calculates more information (like your type resolution step). AST may also change substantially due to adaptation pass.


    Side note: Modern compilers, such as the latest C # compiler AST and others employ a non-variability policy, internal data structures, in that case each has its own new data Builds the structure. You can then create a new set of data structure for each pass, but it can change to a very complex code to maintain it. Anyone from the C # compiler team can expand on this topic.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -