Skip to content

Instantly share code, notes, and snippets.

@mattmcd mattmcd/Hello.g4
Last active Jun 24, 2020

Embed
What would you like to do?
Simple ANTLR4 grammar example
// define a grammar called Hello
grammar Hello;
r : 'hello' ID;
ID : [a-z]+ ;
WS : [ \t\r\n]+ -> skip ;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class Hello {
public static void main( String[] args) throws Exception
{
HelloLexer lexer = new HelloLexer( new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream( lexer );
HelloParser parser = new HelloParser( tokens );
ParseTree tree = parser.r();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk( new HelloWalker(), tree );
}
}
public class HelloWalker extends HelloBaseListener {
public void enterR(HelloParser.RContext ctx ) {
System.out.println( "Entering R : " + ctx.ID().getText() );
}
public void exitR(HelloParser.RContext ctx ) {
System.out.println( "Exiting R" );
}
}
all: Hello.class
HelloParser.java: Hello.g4
antlr4 Hello.g4
Hello.class: HelloParser.java Hello.java HelloWalker.java
javac Hello*.java
hello world
@MarwanMedhat-97

This comment has been minimized.

Copy link

MarwanMedhat-97 commented Mar 21, 2020

how to execute this program

@albayraktaroglu

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.