antlr

How do I declare gradle Antlr task output specs to avoid unnecessary rebuilds

筅森魡賤 提交于 2019-12-14 02:38:01
问题 I have a typical Antlr 4.5 project with two grammar files: MyLexer.g4 and MyParser.g4. From them, Antlr generates 6 output files: MyLexer.java, MyLexer.tokens, MyParser.java, MyParser.tokens, MyParserBaseListener.java and MyParserListener.java. The gradle tasks are all working correctly so that the output files are all generated, compiled and tested as expected. The problem is that gradle sees the 6 target files as always being out of date, so every run or debug session has to regenerate them

antlr4/java: pretty print parse tree to stdout

喜夏-厌秋 提交于 2019-12-14 00:17:04
问题 Beginners question: how do I print a readable version of the parse tree to stdout? CharStream input = CharStreams.fromFileName("testdata/test.txt"); MyLexer lexer = new MyLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); MyParser parser = new MyParser(tokens); parser.setBuildParseTree(true); RuleContext tree = parser.record(); System.out.println(tree.toStringTree(parser)); this prints the whole tree on a single line delimited by brackets '()'. (record (husband <4601>

Antlr greedy-option

早过忘川 提交于 2019-12-13 17:21:47
问题 (I edited my question based on the first comment of @Bart Kiers - thank you!) I have the following grammar: SPACE : (' '|'\t'|'\n'|'\r')+ {$channel = HIDDEN;}; START : 'START:'; STRING_LITERAL : ('"' .* '"')+; rule : START STRING_LITERAL; and I want to parse languages like: 'START: "abcd" START: "img src="test.jpg""' (string literals could be inside string literals). The grammar defined above does not work if there are string literals inside a string literal because for the language 'START:

Parsing Newlines, EOF as End-of-Statement Marker with ANTLR3

落花浮王杯 提交于 2019-12-13 13:30:24
问题 My question is in regards to running the following grammar in ANTLRWorks: INT :('0'..'9')+; SEMICOLON: ';'; NEWLINE: ('\r\n'|'\n'|'\r'); STMTEND: (SEMICOLON (NEWLINE)*|NEWLINE+); statement : STMTEND | INT STMTEND ; program: statement+; I get the following results with the following input (with program as the start rule), regardless of which newline NL (CR/LF/CRLF) or integer I choose: "; NL " or "32; NL " parses without error. ";" or "45;" (without newlines) result in EarlyExitException. " NL

ANTLR: How to replace specific nodes in a subtree using a Tree grammar?

烈酒焚心 提交于 2019-12-13 13:17:51
问题 I have an ANTLR grammar which creates an AST, and then I have written two tree grammars that create tree parsers for doing two passes on the AST for semantic analysis purposes. (After that I do another pass and generate output code with StringTemplate) Everything works fine so far, but I am trying to extend the language to support parametric polymorphism in functions (while so far it only supported "simple" functions). So for example I want to have something like this: T getMax<T> (T a, T b)

Python+ANTLR4: No module named antlr4

两盒软妹~` 提交于 2019-12-13 12:34:55
问题 I would like to use ANTLR4 with Python 2.7 and for this I did the following: I installed the package antlr4-4.6-1 on Arch Linux with sudo pacman -S antlr4 . I wrote a MyGrammar.g4 file and successfully generated Lexer and Parser Code with antlr4 -Dlanguage=Python2 MyGrammar.g4 Now executing for example the generated Lexer code with python2 MyGrammarLexer.py results in the error ImportError: No module named antlr4 . What could to be the problem? FYI: I have both Python2 and Python3 installed -

Replace token in ANTLR

一笑奈何 提交于 2019-12-13 12:13:16
问题 I want to replace a token using ANTLR. I tried with TokenRewriteStream and replace, but it didn't work. Any suggestions? ANTLRStringStream in = new ANTLRStringStream(source); MyLexer lexer = new MyLexer(in); TokenRewriteStream tokens = new TokenRewriteStream(lexer); for(Object obj : tokens.getTokens()) { CommonToken token = (CommonToken)obj; tokens.replace(token, "replacement"); } The lexer finds all occurences of single-line comments, and i want to replace them in the original source too.

Source of parsers for programming languages?

纵然是瞬间 提交于 2019-12-13 12:04:30
问题 I'm dusting off an old project of mine which calculates a number of simple metrics about large software projects. One of the metrics is the length of files/classes/methods. Currently my code "guesses" where class/method boundaries are based on a very crude algorithm (traverse the file, maintaining a "current depth" and adjusting it whenever you encounter unquoted brackets; when you return to the level a class or method began on, consider it exited). However, there are many problems with this

How to generate function definitions from ANTLR AST in java?

耗尽温柔 提交于 2019-12-13 09:34:10
问题 I have used Python3 grammar and have built an AST. My source string is just a small function of Python. My code is as follows: public class Main { public static void main(String[] args) { String source = "def sum( arg1, arg2 ):\r\n" + " total = arg1 + arg2\r\n" + " print \"Inside the function : \", total\r\n" + " return total;\n"; Python3Lexer lexer = new Python3Lexer(CharStreams.fromString(source)); Python3Parser parser = new Python3Parser(new CommonTokenStream(lexer)); ParseTreeWalker

Alternative between parser rule and lexer rule

丶灬走出姿态 提交于 2019-12-13 07:38:00
问题 The question is buried in the update section of another question, now specifically ask it. I am using antlr3.4. I have a simple grammar trying to parse 2 types of text, the line started with "#include" and others. Here is my grammar: cmds : cmd+ ; cmd : include_cmd | other_cmd ; include_cmd : INCLUDE DOUBLE_QUOTE FILE_NAME DOUBLE_QUOTE ; other_cmd : (~INCLUDE)+ ; INCLUDE : '#include' ; DOUBLE_QUOTE : '"' ; FILE_NAME : ('a'..'z' | 'A'..'Z' | '0'..'9' | '_')+ ; New_Line : ('\r' | '\n')+ ; WS :