Skip to content

Instantly share code, notes, and snippets.

Created January 26, 2012 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1684022 to your computer and use it in GitHub Desktop.
Save anonymous/1684022 to your computer and use it in GitHub Desktop.
ANTLR Lexer Ambiguity
grammar Ambig;
options {
output=AST;
ASTLabelType=CommonTree;
}
// --------------------------------------------------------------------------
ID : ('a'..'z' | 'A'..'Z') ('0'..'9' | 'a'..'z' | 'A'..'Z' | ' ')*;
name : ID;
// --------------------------------------------------------------------------
PITCH : (('A'|'a') '#'?)
| (('B'|'b') '#'?)
| (('C'|'c') '#'?);
note : PITCH;
main : name ':' note '\n'? { System.out.println($main.tree.toStringTree()); };
import org.antlr.runtime.*;
public class Ambig {
public static void main(String[] args) throws Exception {
ANTLRInputStream input = new ANTLRInputStream(System.in);
AmbigLexer lex = new AmbigLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lex);
AmbigParser parser = new AmbigParser(tokens);
parser.main();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment