The file with .g4 extension contains the grammer for a parser called ANTLR 4. The ANTLR 4 runtime is required to recognize the G4 file. After installation the parsing tool recognize the G4 content as a language which need to be translated in a typical language. Once the parser works it generates the code for the target programming languages such as, Java, C++ or c#. Therefore, it is important to have an ANTLR runtime installed to let the output or generated code work.
G4 file format
Here is a simple example which shows how a grammer is contained in a G4 file. put the following grammar inside file Hello.g4 and save it in a temp directory
// Define a grammar called Hello
r : 'hello' ID ; // match keyword hello followed by an identifier
ID : [a-z]+ ; // match lower-case identifiers
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines