我正在尝试构建以下存储库中可用的 Antlr V4 语法: https: //github.com/antlr/grammars-v4。我已经成功构建了 Python3 和 Java 语法,并且目标 *.java 文件已成功生成。但是,当我尝试构建 C# 语法(例如 CSharpLexer.g4、CSharpParser.g4 和 CsharpPreporcessorParser.g4)时,我收到构建错误。它会生成以下错误:
error(114): CSharpPreprocessorParser.g4:7:21: cannot find tokens file ./CSharpLexer.tokens
error(126): CSharpParser.g4:20:62: cannot create implicit token for string literal in non-combined grammar: '.'
error(126): CSharpParser.g4:25:14: cannot create implicit token for string literal in non-combined grammar: '?'
error(126): CSharpParser.g4:25:37: cannot create implicit token for string literal in non-combined grammar: '*'
error(126): CSharpParser.g4:31:8: cannot create implicit token for string literal in non-combined grammar: '*'
error(126): CSharpParser.g4:71:3: cannot …
Run Code Online (Sandbox Code Playgroud) 我看到很多例子,其中一些将 yytext 传递给 yylval,而另一些则没有。这是 lex 和 yacc 中简单加法器的代码
/* add.l */
digit [0-9]
%%
{digit}+ {sscanf(yytext, "%d", &yylval);
return(INT);
}
\+ return(PLUS);
\n return(NL);
. ;
%%
int yywrap() { return 1; }
Run Code Online (Sandbox Code Playgroud)
和
/* add.y */
/* L = {INT PLUS INT NL} */
%token INT PLUS NL
%%
add: INT PLUS INT NL { printf("%d\n", $1 + $3);}
%%
#include "lex.yy.c"
yyerror(char *s) { printf("%s\n", s); }
main() {
return yyparse();
}
Run Code Online (Sandbox Code Playgroud)
我没有看到像 printf(yylval) 等代码。为什么代码sscanf(yytext, "%d", &yylval) …
我目前正在使用python 的 lark 解析器来尝试读取一些问题规范。我对扩展巴科斯-诺尔形式的“正确”语法感到困惑,尤其是关于如何分离 LHS 和 RHS。维基百科页面使用等号=
,lark 只需要一个冒号;请参阅云雀备忘单。其他来源使用::=
分隔符,例如atom ebnf 包。
有确定的答案吗?官方ISO 规范似乎建议“定义符号”应该是,=
但规范中似乎有回旋余地。那么为什么会有不同的版本呢?
我\xc2\xb4m 无法确定这个语法\xc2\xb4s 是否有歧义。如何检查是否有歧义?
\nG = ({S,A,B}, {0,1}, P, S}
\n电话:
\n\xe2\x86\x92 0B | S \xe2\x86\x92 1A
\n一个 \xe2\x86\x92 0 | 0S | 1AA
\nB \xe2\x86\x92 1 | 1S | 0BB
\n我有一份包含结构化条目的期刊参考书目的 OCR 文本。我想使用不可见的 XML标准来提取和解析条目。
\n输入示例:
\n\n1 2 Hype. 1990?- 1993. Frequency: Bimonthly. River Edge, \n\nNJ. Published by Word Up! Video, Inc. Last issue 66 pages. \nHeight 28 cm. Line drawings; Photographs (some in color); \nCommercial advertising; Table of contents. Previous editor(s): \nMarica A. Cole. ISSN 1056-4632. LC card no. sn91-1965. \nOCLC no. 23715422. Subject focus and/or Features: Hip hop \nculture, Music, Rap music. \n\nWHi v.l, n.6; v.2, n.5 Pam 01-5450 Aug, 1992; Aug, 1993 …
Run Code Online (Sandbox Code Playgroud) 我想知道我为这个语法做的FIRST和FOLLOW设置是否正确
S -> TS'
S' -> +TS' | -TS' | epsilon
T -> UT'
T' -> *UT' | /UT' | epsilon
U -> VX
X -> ^U | epsilon
V -> (W) | -W | W | epsilon
W -> S | number
Run Code Online (Sandbox Code Playgroud)
FIRST(S) = FIRST(T) = FIRST(U) = FIRST(V) = FIRST(W) = { ( , - , + , number , epsilon }
FIRST(T') = { *, / , epsilon}
FIRST(S') = { + , - , epsilon}
FIRST(X) …
Run Code Online (Sandbox Code Playgroud) 我正在为ANSI C++的元素寻找一些预定义的正则表达式.
我想创建的程序,这需要headerfile(具有包括命名空间,类等)作为输入,并与发现的类名列表返回,方法,属性等
它很难谷歌类似的东西,我总是最后的如何在C使用的正则表达式++教程.也许我只是在谷歌搜索错误的条款?也许有人已经找到/使用/创建了这样的正则表达式.
我正在使用ANTLR 4:
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.TokenStream;
public class Builder
{
public static void main(String[] args)
{
CharStream input = new ANTLRInputStream("ON M1==2 && M3 == 5 && (M2 > 1 || M5 <= 5.0) "
+ "DO P5:42 P4:10");
ExprLexer lexer = new ExprLexer(input);
TokenStream tokens = new CommonTokenStream(lexer);
ExprParser parser = new ExprParser(tokens);
ExprParser.ExpressionContext uu = parser.expression();
for (int i = 0; i < uu.getChildCount(); ++i)
System.out.println(uu.getChild(i));
}
}
Run Code Online (Sandbox Code Playgroud)
以下语法:
grammar Expr;
options
{
// …
Run Code Online (Sandbox Code Playgroud) 我正在使用ANTLR4来解析一个简单的脚本语言.
此语言对FOR
循环使用以下语法:
FOR [I] = 1 to [N]
instructions
NEXT [I]
Run Code Online (Sandbox Code Playgroud)
为了正确,FOR
循环必须在FOR
关键字之后和NEXT
关键字之后具有完全相同的标记.
例如,这是正确的:
FOR I = 1 TO 10
NEXT I
Run Code Online (Sandbox Code Playgroud)
虽然这是不正确的:
FOR I = 1 TO 10
NEXT J
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有一个看起来像这样的规则:
forloop
: FOR VARNAME EQUAL INT TO INT instructions NEXT VARNAME
;
Run Code Online (Sandbox Code Playgroud)
使用以下相关词法规则(我删除了常量关键字FOR : 'FOR';
):
fragment ALPHA : [a-zA-Z_];
fragment ALPHANUM : [a-zA-Z_0-9];
fragment DIGIT : [0-9];
VARNAME : ALPHA ALPHANUM*;
INT : DIGIT+;
Run Code Online (Sandbox Code Playgroud)
但是,此规则将解释为第二个实际上不正确的示例.
如何告诉ANTLR4第二个VARNAME
必须与规则中的第一个相同?