[antlr-interest] Grammar Question

Alexey Demakov demakov at ispras.ru
Sun Jun 5 22:52:39 PDT 2005


Hi Andrew,

----- Original Message ----- 
From: "Andrew Deren" <andrew at adersoftware.com>
To: <antlr-interest at antlr.org>
Sent: Monday, June 06, 2005 7:54 AM
Subject: [antlr-interest] Grammar Question


> I'm working on a small language with syntax similar to basic. 
> One of the expression types is array access, where you can use parenthesis
> for accessing the array member:
> x = somearray(3)
> 
> the problem I'm having is that this somearray(3) could also be function
> call. Currently I'm creating FunctionCall for those and converting those to
> ArrayAccess at type checking stage. 
> I don't like doing that, so are there any ways around it? I'm constructing
> heterogeneous immutable AST, so just for this little thing as a hack I allow
> modification of the expression tree. The tree is constructed by the parser. 

Why not have single node for both cases? It is responsibility
of semantic checker and code generator (or interpreter) to process
them correctly.

> The future problem could be that someone could define array with name
> somearray and function somearray(int), so which expression should be used?
> The language spec allows function and variable names to be the same (I
> didn't write it and can't change it).
> If anyone has experience with basic, how do they handle it?

Language specification must describe this case. If not, you have
ambiguity and have to resolve it. There are two ways:
1. Prefer one case to other. So, if there are function somearray(int) 
and array named somearrray, somearray(1) will always be the sunction call.
Or array access if you like :)
2. Report about semantic error. So, user have to rename function or array.

Regards,
Alexey

P.S. How your AST is described? May be you will be interested in
my work on special language (TreeDL) for description 
of heterohenous tree structure. See link below.

-----
Alexey Demakov
TreeDL: Tree Description Language: http://treedl.sourceforge.net
RedVerst Group: http://www.unitesk.com




More information about the antlr-interest mailing list