halfwarp
halfwarp

Reputation: 1790

No spaces between tokens in ANTLR

I'm writing a very simple subset of a C# grammar as an exercise.

However, I have a rule which whitespaces are giving me some troubles.

I want to distinguish the following:

int a;
int? b;

Where the the first is a "regular" int type and the second is a nullable int type.

However, with my current grammar I'm not being able to parse this.

type     : typeBase x='?'    -> { x == null } typeBase
                             -> ^('?' typeBase)
         ;

typeBase : 'int'
         | 'float'
         ;

The thing is that whith these rules, it only works with a whitespace before '?', like this:

int ? a;

Which I'd don't want.

Any ideas?

Upvotes: 1

Views: 1840

Answers (1)

Arne Deutsch
Arne Deutsch

Reputation: 14769

1) Your definition of whitespace seems to be flawed ... the grammar you present should accept "int?" and "int ?". Maybe you should take a look of the definition of whitespace.

2) If you want to disallow "int ? a" you can define extra tokens 'int?' and 'float?' ... normally you allow whitespace to appear between every token, so you have to make it one token.

Upvotes: 2

Related Questions