antlr4

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

拥有回忆 提交于 2019-12-20 06:31:01
问题 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

ANTLR4 parse tree to DOT using DOTGenerator

你离开我真会死。 提交于 2019-12-20 03:31:20
问题 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. 回答1: I have a small project that has all kind of utility methods w.r

ANTLR4 parse tree to DOT using DOTGenerator

百般思念 提交于 2019-12-20 03:31:17
问题 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. 回答1: I have a small project that has all kind of utility methods w.r

How to force ANTLR to parse all input CharStream

↘锁芯ラ 提交于 2019-12-20 03:03:27
问题 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

ANTLR - mismatched input error

瘦欲@ 提交于 2019-12-19 11:40:37
问题 I have a grammar which looks like this consisting of comment and control statements of a particular language: Grammar: grammar DD; ddlist: (ddstmt| jclcomment)+; ddstmt: dd1 | dd2 | dd3 | dd4 ; dd1: JCLBEGIN ddname DDWORD 'DUMMY'; dd2: JCLBEGIN ddname DDWORD 'DYNAM'; dd3: JCLBEGIN ddname DDWORD NAME'=' ('*'|NAME); dd4: JCLBEGIN ddname DDWORD '*' inlinerec INLINESTMTEND?; inlinerec: (INLINEDATA )+ ; fragment INLINEDATA: (~[\r\n])*; ddname: NAME; jclcomment: JCLCOMMENT+; JCLCOMMENT:

ANTLR4 parse tree simplification

孤者浪人 提交于 2019-12-19 04:12:12
问题 Is there any means to get ANTLR4 to automatically remove redundant nodes in generated parse trees? More specifically, I've been experimenting with a grammar for GLSL and you end up with long linear sequences of "expressions" in the parse tree due to the rule forwarding needed to give the automatic handling of operator precedence. Most of the generated tree nodes are simply "forward to the next level of precedence", so don't provide any useful syntactic information - you only really need the

ANTLR4: Unexpected behavior that I can't understand

我只是一个虾纸丫 提交于 2019-12-18 09:45:30
问题 I'm very new to ANTLR4 and am trying to build my own language. So my grammar starts at program: <EOF> | statement | functionDef | statement program | functionDef program; and my statement is statement: selectionStatement | compoundStatement | ...; and selectionStatement : If LeftParen expression RightParen compoundStatement (Else compoundStatement)? | Switch LeftParen expression RightParen compoundStatement ; compoundStatement : LeftBrace statement* RightBrace; Now the problem is, that when I

How to create a antlr4 grammar which will parse date

浪子不回头ぞ 提交于 2019-12-18 09:08:21
问题 I want to parse few date format using following ANTLR4 grammar. grammar Variables; //varTable : tableNameFormat dateFormat? ; //tableNameFormat: (ID SEPERATOR); dateFormat : YEAR UNDERSCORE MONTH UNDERSCORE TODAY | YEAR ; YEAR : DIGIT DIGIT DIGIT DIGIT; // 4-digits YYYY MONTH : DIGIT DIGIT; // 2-digits MM TODAY : DIGIT DIGIT ; // 2-digits DD UNDERSCORE: ('_' | '-' ); fragment DIGIT : [0-9] ; ID : [a-zA-Z][a-zA-Z0-9]? ; WS : [ \t\r\n]+ -> skip ; This grammar should parse "2016-01-01" easily

How to control error handling and synchronization in Antlr 4 / c#

岁酱吖の 提交于 2019-12-18 07:14:31
问题 I'm using Antlr 4 with c# target. Here is a subset of my grammar: /* * Parser Rules */ text : term+ EOF; term : a1 a2 a3; a1: .... ... ... I want to accept valid data blocks as (term)s, when error exists I want to search for the next valid term and print out the whole text which caused the error for user to analyze manually. How to synchronize input to the next valid term? and How to get the ignored text? 回答1: You will need to create your own implementation of IAntlrErrorStrategy for this,

How to collect errors during run time given by a parser in Antlr4

淺唱寂寞╮ 提交于 2019-12-17 20:47:38
问题 I have upgraded from Antlr 3 to Antlr 4. I was using this code to catch exceptions using this code. But this is not working for Antlr 4. partial class XParser { public override void ReportError(RecognitionException e) { base.ReportError(e); Console.WriteLine("Error in Parser at line " + ":" + e.OffendingToken.Column + e.OffendingToken.Line + e.Message); } } This is the error that appears 'Parser.ReportError(Antlr4.Runtime.RecognitionException)': no suitable method found to override In Antlr 4