我正在尝试使用和理解 AntLR,这对我来说是新的。我的目的是读取用 C 编写的源代码文件并从中提取标识符(变量和函数名称)。
在我的 C 语法(文件C.g4)中考虑:
identifierList
: Identifier
| identifierList Comma Identifier
;
Identifier
: IdentifierNondigit
( IdentifierNondigit
| Digit
)*
;
Run Code Online (Sandbox Code Playgroud)
生成解析器和侦听器后,我创建了自己的标识符列表侦听器。
请注意,MyCListener 类扩展了 CBaseListener:
public class MyCListener extends CBaseListener {
@Override
public void enterIdentifierList(CParser.IdentifierListContext ctx) {
List<ParseTree> children = ctx.children;
for (ParseTree parseTree : children) {
System.out.println(parseTree.getText());
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在主课上有这个:
String fileurl = "C:/example.c";
CLexer lexer;
try {
lexer = new CLexer(new ANTLRFileStream(fileurl));
CommonTokenStream tokens = new CommonTokenStream(lexer);
CParser parser = new CParser(tokens);
CParser.IdentifierListContext …Run Code Online (Sandbox Code Playgroud) 我使用这种方法在Eclipse中从Git导入了一个项目:File> Import> Git> Git中的Projects> Next> Clone URL.
此时我插入了像这样的https://github.com/mygituser/My-Project的URI .然后我的用户和密码,以及项目已导入.
问题是Eclipse没有将它识别为Java项目,我真的不知道为什么.
因此,我无法在我的项目中看到错误或警告,也没有使用自动完成,我收到此消息"此编译单元不在Java项目的构建路径上".
此外,右键单击My-Project> Build path显示消息"No actions avaliable".
如果您需要更多信息来帮助我,请问您评论,请?感谢您的关注!
在Java中,在一些标点符号之后修复丢失的空格的最佳方法是:
, . ; : ? !
Run Code Online (Sandbox Code Playgroud)
例如:
String example = "This is!just an:example,of a string,that needs?to be fixed.by inserting:a whitespace;after punctuation marks.";
Run Code Online (Sandbox Code Playgroud)
输出应该是:
"This is! just an: example, of a string, that needs? to be fixed. by inserting: a whitespace; after punctuation marks."
Run Code Online (Sandbox Code Playgroud)
很明显,这不起作用:
example = example.replaceAll("[,.!?;:]", " ");
Run Code Online (Sandbox Code Playgroud)
所以我正在寻找一个等待你帮助的解决方案.谢谢!!
我正在使用Java和MySQL,我必须从DB中选择这样的东西:
String sql = "SELECT type FROM test_case where input=" + inputTestCase;
Run Code Online (Sandbox Code Playgroud)
问题是inputTestCase字符串有时会有换行符.在这些情况下,我得到一个例外,例如:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version
for the right syntax to use near '5
8
10' at line 2
Run Code Online (Sandbox Code Playgroud)
哪里:
'5
8
10'
Run Code Online (Sandbox Code Playgroud)
是我的inputTestCase.
我该怎么做才能让它Select发挥作用.谢谢大家的帮助!
PS:我没有初始化inputTestCase.该字符串的值来自DB.