Recommended Posts

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?

0

Share this post

Link to post

Share on other sites

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 ;)

0

Share this post

Link to post

Share on other sites

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

0

Share this post

Link to post

Share on other sites

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.

Share this post

Link to post

Share on other sites

Guest Anonymous Poster

Guest Anonymous Poster

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.