Adding line information to my AST in OCaml

后端 未结 1 1206
小蘑菇
小蘑菇 2021-01-27 02:06

I am creating a compiler in OCaml where the grammar is as follow:

type expr =
| Cons of const
| Var of string
| List of ( expr list )
| Sum of ( expr * expr )
|         


        
相关标签:
1条回答
  • 2021-01-27 02:54

    This is more a software design question and there are a few common solutions. I will try to cover them all.

    The most common solution is to wrap your expression type into a record that has, besides the expression payload, some metadata. The type of metadata could be made abstract or concrete, it is also a matter of taste. Abstract type for metadata makes it easier to extend it later. The classical approach is implemented in OCaml compiler itself, refer to the Locations module, it will tell you how to get location information in classical OCaml Yacc/Menhir parsers.

    A slightly different approach is to index the tree and attach an identifier to each AST node. Then you can use external maps (like any associative container) to add arbitrary metadata to your AST. Such approach is used in BAP Primus Lisp Parser. The nice thing is that this approach is that it makes it easy to combine it with hash-consing. It also makes the set of attributes that you can associate with your nodes easily extensible, with the cost the mapping is now partial. (Or common choice between a record a mapping).

    Instead of storing indexes directly in the nodes, you may pick some particular method for numbering your nodes (e.g., DFS numbering). With this approach you do not need to modify your AST type, that is especially nice if you are not controlling it. An example of such approach is the Positions module from the Janestreet's parsexp library that implements the compact set of positions based on some traversal (not the DFS numbering). Unfortunately, their implementation is not general enough to be reused with different AST, but the approach is general.

    0 讨论(0)
提交回复
热议问题