antlr4

Syntactic errors on optional rules in Antlr 4 doesn't work as expected

心已入冬 提交于 2019-12-02 08:02:39
I am using Antlr 4.7.2. I am trying to implement an "if else" statement: Main problem is that optional rule is not being included on ParseTree, for this reason I think I am not getting the syntax's errors on that optional rule. Ones of my current grammar definition are: prog : stat+ ; stat : func_declaration #rFuncDeclStat | if_stat #rIfStat | while_stat #rWhileStat | for_stat #rForStat | 'return' expr? STAT_END #rReturnStat | LET ID ('=' expr)? STAT_END #rVarDeclStat | var_reference '=' expr STAT_END #rAssignStat | print_stat #rPrintStat | expr STAT_END #rFuncCallStat ; block_stat : '{' stat*

ANTLR mismatched input

强颜欢笑 提交于 2019-12-02 06:15:08
I have a simple grammar like this: grammar mygrammar; the_rule : 'abc' 'xyz' ; WS : [ \t\r\n\u000C]+ -> channel(HIDDEN) ; When I parse the text "abc xyz" with the_rule(), I get the expected string tree representation: (the_rule abc xyz) However, then I add the following lexer rule, which I think means "anything except for a w": TEXT : ~[w]+ ; Now when I parse the text "abc xyz" with the_rule(), I get an error: line 1:0 mismatched input 'abc xyz' expecting 'abc' Why would the TEXT lexer rule affect this? I think I'm misunderstanding something simple. :-( ANTLR lexer rules are greedy: the rule

Antlr4: The following sets of rules are mutually left-recursive

霸气de小男生 提交于 2019-12-02 04:37:18
I am trying to describle simple grammar with AND and OR , but fail with the following error The following sets of rules are mutually left-recursive The grammar is following: expr: NAME | and | or; and: expr AND expr; or: expr OR expr; NAME : 'A' .. 'B' + ; OR: 'OR' | '|'; AND: 'AND' | '&'; Simultaneously, the following grammar expr: NAME | expr AND expr | expr OR expr; NAME : 'A' .. 'B' + ; OR: 'OR' | '|'; AND: 'AND' | '&'; does compile. Why? ANTLR4 supports only direct left recursion (which is already an improvement over previous versions). That means you can have left recursion in a single

How to force ANTLR to parse all input CharStream

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 02:44:50
I'm using ANTLR4 to parse a syntax file. When I use BaseErrorListener to detect errors, I got a problem. When faced with an illegal input string, ANTLR automatically matches the appropriate branch and then ignores the subsequent stream of characters even if it contains errors. And I want to detect that error. Here are my g4 file and java file. TransitionLexer is my lexer file and TransitionCondition is my parser file. ErrorDialogListener.java is my errorListener and Test.java id main java file. TransitionLexer.g4 lexer grammar TransitionLexer; BOOLEAN: 'true' | 'false'; IF: 'if'; THEN: 'then';

ANTLR4: How to inject tokens

我只是一个虾纸丫 提交于 2019-12-02 00:04:20
I'm trying to implement a preprocessor for a DSL, modeled after the CPP example in code/extras. However, I'm not using token factory. Is one required? Calling emit(token) does not inject the tokens into the tokens stream as expected. Here's the lexer: // string-delimited path SPATH : '"' (~[\n\r])*? '"' { emit(); // inject the current token // launch another lexer on the include file, get tokens, // emit them all at once here List<CommonToken> tokens = Preprocessor.include(getText()); if (null != tokens) { for (CommonToken tok : tokens) { emit(tok); } } } ; Here's the include method:

ANTLR4 parse tree to DOT using DOTGenerator

情到浓时终转凉″ 提交于 2019-12-01 23:01:10
How do I use DOTGenerator to convert a parse tree to DOT/graphviz format in ANTLR4? I found this related question but the only answer uses TreeViewer to display the tree in a JPanel and that's not what I'm after. This other question is exacly what I need but it didn't get answered. Everything else I stumbled upon relates to DOTTreeGenerator from ANTLR3 and it's not helpful. I'm using Java with the ANTLR4 plugin for IntelliJ. I have a small project that has all kind of utility methods w.r.t. ANTLR4 grammar debugging/testing. I haven't found the time to provide it of some proper documentation so

Why am I getting an error when assigning tokens to a channel?

醉酒当歌 提交于 2019-12-01 22:23:07
I have the following code in my .g4 file. @lexer::members{ public static final int WHITESPACE = 1; public static final int COMMENTS = 2; } WS : (' '|'\t'|'\f')+ -> channel(WHITESPACE) ; COMMENT : '//' ~('\n'|'\r')* -> channel(COMMENTS) ; LINE_COMMENT : '/*' .*? '*/' NEWLINE? -> channel(WHITESPACE) ; I'm getting the following errors: warning(155): Shiro.g4:239:34: rule 'WS' contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output warning(155): Shiro.g4:243:38: rule 'COMMENT' contains a lexer command with an unrecognized constant value; lexer

Antlr error/exception handling

心已入冬 提交于 2019-12-01 21:25:00
问题 After doing some research online I found that this would be the way to catch the exceptions and output my own error messages. For some reason I still cannot seem to catch the errors. Below is the code for a class that overrides antlrs default error handling. All I want to do is catch the exception from antlr and output to the screen that the syntax is incorrect in a java gui. public class ExceptionErrorStrategy extends DefaultErrorStrategy { @Override public void recover(Parser recognizer,

Antlr error/exception handling

放肆的年华 提交于 2019-12-01 21:04:10
After doing some research online I found that this would be the way to catch the exceptions and output my own error messages. For some reason I still cannot seem to catch the errors. Below is the code for a class that overrides antlrs default error handling. All I want to do is catch the exception from antlr and output to the screen that the syntax is incorrect in a java gui. public class ExceptionErrorStrategy extends DefaultErrorStrategy { @Override public void recover(Parser recognizer, RecognitionException e) { throw e; } @Override public void reportInputMismatch(Parser recognizer,

Antlr4 maven plugin cannot find grammar files in different directories

大兔子大兔子 提交于 2019-12-01 20:26:06
问题 I'm using the antlr4 maven plug-in to build my maven project which uses antlr4: <groupId>org.antlr</groupId> <artifactId>antlr4-maven-plugin</artifactId> <version>4.0</version> I started with one grammar file and got my pom.xml set-up and everything was building nicely. Then I decided to split my grammar into logical parts and therefore used several grammar files but in different directories (so the generated code would be put into separate packages) but still all under the same root src/main