0

I read in a Compiler Design course that the output of scanning is a sequence of pairs (token code, token position in the symbol table). I am a bit confused about the meaning of the "position" part. When the symbol table is represented as a structure whose elements can be accessed using an index, e.g., an array, "position" is clear, it means the 1st, 2nd, 99th element in the array. E.g., for source code:

if (a == b) a = a + c;

the output of scanning will be the stream: ( ..., (id,1), ..., (id,2), ..., (id,3) ) - I didn't depict the other tokens for simplicity - and the symbol table will be (a,b,c), so a on position 1, b on position 2, c on position 3 in the symbol table.

What happens when the symbol table is represented as a binary search tree? For the same source code, the symbol table tree will have a root node with key 'b'; b's left node will have key 'a' and b's right node will have key 'c'. What should the output of scanning be now ( ...,(id,?), ..., (id,?),..., (id,?))? If my tree is implemented using a Node class, should I do smth like this: ( ...,(id, reference to Node whose key = a),...)?

What about when the symbol table is a hashtable? In C# for instance, could I have a Hashtable object HashST and the output of scanning looking like this: (..., (id,pointer to HashST["a"]),...)?

I really don't know whether this is the right approach, what else could "position" mean in a tree or a hash table?

Thank you in advance!

porumbel
  • 1
  • 2
  • Some languages, like Pascal and similar languages, doesn't have *keywords*, instead they have *reserved words* which can be entered into the symbol table like any other symbol. Its "position" is simply the index at which the symbol is stored in the table. – Some programmer dude Oct 05 '14 at 10:20
  • I understand what you're saying, but the question is "Does [position in the symbol table] mean [pointer] when the table is a tree or a hashtable?". – porumbel Oct 05 '14 at 10:27
  • The "position" can be anything you want it to be, including a pointer, as long as its a way of finding the symbol data structure. – Some programmer dude Oct 05 '14 at 10:56
  • Can you put this in the Answers section for me to mark it as an asnwer? – porumbel Oct 11 '14 at 08:21

1 Answers1

0

@Joachim Pileborg wrote:

Some languages, like Pascal and similar languages, doesn't have keywords, instead they have reserved words which can be entered into the symbol table like any other symbol. Its "position" is simply the index at which the symbol is stored in the table.

The "position" can be anything you want it to be, including a pointer, as long as its a way of finding the symbol data structure.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129