How do I form a parse tree for equations? [closed]

I have the following equation

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


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?

closed as unclear what you're asking by David Richerby, Nicholas Mancuso, Luke Mathieson, Juho, Thomas KlimpelMay 7 '15 at 21:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• 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? – 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
________/|\________
/         |         \
>          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.