作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
skip ; LIN-6ren">
g4:
grammar KBDP;
@header {package kbdp.translator.antlr;}
COMMENT: '/*' .*? '*/' -> skip ; LINE_COMMENT: '//' ~[\r\n]* '\r'? '\n' -> skip;
KS:'[' ('KS_'|'KA_') MIX+ ']';
STRING : '"' ~[\r\n"]+ '"';
fragment NUM:[0-9]+; VARNAME:[_a-zA-Z-0-9]+;
INCLUDE :'#include' ;
MIX : CHINESE | VARNAME ;
CHINESE : ('\u4E00'..'\u9FA5')+ ; ARG : VARNAME|STRING ;
DB : '[' '数据库' ']'; SQL : '[' 'SQL' NUM ']'; SQLRESULT: '[''SQL' NUM '有数据'']'; SQLREADLINE:'[' '列' NUM ']'; RESULTWIRTELINE:'[' '结果集' NUM ']'; RETURNMULTI:'[' '返回值' NUM '有数据' ']'; RETURNSINGLE:'[' '返回值' NUM ']'; PRINT:'[' '打印' ']'; WS: [\r\n \t] -> skip;
prog: kinglangStat+ | cppStat+;
block:'{' prog* '}';
kinglangStat:kinglangServiceDeclaration |
kinglangDBOpen |
kinglangSQL |
kinglangSQLResult |
kinglangSQLReadLine |
kinglangResultDeclare |
kinglangResultWriteLine |
kinglangFunctionCall |
kinglangFunctionReturnSingle |
kinglangFunctionReturnMulti |
kinglangPrintStatus;
kinglangServiceDeclaration: KS '(' VARNAME? (',' VARNAME)* ')' '{' prog* '}'; kinglangDBOpen:(VARNAME '=')? DB '(' (VARNAME|STRING) ')' ';';
kinglangSQL:(VARNAME '=')? SQL '(' STRING? ')' ';' ;
kinglangSQLResult:SQLRESULT block; kinglangSQLReadLine:SQLREADLINE '(' VARNAME ')' ';'; kinglangResultDeclare:RESULTWIRTELINE ';';
kinglangResultWriteLine:RESULTWIRTELINE '(' kinglangArg? (',' kinglangArg)* ')' ';'; kinglangArg : VARNAME|STRING ;
kinglangFunctionCall:KS '(' (VARNAME|STRING)? (',' (VARNAME|STRING))* ')' ';';
kinglangFunctionReturnSingle:RETURNSINGLE '(' VARNAME? (',' VARNAME)* ')' ';';
kinglangFunctionReturnMulti:RETURNMULTI block;
kinglangPrintStatus:PRINT '(' VARNAME|STRING ')' ';';
cppStat: block |
cppBreakStat |
cppContinueStat|
cppReturnStat |
cppSingleStat |
cppIfStat |
cppWhileStat |
cppGotoStat |
cppLabelStat |
cppForStat |
cppInclude;
cppIfStat: cppIfStatPart cppElseifPart* cppElsePart?;
cppIfStatPart:'if' '(' expression ')' (cppSingleStat|block)?;
cppElseifPart:'else if' '(' expression ')' (cppSingleStat|block)? ;
cppElsePart:'else' (cppSingleStat|block)?;
cppWhileStat:'while' '(' expression ')' block;
cppForStat:'for' '(' cppForCondition1?';' cppForCondition2?';'cppForCondition3?')' block; cppForCondition1:expression; cppForCondition2:expression; cppForCondition3:expression;
cppBreakStat:'break' ';';
cppContinueStat:'continue' ';';
cppGotoStat:'goto' expression ';' ;
cppLabelStat:VARNAME ':' ;
cppReturnStat: 'return' VARNAME? ';';
cppSingleStat: expression ';';
cppInclude: INCLUDE ('<'|'"') VARNAME '.' VARNAME ('>'|'"') ';';
expression: VARNAME |
STRING |
'!' expression |
expression '=' expression |
expression ('<'|'>') expression |
expression expression |
expression ('+'|'-'|'*'|'/'|'%') expression |
expression ('=='|'!=') expression |
expression ('>='|'<=') expression |
expression ('&&'|'||') expression |
expression ('++'|'--') |
('++'|'--') expression |
'(' expression ')'|
'\'' expression '\'' |
expression ',' expression |
expression expression | //var decl
expression '<' expression '>' expression | //class template
expression '[' expression ']' | //array
expression '.' expression | //class obj
expression '(' expression ')'; //func call
文字:
[KS_MyTest](name,code)
{
char szCredit[1024];
memset(szCredit,0,sizeof(szCredit));
[数据库]("DB");
[SQL1]("select * from myTable where name='@name' and code='@code'");
[SQL1有数据]
{
[列0](szCredit);
}
[结果集1];
[结果集1]("ERROE_SUCCESS",0,0);
[结果集2];
[结果集2](szCredit);
}
在添加“cppInclude”解析器之前,一切顺利。但是当我写下文字时:
#include <iostream.h>
[KS_MyTest](name,code)
{
}
分析仪无法正常工作。它注意到了我:
line 20:0 extraneous input '[KS_MyTest]' expecting {<EOF>, '{', '(', 'if', 'while', 'for', 'break', 'continue', 'goto', 'return', '!', '++', '--', ''', STRING, VARNAME, '#include'}
如何修复该错误?
最佳答案
本次制作:
prog: kinglangStat+ | cppStat+;
表示 prog
是 kinglangStat
序列或 cppStat
序列。
你的例子是一个cppStat
后跟一个kinglangStat
(我认为)。这不是prog
。
How could I fix the bug?
试试这个:
prog: ( kinglangStat | cppStat )+;
或
prog: stat+;
stat: kinglangStat | cppStat;
(我不是 Antlr 专家。我只是在阅读语法......作为语法。)
关于java - 为什么我添加了 "cppInclude"解析器然后整个 g4 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39467472/
g4: grammar KBDP; @header {package kbdp.translator.antlr;} COMMENT: '/*' .*? '*/' -> skip ; LIN
我是一名优秀的程序员,十分优秀!