I have the following equation

if (x > y) {
   if (y < a) { 
     x:= a
     y:= b
   } else {
    x:= b;

enter image description here

I was thinking of something like this however I've never done it with actual functions so I'm not quite sure how to proceed.

What are the steps to take in creating a parse tree for something like this?

  • $\begingroup$ What research/self-study have you done? We expect you to do a significant amount of research and self-study before asking. Parsing and parse trees are explained in standard textbooks on compilers. Have you looked at any of them? $\endgroup$ – D.W. May 6 '15 at 18:00

To produce a parse tree you need a string to parse, which you have, but you also need a grammar to tell you how well-formed (legitimate) strings are to be structured.

In your case you did not give a grammar. so that your question makes no sense. Usually this is done with context-free grammars

Furthermore I suspect that what you really want is an abstract syntax tree (AST), which is often very close to parse trees, but is supposed to be defined more abstractedly (for example with a sorted algebra), indpendently of any parsing process, and from which semantics is more easily defined. Note that, while the AST may be defined independently of any arsing process, it is usually associated to textual representation via a parsing process, though other means do exist.

Below is what an AST could look like for your example. But, as for parse trees, an AST can be properly given only if you have defined formally what the abstract syntax is for your language.

To be very complete, you need at least one formal definition that relates tree structures you may be interested in (parse trees or AST) for your text with corresponding textual strutures.

    /         |         \
   >          if        :=
  / \    ____/|\____   /  \
 x   y  /     |     \ x    b
       <     :=     :=
      / \   /  \   /  \
     y   a x    a y    b

Warning: the referenced wikipedia articles could be much improved, at least on the pedagogical side.

| cite | improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.