7
votes

I want to create a very simple grammar with space indentation. Each line consists of 1 or more words but indentation like python (4 spaces or a tab is one indent) and there is no close for indentation, for example:

if something cool occurs
    do this
else
    otherwise do this
    loop around something
       each time doing this
       and do that
say good byte

Rather than read each line, calculate the indentation and build a tree manually is it possible to do all of that in ANTLR grammar? My target language is Java.

1

1 Answers

1
votes

This is possible. All you do is define a rule and let it be skipped.

Here you go:

Ignore :  (' ' | '\t' | '\n' | '\r')+ {skip();}; 

Or if you need to recognize \n or \r

Ignore :  (' ' | '\t')+ {skip();}; 

Add this to your gramar and all spaces and tabs will be ignored.