MarkMa MarkMa - 2 months ago 13
Java Question

Why I add the "cppInclude" parser then the whole g4 doesn't work

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


text:

[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);
}


Before add the "cppInclude" parser,everything goes well.
But when I write the text:

#include <iostream.h>
[KS_MyTest](name,code)
{
}


the analyzer doesn't work well.
It notice me:

line 20:0 extraneous input '[KS_MyTest]' expecting {<EOF>, '{', '(', 'if', 'while', 'for', 'break', 'continue', 'goto', 'return', '!', '++', '--', ''', STRING, VARNAME, '#include'}


How could I fix the bug?

Answer

This production:

  prog: kinglangStat+     |     cppStat+;

says that a prog is either a sequence of kinglangStat or a sequence of cppStat.

Your example is a cppStat followed by a kinglangStat (I think). That isn't a prog.


How could I fix the bug?

Try this:

  prog: ( kinglangStat | cppStat )+;

or

  prog: stat+;
  stat: kinglangStat | cppStat;

(I am not a Antlr expert. I'm just reading the grammar ... as a grammar.)