antlr4

Is it feasible to use Antlr for source code completion?

淺唱寂寞╮ 提交于 2019-11-29 14:59:58
问题 I don't know, if this question is valid since i'm not very familiar with source code parsing. My goal is to write a source code completion function for one existing programming language (Language "X") for learning purposes. Is Antlr(v4) suitable for such a task or should the necessary AST/Parse Tree creation and parsing be done by hand, assuming no existing solutions exists? I haven't found much information about that specific topic, except a list of compiler books, except a compiler is not

ANTLR4 lexer rules don't work as expected

自闭症网瘾萝莉.ら 提交于 2019-11-29 12:52:05
I want to write a lexer rule about the month and the year, the rule is(with regular expression): "hello"[0-9]{1,2}"ever"([0-9]{2}([0-9]{2})?)? the "hello" and "ever" literals are just for debuging. that's say, one or two digits for month, and two or four digits for year. And what's more, the year part could be bypass. such as: Aug 2015 ->hello08ever2015 or hello8ever2015 or hello8ever15 or hello8ever or hello08ever; Oct 2015 -> hello10ever2015 or hello10ever15 or hello10ever; and my lexer rules are as follow(ANTLR4): grammar Hello; r : 'hello' TimeDate 'ever' TimeYear? ; TimeDate : Digit Digit

Antlr4 unexpectedly stops parsing expression

时光总嘲笑我的痴心妄想 提交于 2019-11-29 12:13:14
I'm developing a simple calculator with the formula grammar: grammar Formula ; expr : <assoc=right> expr POW expr # pow | MINUS expr # unaryMinus | PLUS expr # unaryPlus | expr PERCENT # percent | expr op=(MULTIPLICATION|DIVISION) expr # multiplyDivide | expr op=(PLUS|MINUS) expr # addSubtract | ABS '(' expr ')' # abs | '|' expr '|' # absParenthesis | MAX '(' expr ( ',' expr )* ')' # max | MIN '(' expr ( ',' expr )* ')' # min | '(' expr ')' # parenthesis | NUMBER # number | '"' COLUMN '"' # column ; MULTIPLICATION: '*' ; DIVISION: '/' ; PLUS: '+' ; MINUS: '-' ; PERCENT: '%' ; POW: '^' ; ABS:

ANTLR4 negative lookahead in lexer

梦想的初衷 提交于 2019-11-29 12:07:25
I am trying to define lexer rules for PostgreSQL SQL. The problem is with the operator definition and the line comments conflicting with each other. for example @--- is an operator token @- followed by the -- comment and not an operator token @--- In grako it would be possible to define the negative lookahead for the - fragment like: OP_MINUS: '-' ! ( '-' ) . In ANTLR4 I could not find any way to rollback already consumed fragment. Any ideas? Here the original definition what the PostgreSQL operator can be: The operator name is a sequence of up to NAMEDATALEN-1 (63 by default) characters from

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

点点圈 提交于 2019-11-29 12:05:08
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? You will need to create your own implementation of IAntlrErrorStrategy for this, and then set the Parser.ErrorHandler property to an instance of your error strategy. The documentation for the

How Get error messages of antlr parsing?

假如想象 提交于 2019-11-29 11:12:11
I wrote a grammar with antlr 4.4 like this : grammar CSV; file : row+ EOF ; row : value (Comma value)* (LineBreak | EOF) ; value : SimpleValueA | QuotedValue ; Comma : ',' ; LineBreak : '\r'? '\n' | '\r' ; SimpleValue : ~(',' | '\r' | '\n' | '"')+ ; QuotedValue : '"' ('""' | ~'"')* '"' ; then I use antlr 4.4 for generating parser & lexer, this process is successful after generate classes I wrote some java code for using grammar import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CommonTokenStream; public class Main { public static void main(String[] args) { String source

How to match any symbol in ANTLR parser (not lexer)?

孤者浪人 提交于 2019-11-29 10:37:54
How to match any symbol in ANTLR parser (not lexer)? Where is the complete language description for ANTLR4 parsers? UPDATE Is the answer is "impossible"? You first need to understand the roles of each part in parsing: The lexer: this is the object that tokenizes your input string. Tokenizing means to convert a stream of input characters to an abstract token symbol (usually just a number). The parser: this is the object that only works with tokens to determine the structure of a language. A language (written as one or more grammar files) defines the token combinations that are valid. As you can

Antlr : beginner 's mismatched input expecting ID

 ̄綄美尐妖づ 提交于 2019-11-29 08:52:17
As a beginner, when I was learning ANTLR4 from the The Definitive ANTLR 4 Reference book, I tried to run my modified version of the exercise from Chapter 7: /** * to parse properties file * this example demonstrates using embedded actions in code */ grammar PropFile; @header { import java.util.Properties; } @members { Properties props = new Properties(); } file : { System.out.println("Loading file..."); } prop+ { System.out.println("finished:\n"+props); } ; prop : ID '=' STRING NEWLINE { props.setProperty($ID.getText(),$STRING.getText());//add one property } ; ID : [a-zA-Z]+ ; STRING :(~[\r\n]

How do I get help on the antlr4-maven-plugin

人走茶凉 提交于 2019-11-29 08:44:35
问题 The antlr4-maven-plugin does not appear to be document on the Antlr4 website. 回答1: That might get you nothing, like it did me. Try this: mvn org.antlr:antlr4-maven-plugin:help -Ddetail=true Produces: [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- antlr4-maven-plugin:4.0:help

build AST in antlr4

人盡茶涼 提交于 2019-11-29 07:51:32
I was wondering whether we could build an AST using Antlr version 4. I couldn't find any reference on building it using antlr4. One SO answer says that it would be easy to use antlr4 which produces only parse tree but my question is what about the efficiency ? It forces us to crawl whole parse tree instead of an abstract syntax tree which is not efficient way to walk through the whole tree and perform task using visitors. There are two key items I'd like to point out first: Efficiency covers more than simple speed of execution. It can also apply to memory overhead, maintainability, and the