- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 ANTLR 的新手,我正在尝试使用 C 目标在 ANTLR3 中创建一个规则评估器。我正在使用最新版本的 ANTLR3 (3.5.2) 和 Java 1.7.0.80 来生成代码。
我可以毫无问题地生成 Lexer 和 Parser,但是在尝试生成 TreeParser 时出现以下错误:
: error 24 : template error: ASTTreeParser.stg 321:25: anonymous template has 0 arg(s) but mapped across 1 value(s)
: error 24 : template error: ASTTreeParser.stg 323:25: anonymous template has 0 arg(s) but mapped across 1 value(s)
但是,当我将目标语言更改为 Java 时,没有发生任何错误并且代码(Java)已正确生成。
我的 Lexer/Parser 语法是:
grammar RuleParser;
options
{
language = C;
output = AST;
}
tokens
{
MB_TRUE = 'true';
MB_FALSE = 'false';
MB_AND = 'AND';
MB_OR = 'OR';
MB_LT = '<';
MB_GT = '>';
MB_LTEQ = '<=';
MB_GTEQ = '>=';
MB_EQ = '==';
MB_NEQ = '!=';
MB_LBRACK = '[';
MB_RBRACK = ']';
MB_LPAREN = '(';
MB_RPAREN = ')';
MB_LCURLY = '{';
MB_RCURLY = '}';
MB_COLON = ':';
MB_SEMICOLON = ';';
MB_COMA = ',';
MB_OPTION = '|';
MB_DOT = '.';
MB_IN = 'IN';
MB_STRING_SUFIX = '@';
MB_CASE_SENSITIVE = 'CS';
MB_CASE_INSENSITIVE = 'CI';
MB_ENCODE_UNICODE = 'EU';
MB_ENCODE_ANSI = 'EA';
MB_RULEDI = 'rule_di';
MB_HEADER = 'header';
MB_CONDITIONS = 'conditions';
CONDITION = 'condition';
RULE = 'rule';
RULES = 'rules';
MB_METADATA = 'metadata';
}
version
: INT MB_DOT! INT MB_DOT! INT
;
string
: STRING^
| STRING^ MB_STRING_SUFIX! MB_LBRACK! ((a=MB_CASE_INSENSITIVE | b=MB_CASE_SENSITIVE)? MB_OPTION!? (c=MB_ENCODE_UNICODE | d=MB_ENCODE_ANSI)?) MB_RBRACK!
;
// Rules definitions
rules
: rule+ -> ^(RULES rule+)
;
rule
: MB_RULEDI MB_LCURLY header conditions MB_RCURLY -> ^(RULE header conditions)
;
header
: MB_HEADER MB_LCURLY header_block+ MB_RCURLY -> ^(MB_HEADER header_block+)
;
header_block
: 'Id' MB_COLON INT MB_SEMICOLON -> ^(MB_METADATA 'Id' INT)
| 'Version' MB_COLON version MB_SEMICOLON-> ^(MB_METADATA 'Version' version)
| 'Author' MB_COLON STRING MB_SEMICOLON-> ^(MB_METADATA 'Author' STRING)
| 'Date' MB_COLON DATE MB_SEMICOLON-> ^(MB_METADATA 'Date' DATE)
| 'Brief' MB_COLON STRING MB_SEMICOLON-> ^(MB_METADATA 'Brief' STRING)
;
conditions
: MB_CONDITIONS '{' conditions_block '}' -> ^(MB_CONDITIONS conditions_block)
;
conditions_block
: statement+ -> ^(CONDITION statement)+
;
statement
: expression MB_SEMICOLON -> expression
| string MB_IN IDENTIFIER MB_SEMICOLON -> ^(MB_IN string IDENTIFIER)
;
expression
: andexpression;
andexpression : orexpression (MB_AND^ orexpression)*;
orexpression : term (MB_OR^ term)*;
term: atom (( MB_LT | MB_GT | MB_LTEQ | MB_GTEQ | MB_EQ | MB_NEQ)^ atom)? | MB_LPAREN! expression MB_RPAREN!;
atom : IDENTIFIER | INT | string | MB_TRUE | MB_FALSE;
// Lexer rules
//-------------------------------------------------
DATE
: DATEDAY SEPARATOR (MONTH | DATEMONTH) SEPARATOR DATEYEAR
;
MONTH
: JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC
;
fragment JAN : ('J'|'j')('A'|'a')('N'|'n') ;
fragment FEB : ('F'|'f')('E'|'e')('B'|'b') ;
fragment MAR : ('M'|'m')('A'|'a')('R'|'r') ;
fragment APR : ('A'|'a')('P'|'p')('R'|'r') ;
fragment MAY : ('M'|'m')('A'|'a')('Y'|'y') ;
fragment JUN : ('J'|'j')('U'|'u')('N'|'n') ;
fragment JUL : ('J'|'j')('U'|'u')('L'|'l') ;
fragment AUG : ('A'|'a')('U'|'u')('G'|'g') ;
fragment SEP : ('S'|'s')('E'|'e')('P'|'p') ;
fragment OCT : ('O'|'o')('C'|'c')('T'|'t') ;
fragment NOV : ('N'|'n')('O'|'o')('V'|'v') ;
fragment DEC : ('D'|'d')('E'|'e')('C'|'c') ;
SEPARATOR
: ('/'|'\\'|'-')
;
fragment
DATEMONTH
: ('0'..'1')?DIGIT
;
fragment
DATEDAY
: ('0'..'3')?DIGIT
;
fragment
DATEYEAR
: '2' DIGIT DIGIT DIGIT
;
fragment
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
IDENTIFIER: ID (MB_DOT ID)?;
COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
| '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
;
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
STRING
: '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
;
CHAR: '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
;
fragment
HEX_DIGIT
: ('0'..'9'|'a'..'f'|'A'..'F')
| '\t'
| '\r'
| '\n'
;
fragment
ESC_SEQ
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
| UNICODE_ESC
| OCTAL_ESC
;
fragment
OCTAL_ESC
: '\\' ('0'..'3') ('0'..'7') ('0'..'7')
| '\\' ('0'..'7') ('0'..'7')
| '\\' ('0'..'7')
;
fragment
UNICODE_ESC
: '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
;
INT : DIGIT+;
fragment DIGIT : ('0'..'9');
我的树解析器语法:
tree grammar RuleParserTree;
options
{
language = C;
tokenVocab = RuleParser;
output = AST;
ASTLabelType = pANTLR3_BASE_TREE;
//rewrite=true;
}
version
: INT INT INT
;
string
: STRING
| ^(STRING (a=MB_CASE_INSENSITIVE | b=MB_CASE_SENSITIVE)? (c=MB_ENCODE_UNICODE | d=MB_ENCODE_ANSI)?)
;
// Rules definitions
rules
: ^(RULES rule+)
;
rule
: ^(RULE header conditions)
;
header
: ^(MB_HEADER header_block+)
;
header_block
: ^(MB_METADATA 'Id' INT)
| ^(MB_METADATA 'Version' version)
| ^(MB_METADATA 'Author' STRING)
| ^(MB_METADATA 'Date' DATE)
| ^(MB_METADATA 'Brief' STRING)
;
conditions
: ^(MB_CONDITIONS conditions_block)
;
conditions_block
: CONDITION statement+
;
statement
: expression
| ^(MB_IN string IDENTIFIER)
;
expression
: ^(MB_AND expression expression)
| ^(MB_OR expression expression)
| ^(MB_LT expression expression)
| ^(MB_GT expression expression)
| ^(MB_LTEQ expression expression)
| ^(MB_GTEQ expression expression)
| ^(MB_EQ expression expression)
| ^(MB_NEQ expression expression)
| IDENTIFIER
| INT
| STRING
| MB_TRUE
| MB_FALSE
;
完整的日志转储:
ANTLR Parser Generator Version 3.5.2
--------------------
attempting LL(1) DFA (d=1) for 22:13: (a= MB_CASE_INSENSITIVE |b= MB_CASE_SENSITIVE )?
> LOOK(33)
< LOOK(33)=MB_CASE_INSENSITIVE
> LOOK(35)
< LOOK(35)=MB_CASE_SENSITIVE
> LOOK(37)
< LOOK(37)={UP, MB_ENCODE_ANSI..MB_ENCODE_UNICODE}
decision 1 is simple LL(1)
--------------------
attempting LL(1) DFA (d=2) for 22:60: (c= MB_ENCODE_UNICODE |d= MB_ENCODE_ANSI )?
> LOOK(41)
< LOOK(41)=MB_ENCODE_UNICODE
> LOOK(43)
< LOOK(43)=MB_ENCODE_ANSI
> LOOK(45)
< LOOK(45)=UP
decision 2 is simple LL(1)
--------------------
attempting LL(1) DFA (d=3) for 20:1: string : ( STRING | ^( STRING (a= MB_CASE_INSENSITIVE |b= MB_CASE_SENSITIVE )? (c= MB_ENCODE_UNICODE |d= MB_ENCODE_ANSI )? ) );
> LOOK(27)
< LOOK(27)=STRING
> LOOK(29)
< LOOK(29)=STRING
decision 3 not suitable for LL(1)-optimized DFA analysis
--------------------
building lookahead DFA (d=3) for 20:1: string : ( STRING | ^( STRING (a= MB_CASE_INSENSITIVE |b= MB_CASE_SENSITIVE )? (c= MB_ENCODE_UNICODE |d= MB_ENCODE_ANSI )? ) );
closure at string state 27|1 filling DFA state 0 with context [$]
closure at string state 29|2 filling DFA state 0 with context [$]
convert DFA state 0 (2 nfa states)
DFA state after reach 65 0:{27|1, 29|2}-STRING->1:{28|1, 30|2}
closure(1:{28|1, 30|2})
closure at string state 28|1 filling DFA state 1 with context [$]
closure at string state 51|1 filling DFA state 1 with context [$]
closure at string state 3|1 filling DFA state 1 with context [$]
closure at statement state 169|1 filling DFA state 1 with context [$]
resolveNonDeterminisms 1:{28|1, 30|2, 51|1, 3|1, 169|1}
convert DFA state 1 (5 nfa states)
DFA state after reach 2 1:{28|1, 30|2, 51|1, 3|1, 169|1}-DOWN->2:{32|2}
resolveNonDeterminisms 2:{32|2}
DFA state after reach 19 1:{28|1, 30|2, 51|1, 3|1, 169|1}-IDENTIFIER->3:{171|1}
resolveNonDeterminisms 3:{171|1}
cost: 4 states, 19 ms
--------------------
attempting LL(1) DFA (d=4) for ()+ loopback of 29:12: ( rule )+
> LOOK(58)
< LOOK(58)=RULE
> LOOK(62)
< LOOK(62)=UP
decision 4 is simple LL(1)
--------------------
attempting LL(1) DFA (d=5) for ()+ loopback of 40:16: ( header_block )+
> LOOK(81)
< LOOK(81)=MB_METADATA
> LOOK(85)
< LOOK(85)=UP
decision 5 is simple LL(1)
--------------------
attempting LL(1) DFA (d=6) for 44:1: header_block : ( ^( MB_METADATA 'Id' INT ) | ^( MB_METADATA 'Version' version ) | ^( MB_METADATA 'Author' STRING ) | ^( MB_METADATA 'Date' DATE ) | ^( MB_METADATA 'Brief' STRING ) );
> LOOK(89)
< LOOK(89)=MB_METADATA
> LOOK(99)
< LOOK(99)=MB_METADATA
> LOOK(109)
< LOOK(109)=MB_METADATA
> LOOK(119)
< LOOK(119)=MB_METADATA
> LOOK(129)
< LOOK(129)=MB_METADATA
decision 6 not suitable for LL(1)-optimized DFA analysis
--------------------
building lookahead DFA (d=6) for 44:1: header_block : ( ^( MB_METADATA 'Id' INT ) | ^( MB_METADATA 'Version' version ) | ^( MB_METADATA 'Author' STRING ) | ^( MB_METADATA 'Date' DATE ) | ^( MB_METADATA 'Brief' STRING ) );
closure at header_block state 89|1 filling DFA state 0 with context [$]
closure at header_block state 99|2 filling DFA state 0 with context [$]
closure at header_block state 109|3 filling DFA state 0 with context [$]
closure at header_block state 119|4 filling DFA state 0 with context [$]
closure at header_block state 129|5 filling DFA state 0 with context [$]
convert DFA state 0 (5 nfa states)
DFA state after reach 46 0:{89|1, 99|2, 109|3, 119|4, 129|5}-MB_METADATA->1:{90|1, 100|2, 110|3, 120|4, 130|5}
closure(1:{90|1, 100|2, 110|3, 120|4, 130|5})
resolveNonDeterminisms 1:{90|1, 100|2, 110|3, 120|4, 130|5}
convert DFA state 1 (5 nfa states)
DFA state after reach 2 1:{90|1, 100|2, 110|3, 120|4, 130|5}-DOWN->2:{92|1, 102|2, 112|3, 122|4, 132|5}
closure(2:{92|1, 102|2, 112|3, 122|4, 132|5})
resolveNonDeterminisms 2:{92|1, 102|2, 112|3, 122|4, 132|5}
convert DFA state 2 (5 nfa states)
DFA state after reach 71 2:{92|1, 102|2, 112|3, 122|4, 132|5}-'Id'->3:{94|1}
resolveNonDeterminisms 3:{94|1}
DFA state after reach 72 2:{92|1, 102|2, 112|3, 122|4, 132|5}-'Version'->4:{104|2}
resolveNonDeterminisms 4:{104|2}
DFA state after reach 68 2:{92|1, 102|2, 112|3, 122|4, 132|5}-'Author'->5:{114|3}
resolveNonDeterminisms 5:{114|3}
DFA state after reach 70 2:{92|1, 102|2, 112|3, 122|4, 132|5}-'Date'->6:{124|4}
resolveNonDeterminisms 6:{124|4}
DFA state after reach 69 2:{92|1, 102|2, 112|3, 122|4, 132|5}-'Brief'->7:{134|5}
resolveNonDeterminisms 7:{134|5}
cost: 8 states, 27 ms
--------------------
attempting LL(1) DFA (d=7) for ()+ loopback of 60:14: ( statement )+
> LOOK(156)
< LOOK(156)={IDENTIFIER..INT, MB_AND, MB_EQ..MB_GTEQ, MB_IN, MB_LT..MB_LTEQ, MB_NEQ, MB_OR, MB_TRUE, STRING}
> LOOK(160)
< LOOK(160)=UP
decision 7 is simple LL(1)
--------------------
attempting LL(1) DFA (d=8) for 64:1: statement : ( expression | ^( MB_IN string IDENTIFIER ) );
> LOOK(162)
< LOOK(162)={IDENTIFIER..INT, MB_AND, MB_EQ..MB_GTEQ, MB_LT..MB_LTEQ, MB_NEQ, MB_OR, MB_TRUE, STRING}
> LOOK(164)
< LOOK(164)=MB_IN
decision 8 is simple LL(1)
--------------------
attempting LL(1) DFA (d=9) for 70:1: expression : ( ^( MB_AND a= expression b= expression ) | ^( MB_OR a= expression b= expression ) | ^( MB_LT a= expression b= expression ) | ^( MB_GT a= expression b= expression ) | ^( MB_LTEQ a= expression b= expression ) | ^( MB_GTEQ a= expression b= expression ) | ^( MB_EQ a= expression b= expression ) | ^( MB_NEQ a= expression b= expression ) | IDENTIFIER | INT | STRING | MB_TRUE | MB_FALSE );
> LOOK(177)
< LOOK(177)=MB_AND
> LOOK(189)
< LOOK(189)=MB_OR
> LOOK(201)
< LOOK(201)=MB_LT
> LOOK(213)
< LOOK(213)=MB_GT
> LOOK(225)
< LOOK(225)=MB_LTEQ
> LOOK(237)
< LOOK(237)=MB_GTEQ
> LOOK(249)
< LOOK(249)=MB_EQ
> LOOK(261)
< LOOK(261)=MB_NEQ
> LOOK(273)
< LOOK(273)=IDENTIFIER
> LOOK(275)
< LOOK(275)=INT
> LOOK(277)
< LOOK(277)=STRING
> LOOK(279)
< LOOK(279)=MB_TRUE
> LOOK(281)
< LOOK(281)=MB_FALSE
decision 9 is simple LL(1)
: error 24 : template error: ASTTreeParser.stg 321:25: anonymous template has 0 arg(s) but mapped across 1 value(s)
: error 24 : template error: ASTTreeParser.stg 323:25: anonymous template has 0 arg(s) but mapped across 1 value(s)
> LOOK(32)
< LOOK(32)={UP, MB_CASE_INSENSITIVE..MB_CASE_SENSITIVE, MB_ENCODE_ANSI..MB_ENCODE_UNICODE}
> LOOK(57)
< LOOK(57)=RULE
> LOOK(69)
< LOOK(69)=MB_HEADER
> LOOK(80)
< LOOK(80)=MB_METADATA
> LOOK(92)
< LOOK(92)='Id'
> LOOK(102)
< LOOK(102)='Version'
> LOOK(112)
< LOOK(112)='Author'
> LOOK(122)
< LOOK(122)='Date'
> LOOK(132)
< LOOK(132)='Brief'
> LOOK(148)
< LOOK(148)=CONDITION
> LOOK(167)
< LOOK(167)=STRING
> LOOK(180)
< LOOK(180)={IDENTIFIER..INT, MB_AND, MB_EQ..MB_GTEQ, MB_LT..MB_LTEQ, MB_NEQ, MB_OR, MB_TRUE, STRING}
> LOOK(192)
< LOOK(192)={IDENTIFIER..INT, MB_AND, MB_EQ..MB_GTEQ, MB_LT..MB_LTEQ, MB_NEQ, MB_OR, MB_TRUE, STRING}
> LOOK(204)
< LOOK(204)={IDENTIFIER..INT, MB_AND, MB_EQ..MB_GTEQ, MB_LT..MB_LTEQ, MB_NEQ, MB_OR, MB_TRUE, STRING}
> LOOK(216)
< LOOK(216)={IDENTIFIER..INT, MB_AND, MB_EQ..MB_GTEQ, MB_LT..MB_LTEQ, MB_NEQ, MB_OR, MB_TRUE, STRING}
> LOOK(228)
< LOOK(228)={IDENTIFIER..INT, MB_AND, MB_EQ..MB_GTEQ, MB_LT..MB_LTEQ, MB_NEQ, MB_OR, MB_TRUE, STRING}
> LOOK(240)
< LOOK(240)={IDENTIFIER..INT, MB_AND, MB_EQ..MB_GTEQ, MB_LT..MB_LTEQ, MB_NEQ, MB_OR, MB_TRUE, STRING}
> LOOK(252)
< LOOK(252)={IDENTIFIER..INT, MB_AND, MB_EQ..MB_GTEQ, MB_LT..MB_LTEQ, MB_NEQ, MB_OR, MB_TRUE, STRING}
> LOOK(264)
< LOOK(264)={IDENTIFIER..INT, MB_AND, MB_EQ..MB_GTEQ, MB_LT..MB_LTEQ, MB_NEQ, MB_OR, MB_TRUE, STRING}
是我语法有问题还是C Generator Template有错误?我看过一些类似错误的报告,例如 this one , 但总是与旧版本的 ANTLR 一起使用。
如果有任何帮助,我将不胜感激。提前致谢。
更新 1:使用 bart-kiers 建议更新了 RuleParser.g 语法。
最佳答案
终于找到问题了。RuleParserTree.g 文件中的 output = AST
选项被遗留下来。我删除了它,终于正确生成了代码。
关于java - ANTLR3 C 目标错误生成 TreeParser : ( ASTTreeParser. stg 321:25:匿名模板有 0 个参数但映射到 1 个值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39870126/
简而言之:我想从可变参数模板参数中提取各种选项,但不仅通过标签而且通过那些参数的索引,这些参数是未知的 标签。我喜欢 boost 中的方法(例如 heap 或 lockfree 策略),但想让它与 S
我可以对单元格中的 excel IF 语句提供一些帮助吗? 它在做什么? 对“BaselineAmount”进行了哪些评估? =IF(BaselineAmount, (Variance/Baselin
我正在使用以下方法: public async Task Save(Foo foo,out int param) { ....... MySqlParameter prmparamID
我正在使用 CodeGear RAD Studio IDE。 为了使用命令行参数测试我的应用程序,我多次使用了“运行 -> 参数”菜单中的“参数”字段。 但是每次我给它提供一个新值时,它都无法从“下拉
我已经为信用卡类编写了一些代码,粘贴在下面。我有一个接受上述变量的构造函数,并且正在研究一些方法将这些变量格式化为字符串,以便最终输出将类似于 号码:1234 5678 9012 3456 截止日期:
MySql IN 参数 - 在存储过程中使用时,VarChar IN 参数 val 是否需要单引号? 我已经像平常一样创建了经典 ASP 代码,但我没有更新该列。 我需要引用 VarChar 参数吗?
给出了下面的开始,但似乎不知道如何完成它。本质上,如果我调用 myTest([one, Two, Three], 2); 它应该返回元素 third。必须使用for循环来找到我的解决方案。 funct
将 1113355579999 作为参数传递时,该值在函数内部变为 959050335。 调用(main.c): printf("%d\n", FindCommonDigit(111335557999
这个问题在这里已经有了答案: Is Java "pass-by-reference" or "pass-by-value"? (92 个回答) 关闭9年前。 public class StackOve
我真的很困惑,当像 1 == scanf("%lg", &entry) 交换为 scanf("%lg", &entry) == 1 没有区别。我的实验书上说的是前者,而我觉得后者是可以理解的。 1 =
我正在尝试使用调用 SetupDiGetDeviceRegistryProperty 的函数使用德尔福 7。该调用来自示例函数 SetupEnumAvailableComPorts .它看起来像这样:
我需要在现有项目上实现一些事件的显示。我无法更改数据库结构。 在我的 Controller 中,我(从 ajax 请求)传递了一个时间戳,并且我需要显示之前的 8 个事件。因此,如果时间戳是(转换后)
rails 新手。按照多态关联的教程,我遇到了这个以在create 和destroy 中设置@client。 @client = Client.find(params[:client_id] || p
通过将 VM 参数设置为 -Xmx1024m,我能够通过 Eclipse 运行 Java 程序-Xms256M。现在我想通过 Windows 中的 .bat 文件运行相同的 Java 程序 (jar)
我有一个 Delphi DLL,它在被 Delphi 应用程序调用时工作并导出声明为的方法: Procedure ProduceOutput(request,inputs:widestring; va
浏览完文档和示例后,我还没有弄清楚 schema.yaml 文件中的参数到底用在哪里。 在此处使用 AWS 代码示例:https://github.com/aws-samples/aws-proton
程序参数: procedure get_user_profile ( i_attuid in ras_user.attuid%type, i_data_group in data_g
我有一个字符串作为参数传递给我的存储过程。 dim AgentString as String = " 'test1', 'test2', 'test3' " 我想在 IN 中使用该参数声明。 AND
这个问题已经有答案了: When should I use "this" in a class? (17 个回答) 已关闭 6 年前。 我运行了一些java代码,我看到了一些我不太明白的东西。为什么下
我输入 scroll(0,10,200,10);但是当它运行时,它会传递字符串“xxpos”或“yypos”,我确实在没有撇号的情况下尝试过,但它就是行不通。 scroll = function(xp
我是一名优秀的程序员,十分优秀!