antlr

Antlr for multiple language generation

被刻印的时光 ゝ 提交于 2019-12-13 07:35:38
问题 This post about the antlr simple example shows how to create and us a grammar for java. However, this intermixes the grammar and the Java source code in the Exp.g source. My Question is, Is it possible to decouple the grammar file from the target language, so that the one grammar file can be used for generating multiple Java, Scala, C++, etc Lexers/Parsers? 回答1: It depends mostly on the reason why target code is used in the grammar. Is it only action code to do something with the found tokens

Flex ‘r/s’ in ANTLv4

左心房为你撑大大i 提交于 2019-12-13 06:57:06
问题 Flex: ‘r/s’ an ‘r’ but only if it is followed by an ‘s’. The text matched by ‘s’ is included when determining whether this rule is the longest match, but is then returned to the input before the action is executed. So the action only sees the text matched by ‘r’. This type of pattern is called trailing context. (There are some combinations of ‘r/s’ that flex cannot match correctly. See Limitations, regarding dangerous trailing context.) How do this in ANTLRv4 ? 回答1: There are two primary ways

How to modify an AST from YOSYS? And how to synthesis a modified AST to Verilog code?

ⅰ亾dé卋堺 提交于 2019-12-13 06:12:42
问题 We know that we can get AST textfile of Verilog code. Now I want to modify the AST to get some new features, Is ANTLR right for this job,or which software should I use? Or How should I do? Then, I want to synthesis the modified AST to generate Verilog code? Can YOSYS finish this Job? What should I do? Can you tell me in detail? Thanks for your help! 回答1: ANTLR parses, but is not particularly good at supporting modifications to the AST or regenerating the source code accurately. Our DMS

ANTLR disable specific Userpath in Comments and returns

放肆的年华 提交于 2019-12-13 06:04:50
问题 Always when i generate Parser and Lexer from my Grammarfile, everything works fine instead of the problem, that ANTLR automaticly adds the whole User directory to any returns and comments. That is not good. E.g.: return "C:\\Users\\##\\workspace\\project\\src\\Grammar.g" also this tool does the same with any comments: // $ANTLR 3.4 C:\\Users\\##\\workspace\\project\\src\\Grammar.g 2012-06-18 18:25:20 i have to publish my project so is there any way to disable this "function" ? Regards,

Storing line number in ANTLR Parse Tree

一笑奈何 提交于 2019-12-13 05:31:37
问题 Is there any way of storing line numbers in the created parse tree, using ANTLR 4? I came across this article: http://puredanger.github.io/tech.puredanger.com/2007/02/01/recovering-line-and-column-numbers-in-your-antlr-ast/ ,which does it but i think it's for older ANTLR version, because parser.setASTFactory(factory); does not seem to be applicable for ANTLR 4. I am thinking of having something like treenode.getLine() , just like we can have treenode.getChild() 回答1: With Antlr4, you normally

Lexer to handle lines with line number prefix

谁说我不能喝 提交于 2019-12-13 04:40:01
问题 I'm writing a parser for a language that looks like the following: L00<<identifier>> L10<<keyword>> L250<<identifier>> <<identifier>> That is, each line may or may not start with a line number of the form Lxxx.. ('L' followed by one or more digits) followed by an identifer or a keyword. Identifiers are standard [a-zA-Z_][a-zA-Z0-9_]* and the number of digits following the L is not fixed. Spaces between the line number and following identifer/keyword are optional (and not present in most cases

How to multi-thread an ANTLR parser in java

本秂侑毒 提交于 2019-12-13 04:17:19
问题 I have my program that is proving slow in reading a file and then parsing it with antlr grammar. To improve performance of this I would like to multi-thread the parsing? Read File: LogParser pa = new LogParser(); LogData logrow; String inputLine; int a=0; try { //feed line by line FileReader fr = new FileReader(jFileChooser1.getSelectedFile()); BufferedReader reader = new BufferedReader(fr); while ((inputLine = reader.readLine()) != null) { try { a++; jProgressBar.setValue(a); pa.parse

ANTLR4 left-recursive error

天大地大妈咪最大 提交于 2019-12-13 03:09:13
问题 My ANTLR4 grammar in file power.g4 is this: assign : id '=' expr ; id : 'A' | 'B' | 'C' ; expr : expr '+' term | expr '-' term | term ; term : term '*' factor | term '/' factor | factor ; factor : expr '**' factor | '(' expr ')' | id ; WS : [ \t\r\n]+ -> skip ; When I run command antlr4 power.g4 This error occurred: error(119): power.g4::: The following sets of rules are mutually left-recursive [expr, factor, term] What can I do? 回答1: To avoid the left recursion error, put all forms of an

How to exclude more than one character in rule?

狂风中的少年 提交于 2019-12-13 02:58:25
问题 I'm trying to write a string matching rule in ANTLRWorks, and I need to match either escaped quotes or any non quote character. I can match escaped quotes but I'm having trouble with the other part: ~'\'' | ~'\"' will end up matching everything and ~'\'\"' seems to be ignored by the grammar generator (at least the visual display). What sequence of characters will get me what I want? 回答1: Try something like this: StringLiteral : '"' (EscapeSequence | StringChar)* '"' ; EscapeSequence : '\\' ('

ANTLR from Java to C#

无人久伴 提交于 2019-12-13 02:13:43
问题 I'm planning to create something that would do automated translation from Java to C# (and in reverse afterwards). What I need is something that you could use to translate Java source code into C# source code. I ran across ANTLR, but I'm not exactly sure of how go about using it for my task. I know ANTLR has a strong support for both Java and C#, and there are already existing grammars for both of them, the lexing/parsing process, then the AST creation, and then finally the tree walker.