From: | Jeff Kenton <jeffrey.kenton@comcast.net> |
Newsgroups: | comp.compilers |
Date: | Sat, 19 May 2007 09:47:10 -0400 |
Organization: | Compilers Central |
References: | 07-05-067 |
Keywords: | parse, symbols |
Posted-Date: | 20 May 2007 22:07:21 EDT |
Ryan,
A few things are unclear. First, look at your language and make sure
that there's nothing that's truly ambiguous. Second, it looks from your
example as if identifiers aren't necessarily declared before they're
used, except perhaps in function declarations. If that's true, when do
you know what they are? Are they declared anywhere? Dimensioned? Or
else, what are the rules for assuming types and dimensions? Furthermore,
in your example it seems that "i" is declared in the statement "Function
do_something(i As Integer)". What information about "i" are you missing?
In general, you should be building the symbol table as you parse. If
there is information about an identifier that's missing, that should be
explicitly noted as you proceed. If it's still missing when you finish
parsing, you will need to report errors or apply defaults, depending on
the language specifications.
Overall, my first reaction is that you language sample looks a little
confusing. Your function declarations have parentheses but your
function calls don't. You seem to have mixed your "Dim" statement with
an assignment. Even if your language isn't exactly ambiguous, you might
find that you have made it difficult to parse (and to code) correctly,
and that a little redesign would help. In any case, when you're
parsing, you should be gathering as much information as you can, as soon
as you can. Even noting that information is missing and will have to be
found later is useful to know.
Good luck,
jeff
Ryan Dary wrote:
> I'm working on a phase of my compiler. The language is similar to
> Visual Basic, and I want to know if I'm parsing this correctly.
> ...
>
> Function do_something( i As Integer ) As Integer
> Dim a As Integer = i + 10
> do_something_else i
> Return a * 5
> End Function
> ... So, I'm not building a symbol tree at this phase... the
> problem with that seems to be that I'm unable to make heads or tails
> of the lines of code within the function declaration. 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.
Return to the
comp.compilers page.
Search the
comp.compilers archives again.