From: | Uli Kusterer <ulimakesacompiler@googlemail.com> |
Newsgroups: | comp.compilers |
Date: | Sun, 20 May 2007 13:18:32 +0200 |
Organization: | Compilers Central |
References: | 07-05-067 |
Keywords: | parse, symbols |
Posted-Date: | 20 May 2007 22:08:03 EDT |
On 18.05.2007, at 06:43, Ryan Dary wrote:
> For instance,
> as I parse the Dim statement (which is used to declare a variable), I
> am able to parse the components "Dim a As Integer = <exp>" where the
> <exp> (expression) seems to be impossible to really parse without
> having a symbol table thus far in the parsing. I wouldn't know if "i"
> is a variable or a function or a constant, because I don't have any
> way of looking it up in a symbol table. So, should I be building the
> symbol table as I'm parsing the syntax tree from the tokens?
Now, I don't have any formal teaching in compilers and parsers, but
I've wiggled through quite well with books and the web and my own
attempts so far, and I think you're right here. You will need to know
whether a certain word is a user-defined type, a variable or
whatever, unless you do like PHP or some other languages which
explicitly mark variables with an operator ("$myVar" and the likes,
or have keywords uppercase and the rest lowercase, or whatever...).
Of course, depending on the nature of your language, you might get
away with breaking it up some more without having a symbol table .
The end of the line is indicated by return characters, a line that
starts with "Dim" is a variable definition/declaration, a function
call is an identifier immediately followed by a parameter list in
brackets... You can scan ahead to find out about such things for some
languages. For the code snippet you posted, that might even work.
Depends on what else the language can do.
I don't know enough about the rules of the syntax tree to tell you
whether that's a good idea, mind you. I usually just tokenize and
then parse right away, building my symbol table as I go. But then I
usually create a variant of HyperTalk, where you neither specially
mark, nor declare variables, so it's simply impossible to parse
without the kind of context provided by a symbol table.
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
Return to the
comp.compilers page.
Search the
comp.compilers archives again.