[Q] How Is AST structured for entire program??

heliboy2003@yahoo.com.tw
Tue, 16 Oct 2007 09:05:48 -0000

          From comp.compilers

Related articles
[Q] How Is AST structured for entire program?? heliboy2003@yahoo.com.tw (2007-10-16)
Re: [Q] How Is AST structured for entire program?? gneuner2@comcast.net (George Neuner) (2007-10-17)
Re: [Q] How Is AST structured for entire program?? DrDiettrich1@aol.com (Hans-Peter Diettrich) (2007-10-17)
Re: [Q] How Is AST structured for entire program?? cfc@shell01.TheWorld.com (Chris F Clark) (2007-10-18)
Re: [Q] How Is AST structured for entire program?? paul@paulbmann.com (Paul B Mann) (2007-10-24)
| List of all articles for this month |

From: heliboy2003@yahoo.com.tw
Newsgroups: comp.compilers
Date: Tue, 16 Oct 2007 09:05:48 -0000
Organization: Compilers Central
Keywords: AST, question

Dear all,


I am a very newbie and am learning compiler, after read (or tried to
read) many compiler books, there is one thing I haven't been able to
get a clear picture on. I understand that when we parse a program, a
parse tree can be constructed, and I can construct a parse tree for
ONE expression, but I can not see how this parse tree is constructed
for the entire program.


Let me take a simple example in C


void main()
{
int a;
int b;
int c;
int i;
a = 3;
b = 4;
c = a+b;


for (i = 0; i < 10; i++)
{
c = a-b;
}


}


Do we construct one tree for each line of expressions (Tree1 for a =
3, Tree2 for b = 4....... this I know how to do) or do we generate ONE
big tree with all the expression built into the tree (this I have no
idea how)?


Also, how is the for-loop constructed into the tree or it just simply
not built into tree and only expressions are built into tree?


Now when we go one step deeper to optimize the code, there are two
redundencies, first is c=a+b and second the for-loop, a well-optimized
code will be


void main()
{
int a;
int b;
int c;
int i;
a = 3;
b = 4;
c = a-b;
}


I am curious do we get any help from some magical arrangement of the
tree structure so that these types of redudencies can be detected or
we have to write a special routine to scan through the code for this
types of redundencies? Or my understanding of the whole thing is
incorrect?


Thanks


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.