[antlr-interest] Re: declarations after code

micheal_jor open.zone at virgin.net
Tue Nov 23 01:51:08 PST 2004



--- In antlr-interest at yahoogroups.com, "Maassen, H.A.M."
<H.A.M.Maassen at s...> wrote:
> Hello,
>  
> I'm in a bit of a fix :)
>  
> I'm currently writing a parser for a language that can have
variables declared after the code where they are used. In addition,
the language is ambiguous depending on the types of the variables.
>  
> For example, the following code results in two different trees
depending on the types of the variables "a", "b" and "c":
>  
> Code
>   a b c
>  
> Here, "a" could be a function that takes "b" and "c" as parameters
or "a", "b" and "c" could each be separate functions (the language has
no statement-separator).
>  
> It looks like I'll have to do two passes over the code, once to get
all the variable types and once to parse the statements (and then a
treewalk for full blown semantic checks).
>  
> Can anyone think of a cleaner solution to this problem?

Tree parsers, multiple phases and a symbol table. 

- Parse the source (entering declarations into the table as you) and
build an AST
- Walk the AST and use the symtab info to restructure your tree (your
symtab already has info about all declarations you will encounter)
- Walk the AST again and do your full blown semantics check

You can write more complex code that would reduce this to a single
post-parse AST traversal in many cases by recording (during the parse)
the AST subtrees that need fixing. Fix before the full semantic checks.

Cheers!

Micheal
ANTLR/C#





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





More information about the antlr-interest mailing list