antlr

Get original text of an Antlr rule

白昼怎懂夜的黑 提交于 2019-11-29 04:36:25
I am an ANTLR beginner and want to calculate a SHA1-Hash of symbols. My simplified example grammar: grammar Example; method @after{calculateSha1($text); }: 'call' ID; ID: 'A'..'Z'+; WS: (' '|'\n'|'\r')+ {skip(); } COMMENT: '/*' (options {greedy=false;}: .)* '*/' {$channel=HIDDEN} As the lexer removes all whitespaces the different strings callABC , call /* DEF */ ABC unfortunately get the same SHA1-Hash value. Is it possible to get the "original" text of a rule between the start- and end-token with all the skipped whitespaces and the text of the other channels? (One possibility that comes into

Parsing string interpolation in ANTLR

别来无恙 提交于 2019-11-29 03:59:53
问题 I'm working on a simple string manipulation DSL for internal purposes, and I would like the language to support string interpolation as it is used in Ruby. For example: name = "Bob" msg = "Hello ${name}!" print(msg) # prints "Hello Bob!" I'm attempting to implement my parser in ANTLRv3, but I'm pretty inexperienced with using ANTLR so I'm unsure how to implement this feature. So far, I've specified my string literals in the lexer, but in this case I'll obviously need to handle the

Nested Boolean Expression Parser using ANTLR

牧云@^-^@ 提交于 2019-11-29 02:43:47
问题 I'm trying to parse a Nested Boolean Expression and get the individual conditions within the expression separately. For e.g., if the input string is: (A = a OR B = b OR C = c AND ((D = d AND E = e) OR (F = f AND G = g))) I would like to get the conditions with the correct order. i.e., D =d AND E = e OR F = f AND G = g AND A = a OR B = b OR C = c I'm using ANTLR 4 to parse the input text and here's my grammar: grammar SimpleBoolean; rule_set : nestedCondition* EOF; AND : 'AND' ; OR : 'OR' ;

sharding-jdbc之ANTLR4 SQL解析

时光总嘲笑我的痴心妄想 提交于 2019-11-29 02:12:02
Sharding主要利用 ANTLR4 来解析SQL,以mysql为例,分析源码前可以先了解以下三点: antlr4,如何编写 .g4 语法文件 mysql 语法可以参考 https://dev.mysql.com/doc/refman/8.0/en/sql-syntax-data-manipulation.html mysql g4文件编写可以参考 https://github.com/antlr/grammars-v4/blob/master/mysql 源码分析 1.解析入口ParsingSQLRouter#parse /** * 解析sql * * @param logicSQL 逻辑sql * @param useCache 是否缓存解析后的结果 * @return */ @Override public SQLStatement parse(final String logicSQL, final boolean useCache) { //解析前钩子,如:调用链etx parsingHook.start(logicSQL); try { //解析SQL SQLStatement result = new ShardingSQLParseEntry(databaseType, shardingMetaData.getTable(), parsingResultCache

ANTLR Parser with manual lexer

江枫思渺然 提交于 2019-11-29 01:02:49
问题 I'm migrating a C#-based programming language compiler from a manual lexer/parser to Antlr. Antlr has been giving me severe headaches because it usually mostly works, but then there are the small parts that do not and are incredibly painful to solve. I discovered that most of my headaches are caused by the lexer parts of Antlr, rather than the parser. Then I noticed parser grammar X; and realized that perhaps I could have my manually written lexer and then an Antlr generated parser. So I'm

ANTLR 4 $channel = HIDDEN and options

烈酒焚心 提交于 2019-11-28 23:45:31
问题 I need help with my ANTLR 4 grammar after deciding to switch to v4 from v3. I am not very experienced with ANTLR so I am really sorry if my question is dumb ;) In v3 I used the following code to detect Java-style comments: COMMENT : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;} | '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;} ; In v4 there are no rule-specific options. The actions (move to hidden channel) are also invalid. Could somebody please give me a hint how to do it in

SS-学习antlr4

醉酒当歌 提交于 2019-11-28 21:47:47
首先阅读一下 ANTLR4(ANother Tool for Language Recognition)官网 ,ANTLR 4 是一款强大的语法分析器生成工具,可用于读取、处理、执行和翻译结构化的文本或二进制文件,比如mysql等一些查用的语法可在 https://github.com/antlr/grammars-v4 查看 ANTLR4分词法分析器与语法分析器 词法分析器将输入数据解析为词(Token) 语法解析器将词解析为抽象语法树(AST) 环境搭建 #OS X $ cd /usr/local/lib $ sudo curl -O https://www.antlr.org/download/antlr-4.7.2-complete.jar #编辑~/.bash_profile文件添加环境变量 $ vim ~/.bash_profile export CLASSPATH=".:/usr/local/lib/antlr-4.7.2-complete.jar:$CLASSPATH" alias antlr4='java -jar /usr/local/lib/antlr-4.7.2-complete.jar' alias grun='java org.antlr.v4.gui.TestRig' $ source ~/.bash_profile #测试antlr是否配置成功 $

Good parser generator (think lex/yacc or antlr) for .NET? Build time only? [closed]

本秂侑毒 提交于 2019-11-28 19:22:58
Is there a good parser generator (think lex/yacc or antlr) for .NET? Any that have a license that would not scare lawyers? Lot’s of LGPL but I am working on embedded components and some organizations are not comfortable with me taking an LGPL dependency. I've heard that Oslo may provide this functionality but I'm not sure if it's a build time dependency or also a runtime dependency. Can anyone clarify what Oslo will provide? UPDATE What I would really like is a parser generator that is a build time only dependency. It looks like ANTLR has a runtime component. I just discovered that F# ships

android ANTLR make not working properly

痴心易碎 提交于 2019-11-28 18:29:09
I am trying to use ANTLR on Android, and I found this: ANTLR and Android After downloading the AntlrJavaRuntime I am not sure of what to do, I am supposed to make this: 1. lunch the appropriate target 2. make AntlrJavaRuntime 3. verify that AntlrJavaRuntime.xml was placed in /system/etc/permissions and 4. AntlrJavaRuntime.jar was placed in /system/framework 5. after this, you can run a normal make First of all, what does step 1 even means? Secondly, when I try doing: make AntlrJavaRuntime I get the following error: ~/AntlrJavaRuntime$ make AntlrJavaRuntime make: Nothing to be done for

Is there a simple example of using antlr4 to create an AST from java source code and extract methods, variables and comments? [closed]

一曲冷凌霜 提交于 2019-11-28 17:10:49
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 7 months ago . Can someone provide a detailed example as to how I can do this using antlr4? Instructions right from installing antlr4 and its dependencies would be highly appreciated. 回答1: Here it is. First, you're gonna buy the ANTLR4 book ;-) Second, you'll download antlr4 jar and the java