Compiler talk....Parsing questions?

Started by
3 comments, last by GameDev.net 17 years, 2 months ago
So I am still reading "Writing Compilers and Interpreters" as suggested by a few people on this board. Progress is slow but none the less it is progress. I am now reading into parsing and have run into a few questions. The book state that declarations provide more difficulty with parsing than expressions. My question obvisously would be why? If we allow initialization of a declaration, what additional work will be required by the parser?
Advertisement
It is a good book, I thoroughly enjoyed the ending.

Declarations are a little more complicated because you need to deal with a definition and possible assignment in one go. I don't think it's a major issue and if you don't then cool, just get on with it ;)

"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
The book is good but it kills me how old the code is. So many outdated practices. Any thought on my second part of the question - If we allow initialization of a declaration, what additional work will be required by the parser?


Thanks

Chad
In addition to initialization, you have to keep track of the name in a symbol table, and make sure you can resolve the proper symbol based on the scope of the block you are in. You can declare a global variable 'x', and then declare an 'x' inside a block, which will be used only inside that block.
Quote:The book state that declarations provide more difficulty with parsing than expressions.


The other point, wich my lecturer missed entirely is that of 'inherited' atributes and 'synthesized' attributes. Consider these two ways to declare variables:

Pascal:

a, b, c: INTEGER

C:

int a, b, c;

Imagine you're doing LL (left reading, left reducing). In the pascal example you're reading variables from left to right, but you don't know the type is yet. So what do you do with the var names/labels? You have to keep track of them somewhere so you can pin the type on to them when you get to it.

In the C example, you know the type right away and can set the type attribute when you add it to the symbol table. Pascal: synthesized, C, inherited.

That's my understanding at least, if I'm wrong, please correct me!

This topic is closed to new replies.

Advertisement