Skip to content

Instantly share code, notes, and snippets.

@MrCoder
Created June 11, 2017 12:05
Show Gist options
  • Save MrCoder/b892a5db17ccee704e12cd297fa4faaf to your computer and use it in GitHub Desktop.
Save MrCoder/b892a5db17ccee704e12cd297fa4faaf to your computer and use it in GitHub Desktop.
private static final String someLangSourceCode =
"class SomeClass {\n"+
" fun1 {\n"+
" instruction11\n"+
" instruction12\n"+
" }\n"+
" fun2 {\n"+
" instruction21\n"+
" instruction22\n"+
" }\n"+
"}";
public static void main( String[] args ) throws IOException {
System.out.println( "Hello world!" );
try {
CodePointCharStream codePointCharStream = CharStreams.fromString(someLangSourceCode, "string");
SomeLanguageLexer lexer = new SomeLanguageLexer(codePointCharStream);
TokenStream tokens = new CommonTokenStream(lexer);
SomeLanguageParser parser = new SomeLanguageParser(tokens);
parser.classDeclaration().children.forEach(System.out::println);
} catch (RecognitionException e) {
throw new IllegalStateException("Recognition exception is never thrown, only declared.");
}
}
@MrCoder
Copy link
Author

MrCoder commented Jun 11, 2017

g4 file must be put under src/your/package/full/name/grammar-filename.g4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment