Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for lexer grammar #17

Open
moy opened this issue Feb 8, 2024 · 0 comments
Open

Support for lexer grammar #17

moy opened this issue Feb 8, 2024 · 0 comments

Comments

@moy
Copy link

moy commented Feb 8, 2024

I'm currently using org.antlr.v4.gui.TestRig for ANTLR teaching to help students debug simple grammars. Since my course uses Python, I'd rather avoid exposing students to Java stuff.

antlr4-parse looks very much like what I'm looking for, but doesn't seem to work for lexer grammars:

With TestRig, I can do:

$ cat Tokens.g4
lexer grammar Tokens;

HELLO : 'hello' ;         // beware the single quotes
ID : [a-z]+ ;             // match lower-case identifiers
INT : [0-9]+ ;
KEYWORD : 'begin' | 'end' | 'for' ; // perhaps this should be elsewhere
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
$ antlr4 Tokens.g4 && javac *.java
$ echo hello | java org.antlr.v4.gui.TestRig Tokens tokens -tokens
[@0,0:4='hello',<'hello'>,1:0]
[@1,6:5='<EOF>',<EOF>,2:0]

However, it crashes when I try antlr4-parse:

$ antlr4-parse Tokens.g4 tokens -tokens 
Exception in thread "main" java.lang.ClassCastException: class org.antlr.v4.gui.Interpreter$IgnoreTokenVocabGrammar cannot be cast to class org.antlr.v4.tool.LexerGrammar (org.antlr.v4.gui.Interpreter$IgnoreTokenVocabGrammar and org.antlr.v4.tool.LexerGrammar are in unnamed module of loader 'app')
        at org.antlr.v4.semantics.SymbolChecks.checkForModeConflicts(SymbolChecks.java:313)
        at org.antlr.v4.semantics.SemanticPipeline.process(SemanticPipeline.java:110)
        at org.antlr.v4.Tool.processNonCombinedGrammar(Tool.java:371)
        at org.antlr.v4.Tool.process(Tool.java:359)
        at org.antlr.v4.tool.Grammar.<init>(Grammar.java:364)
        at org.antlr.v4.gui.Interpreter$IgnoreTokenVocabGrammar.<init>(Interpreter.java:42)
        at org.antlr.v4.gui.Interpreter.interp(Interpreter.java:141)
        at org.antlr.v4.gui.Interpreter.main(Interpreter.java:277)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant