antlr4

Troubles with returns declaration on the first parser rule in an ANTLR4 grammar

╄→гoц情女王★ 提交于 2019-12-22 02:40:17
问题 I am using returns for my parser rules which works for all parser rules except the first one. If the first parser rule in my grammer uses the returns declaration ANTLR4 complains as follows: expecting ARG_ACTION while matching a rule If I add another parser rule above which does not use "returns" ANTLR does not complain. Here you have a grammar reduced to the problem: grammar FirstParserRuleReturnIssue; ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*; aRule returns [String s]:

Get Preprocessor lines with antlr4 with parsing C Code

≯℡__Kan透↙ 提交于 2019-12-21 21:24:46
问题 I am using Antlr4 to parse C code and I am using the following grammar to parse: Link to C.g4 The above grammar by default does not provide any parsing rules to get preprocessor statements. I changed the grammar slightly to get the preprocessor lines by adding the following lines externalDeclaration : functionDefinition | declaration | ';' // stray ; | preprocessorDeclaration ; preprocessorDeclaration : PreprocessorBlock ; PreprocessorBlock : '#' ~[\r\n]* -> channel(HIDDEN) ; And in Java I am

Antlr4 - Is there a simple example of using the ParseTree Walker?

浪尽此生 提交于 2019-12-21 09:43:16
问题 Antlr4 has a new class ParseTreeWalker. But how do I use it? I am looking for a minimal working example. My grammar file is 'gram.g4' and I want to parse a file 'program.txt' Here is my code so far. (This assumes ANTLR has run my grammar file and created all of the gramBaseListener , gramLexer , etc etc): import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; import static org.antlr.v4.runtime.CharStreams.fromFileName; public class launch{ public static void main(String[] args) {

Antlr4 - Is there a simple example of using the ParseTree Walker?

走远了吗. 提交于 2019-12-21 09:41:09
问题 Antlr4 has a new class ParseTreeWalker. But how do I use it? I am looking for a minimal working example. My grammar file is 'gram.g4' and I want to parse a file 'program.txt' Here is my code so far. (This assumes ANTLR has run my grammar file and created all of the gramBaseListener , gramLexer , etc etc): import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; import static org.antlr.v4.runtime.CharStreams.fromFileName; public class launch{ public static void main(String[] args) {

Ignoring whitespace (in certain parts) in Antlr4

主宰稳场 提交于 2019-12-21 06:04:03
问题 I am not so familiar with antlr. I am using version 4 and I have a grammar where whitespace is not important in some parts (but it might be in others, or rather its luck). So say we have the following grammar grammar Foo; program : A* ; A : ID '@' ID '(' IDList ')' ';' ; ID : [a-zA-Z]+ ; IDList : ID (',' IDList)* ; WS : [ \t\r\n]+ -> skip ; and a test input foo@bar(X,Y); foo@baz ( z,Z) ; The first line is parsed correctly whereas the second one is not. I don't want to polute my rules with the

Is it advisable to use tokens for the purpose of syntax highlighting?

℡╲_俬逩灬. 提交于 2019-12-21 05:57:36
问题 I'm trying to implement syntax highlighting in C# on Android, using Xamarin. I'm using the ANTLR v4 library for C# to achieve this. My code, which is currently syntax highlighting Java with this grammar, does not attempt to build a parse tree and use the visitor pattern. Instead, I simply convert the input into a list of tokens: private static IList<IToken> Tokenize(string text) { var inputStream = new AntlrInputStream(text); var lexer = new JavaLexer(inputStream); var tokenStream = new

Using Antlr for parsing data from never-ending stream

空扰寡人 提交于 2019-12-21 04:42:21
问题 Is Antlr suitable for parsing data from streams that don't have EOF right after the text to parse? According to my observation, the lexer does not emit the current token until the first character of next token is received. On top of that - the parser seems not to emit the rule until the first token of next rule is received. Here is a simple grammar I tried: fox: 'quick' 'brown' 'fox' '\r'? '\n' ; Then I used the generated parser with UnbufferedCharStream and UnbufferedTokenStream: CharStream

How do I pretty-print productions and line numbers, using ANTLR4?

我的梦境 提交于 2019-12-21 04:27:08
问题 I'm trying to write a piece of code that will take an ANTLR4 parser and use it to generate ASTs for inputs similar to the ones given by the -tree option on grun ( misc.TestRig ). However, I'd additionally like for the output to include all the line number/offset information. For example, instead of printing (add (int 5) '+' (int 6)) I'd like to get (add (int 5 [line 3, offset 6:7]) '+' (int 6 [line 3, offset 8:9]) [line 3, offset 5:10]) Or something similar. There aren't a tremendous number

Antlr4 C# targets and output path of generated files

ⅰ亾dé卋堺 提交于 2019-12-20 10:58:37
问题 I have a C# solution with an Antlr3 grammar file, and I'm trying to upgrade to Anltr4. It turns out the grammar was the easy part (it became better, and one third the size!). Generating the parser turned out to be the tricky part. In the old solution I merely ran AntlrWorks to update the lexer and parser .cs files when the grammar file changed. The lexer and parser were included directly in the same project as the grammar so the framework around the parser could make use of them directly.

Why does not ANTLR4 match “of” as a word and “,” as punctuation?

自闭症网瘾萝莉.ら 提交于 2019-12-20 06:38:44
问题 I have a Hello.g4 grammar file with a grammar definition: definition : wordsWithPunctuation ; words : (WORD)+ ; wordsWithPunctuation : word ( word | punctuation word | word punctuation | '(' wordsWithPunctuation ')' | '"' wordsWithPunctuation '"' )* ; NUMBER : [0-9]+ ; word : WORD ; WORD : [A-Za-z-]+ ; punctuation : PUNCTUATION ; PUNCTUATION : (','|'!'|'?'|'\''|':'|'.') ; WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines Now, if I am trying to build a parse tree from the following