[antlr-interest] Multiplication and pointers in C-style language
Gavin Lambert
antlr at mirality.co.nz
Thu Apr 10 14:41:07 PDT 2008
At 01:06 11/04/2008, David Olsson wrote:
>Unfortunately, my language is not like C in all cases.
>Specifically, my language is a two pass language. In the
>first pass, all the parsing is done. In the second, type checks
>and such is done. This means that I don't need a preprocessor
>nor includes. This, however, also means that there is no easy
way
>of telling if an identifier is a type or a variable during
>parsing; that information isn't available until the second pass.
>
>I've looked at Terence Parr's example Java grammar but since
there
>are no pointers in Java it is of little help.
You could possibly have a look at a C# grammar; I think there's a
C# ANTLR grammar floating around somewhere, but failing that you
could look at the official spec.
C# has pointers (though they're rarely used) and doesn't require
pre-declaration of types (no header files), so that's probably the
closest example to what you're trying to do.
I don't know offhand, but I think C# gets around the problem by
declaring that expressions cannot be statements in their own
right; combining this with the fixed signatures for operator
overloading means that a * on the left of an assignment or on its
own can only possibly be a pointer declaration, not a
multiplication, since a multiplication cannot produce an
lvalue. (Unlike in C++, where you can overload it to return
whatever you want.)
>Maybe the only solution is to change the pointer syntax so that
>there are no conflicts with other operators (the benefit of
>inventing your own language ;-))...
That might be the easiest option, but it's not the only one :)
More information about the antlr-interest
mailing list