Odd bugs happening with whitespace in .g4 file.

24 views
Skip to first unread message

William Nodvik

unread,
Apr 13, 2018, 3:41:08 PM4/13/18
to antlr-discussion
Hi all, 

I am new to ANTLR and writing a parser using the intellij tool (I tried to use the command line tool, but that is another discussion). As a test, I downloaded and ran the plugin on the calculator grammer. There were errors at first, but eventually I managed to get a working parse tree after deleting some whitespace (mostly indentation) in the .g4 file. 

My question is, how does ANTLR4 read whitespace and what cases whitespace gets in the way of ANTLR and where it is acceptable? I am fine with not using indentation if I need to, but it seems like most .g4 grammars have indentation and plenty of whitespace to make it more readable.

Also, to clarify, this has nothing to do with dealing with whitespace when making a grammar, I am having trouble with whitespace as I physically type out my grammar and run the intellij tool. 

Thanks everybody. 

Mike Lischke

unread,
Apr 14, 2018, 8:22:33 AM4/14/18
to azrdev via antlr-discussion
Hi William,
I am new to ANTLR and writing a parser using the intellij tool (I tried to use the command line tool, but that is another discussion). As a test, I downloaded and ran the plugin on the calculator grammer. There were errors at first, but eventually I managed to get a working parse tree after deleting some whitespace (mostly indentation) in the .g4 file. 

That’s very strange. I just tested the grammar and ANTLR4 generated the parser/lexer files without any problem. What was the exact errors you got?


My question is, how does ANTLR4 read whitespace and what cases whitespace gets in the way of ANTLR and where it is acceptable? I am fine with not using indentation if I need to, but it seems like most .g4 grammars have indentation and plenty of whitespace to make it more readable.

ANTLR4 is bootstrapped by itself, meaning it uses a parser + lexer generated by itself. Look in the grammar directory. There’s an ANTLR4 grammar too. And because of that the whitespace handling is exactly the same like for any other grammar:

// -------------------------
// Whitespace

WS  :   Ws+     -> channel(OFF_CHANNEL) ;

Whitespaces end up in a separate channel.
Reply all
Reply to author
Forward
0 new messages