Abstract syntax trees are not capable of sharing subexpression nodes because it is not possible for a node in a proper tree to have more than one parent. Although this conceptual simplicity is appealing, it may come at the cost of redundant representation and, in turn, possibly inefficiently duplicating the computation of identical terms. For this reason ASGs are often used as an intermediate language at a subsequent compilation stage to abstract syntax tree construction via parsing.
An abstract semantic graph is typically constructed from an abstract syntax tree by a process of enrichment and abstraction. The enrichment can for example be the addition of back-pointers, edges from an identifier node (where a variable is being used) to a node representing the declaration of that variable. The abstraction can entail the removal of details which are relevant only in parsing, not for semantics.
Example: Code Refactoring
For example, consider the case of code refactoring. To represent the implementation of a function that takes an input argument, the received parameter is conventionally given an arbitrary, distinct name in the source code so that it can be referenced. The abstract representation of this conceptual entity, a "function argument" instance, will likely be mentioned in the function signature, and also one or more times within the implementation code body. Since the function as a whole is the parent of both its header or "signature" information as well as its implementation body, an AST would not be able to use the same node to co-identify the multiple uses or appearances of the argument entity. This is solved by the DAG nature of an ASG. A key advantage of having a single, distinct node identity for any given code element is that each element's properties are, by definition, uniquely stored. This simplifies refactoring operations, because there is exactly one existential nexus for any given property instantiation. If the developer decides to change a property value such as the "name" of any code element (the "function argument" in this example), the ASG inherently exposes that value in exactly one place, and it follows that any such property changes are implicitly, trivially, and immediately propagated globally.
^Garner, Richard (2011). "An abstract view on syntax with sharing". Journal of Logic and Computation. 22 (6): 1427–1452. arXiv:1009.3682. doi:10.1093/logcom/exr021. The notion of term graph encodes a refinement of inductively generated syntax in which regard is paid to the sharing and discard of subterms.
^Plump, D. (1999). Ehrig, Hartmut; Engels, G.; Rozenberg, Grzegorz (eds.). Handbook of Graph Grammars and Computing by Graph Transformation: applications, languages and tools. Vol. 2. World Scientific. pp. 9–13. ISBN9789810228842.
^Barendregt, H. P.; Eekelen, M. C. J. D.; Glauert, J. R. W.; Kennaway, J. R.; Plasmeijer, M. J.; Sleep, M. R. (1987). "Term graph rewriting". In Bakker, J. W.; Nijman, A. J.; Treleaven, P. C. (eds.). PARLE Parallel Architectures and Languages Europe (PARLE 1987). Lecture Notes in Computer Science. Vol. 259. Springer. pp. 141–158. doi:10.1007/3-540-17945-3_8. ISBN978-3-540-17945-0.
Mamas, Evan; Kontogiannis, Kostas (2000). Towards Portable Source Code Representations Using XML. Seventh Working Conference on Reverse Engineering. pp. 172–182. CiteSeerX10.1.1.88.6173.