Quick verification on AST's

Started by
20 comments, last by thre3dee 17 years ago
Quote:Original post by Sneftel
"/ 5" isn't an expression.

The proper subtree for 3*4/5, assuming left-to-right and equal precedence (and using ~ for division to avoid graphical ambiguity), would look like
    *      / \     3   ~        / \       4   5   


I think you are mistaken. His tree looks correct yours does not. Evaluating a binary expression tree starts from the bottom up usually with a postorder traversal. in your case you will do 4/5 first and then multiply that result by 3.
Advertisement
Quote:Original post by thre3dee
1: * *= / /=
2: + +=
3: - -=

?


Where did you get this, and why do you think this is common?
SoftwareGuy256 is right - the OP's first AST is in fact correct. I just ran that expression through my Java subset compiler and looked at the AST printout, and it matches what he has.

The divide being above the multiply does NOT mean that division has a higher or lower precedence than multiplication; it just means that the 3*4 is being done before the divide by 5. This implies that binary operations are left associative, which is the case in C-like languages. If you don't believe me, try running the following program and tell me what you get: (you should get a result of 2, with "three", "four", then "five" being printed out.)

int three() {	printf("three\n");	return 3;}int four() {	printf("four\n");	return 4;}int five() {	printf("five\n");	return 5;}int main() {	int x = three()*four()/five();	printf("%d\n", x);	return 0;


In general, if you have a sequence of binary operations with the same precedence level and you want to capture left associativity, you place the rightmost operator as the root, which is what the OP has done.
Quote:Original post by ITGER
SoftwareGuy256 is right - the OP's first AST is in fact correct.


Yes.

Quote:If you don't believe me, try running the following program and tell me what you get: (you should get a result of 2, with "three", "four", then "five" being printed out.)


int x = three()*four()/five();


You're confusing precedence and order of evaluation. With only precedence rules applying, the code above could very well print "five", "three" then "four" or any other permutation of these.

However, I agree that the only way to get 2 as a result of this operation is for the OP's tree to be correct. Sneftel's would have yielded 3*(4/5) = 3*0 = 0 instead.

Quote:Original post by ITGER
SoftwareGuy256 is right - the OP's first AST is in fact correct. I just ran that expression through my Java subset compiler and looked at the AST printout, and it matches what he has.

The divide being above the multiply does NOT mean that division has a higher or lower precedence than multiplication; it just means that the 3*4 is being done before the divide by 5. This implies that binary operations are left associative, which is the case in C-like languages. If you don't believe me, try running the following program and tell me what you get: (you should get a result of 2, with "three", "four", then "five" being printed out.)

int three() {	printf("three\n");	return 3;}int four() {	printf("four\n");	return 4;}int five() {	printf("five\n");	return 5;}int main() {	int x = three()*four()/five();	printf("%d\n", x);	return 0;


In general, if you have a sequence of binary operations with the same precedence level and you want to capture left associativity, you place the rightmost operator as the root, which is what the OP has done.

Exactly. It's pretty much sorting the operations based on priority, so in theory a sort of equal priority will not change for left-associativity. Thus: 3 * 4 / 5 will evaluate the multiplication first then divide the result (which is how common math works as well).

You learn this in early math class:
1: Exponentiation
2: Multiplication and Division
3: Addition and Subtraction
Quote:Original post by ToohrVyk
You're confusing precedence and order of evaluation. With only precedence rules applying, the code above could very well print "five", "three" then "four" or any other permutation of these.


Oh right, now that I think about it, the C standard doesn't specify a particular order in which subexpressions are evaluated.

Hey thre3dee, out of curiosity, how did you produce those pretty AST pictures? Did you draw those by hand, or write code to generate them?

Something like that would be crazy useful for debugging a code generator. Being able to graphically see your AST, in something other then textual form.

Best I've ever come up with is using a tree view to display certain expressions, but even that isn't perfect.
Quote:Original post by GnomeTank
Hey thre3dee, out of curiosity, how did you produce those pretty AST pictures? Did you draw those by hand, or write code to generate them?

Something like that would be crazy useful for debugging a code generator. Being able to graphically see your AST, in something other then textual form.

Best I've ever come up with is using a tree view to display certain expressions, but even that isn't perfect.


Actually I simply used MS Visio to draw them up. That'd actually be a great idea.. You could even generate an image like mine of the AST or something.

I'll just stick to MS VS2005's object info trees..
Quote:Original post by thre3dee
Quote:Original post by GnomeTank
Hey thre3dee, out of curiosity, how did you produce those pretty AST pictures? Did you draw those by hand, or write code to generate them?

Something like that would be crazy useful for debugging a code generator. Being able to graphically see your AST, in something other then textual form.

Best I've ever come up with is using a tree view to display certain expressions, but even that isn't perfect.


Actually I simply used MS Visio to draw them up. That'd actually be a great idea.. You could even generate an image like mine of the AST or something.

I'll just stick to MS VS2005's object info trees..


Object Info trees? Never heard of such a thing, unless I have and I just don't know it by that name. Is it one of those magical hidden controls and/or tools in VS2005?
Quote:Original post by GnomeTank
Quote:Original post by thre3dee
Quote:Original post by GnomeTank
Hey thre3dee, out of curiosity, how did you produce those pretty AST pictures? Did you draw those by hand, or write code to generate them?

Something like that would be crazy useful for debugging a code generator. Being able to graphically see your AST, in something other then textual form.

Best I've ever come up with is using a tree view to display certain expressions, but even that isn't perfect.


Actually I simply used MS Visio to draw them up. That'd actually be a great idea.. You could even generate an image like mine of the AST or something.

I'll just stick to MS VS2005's object info trees..


Object Info trees? Never heard of such a thing, unless I have and I just don't know it by that name. Is it one of those magical hidden controls and/or tools in VS2005?


When ur debugging in VS2005 u place the mouse over a symbol in the code and it comes up with its value or subvariables etc. u can expand it through link lists as much as u want..

This topic is closed to new replies.

Advertisement