-2

I'm implementing abstract syntax tree metamodel (ASTM) for my java parser. Now I done with parse tree with my grammar file. Now I tried to map parse tree to astm. I'm totally confused to map which to which, mapped If statement to IfStatement.java class but no idea to map conditions, thenbody, elsebody etc. Is there is any document for mapping?

ASTM reference link: http://www.omg.org/news/meetings/workshops/ADM_2005_Proceedings_FINAL/T-3_Newcomb.pdf astm source: https://github.com/adolfosbh/cs2as/tree/master/org.xtext.example.delphi/emf-gen/org/xtext/example/delphi/astm https://hal.inria.fr/hal-00752688/file/sosym-gra2mol.pdf

  • At least show us what you have so far. And explain where and why exactly you have problems implementing it on your own. Read [ask]. – Zabuzard Jun 08 '18 at 15:05
  • 1
    If you chose to use this particular AST definition because you have the sense that it is somehow an official or standardised AST, you will probably end up frustrated. There is no standardised AST style, and the only reason to use someone else's would be the existence of useful tools or libraries; that doesn't seem to be the case here. By all means use it as an example (if you cannot find a more accessible example) but remember that the point if an AST is to abstract only the information you need in your application from a concrete syntax. – rici Jun 08 '18 at 15:09

1 Answers1

0

As indicated in the example diagram on page 63 of your first link, the condition of an if statement is an EXPRESSION and the then and else clauses are each "EXEC-STATEMENT-CONTENTS". (I couldn't find the definition of that class but I suppose it describes a sequence of statements.) The semantics specific to the if statement are encapsulated by the fact that these standard objects are particular attributes of the if statement; they don't need to be different classes.

If that seems confusing, consider a simple Pixel class:

struct Pixel {
  int x;
  int y;
};

Note that it is not necessary (or even desirable) to invent specific classes for "integers which are X-coordinates" and "integers which are Y-coordinates". Both are just integers and the particular semantics are a feature of the Pixel class itself.

In the same way, then clauses and else clauses are both just statement lists.

In fact, in Java they are single Statements, although that is not true of every language. In Python, for example, they are a sequence of statements but not a scope, while in Lua they are an ordinary block (which is a scope). In many languages, including Java, the else clause is optional (unlike the diagram I referred to which seems to insist that there is exactly one).

rici
  • 234,347
  • 28
  • 237
  • 341