为什么我添加了 “cppInclude” 解析器然后整个G4不起作用

G4:为什么我添加了 “cppInclude” 解析器然后整个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其次是 a kinglangStat(我认为)。那不是prog


我怎么会修正错误?

试试这个:

prog: (kinglangStat | cppStat)+; 

prog: stat+; 

stat: kinglangStat | cppStat;

(我不是一个ANTLR的专家,我只是阅读语法......作为一个语法。)

以上是 为什么我添加了 “cppInclude” 解析器然后整个G4不起作用 的全部内容, 来源链接: utcz.com/qa/261772.html

回到顶部