[antlr-interest] Multiplication and pointers in C-style language

Grzegorz Cieslewski cieslewski at hcs.ufl.edu
Wed Apr 9 04:40:10 PDT 2008


On Wed, Apr 9, 2008 at 5:39 AM, Gavin Lambert <antlr at mirality.co.nz> wrote:
> At 19:01 9/04/2008, David Olsson wrote:
>
> > Actually, I have been looking quite a lot at the C example but as a novice
> when it comes to ANTLR and constructing languages I don't really see how and
> why it works. Perhaps I should rephrase my question; can anyone explain how
> and why the ANSI C example grammar works with the multiplication and pointer
> situation described below? :-)
> >
>
>  I'm not sure if the example shows it (I haven't looked at it in a while),
> but this sort of problem requires semantic disambiguation.  Specifically,
> you need to examine the first identifier token and figure out whether it's a
> type or a variable.  You should be able to do this since both types and
> variables are required to be declared before they are used.
>
>

David,

I think all you need to add to your grammar is a list of allowed types
as separate tokens like:
type_specifier
	: 'void'
	| 'char'
	| 'short'
	| 'int'
	| 'long'
	| 'float'
	| 'double'
	| 'signed'
	| 'unsigned';

This way there would be no ambiguity (I think)

Greg Cieslewski


More information about the antlr-interest mailing list