Created
January 26, 2012 17:52
-
-
Save anonymous/1684022 to your computer and use it in GitHub Desktop.
ANTLR Lexer Ambiguity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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