我有以下BNF:
block
: statement+
;
/*input
: (simple_statement
| complex_statement NEWLINE)
;*/
statement
: simple_statement /*{missingToken();}*/ (SCOLON simple_statement)* SCOLON? NEWLINE?
| complex_statement /*{System.out.println("COMPLEX");}*/
;
simple_statement
: assignment /*{System.out.println("ASSIGNMENT");}*/
| functionCall /* {System.out.println("FUNC CALL");}*/
| simple_expression /*{System.out.println("SIMPLESTATEMENT EXPRESSION");}*/
;
complex_statement
: ifStatement
| forStatement
| whileStatement
| functionDecl
;
assignment
: IDENTIFIER '=' expression
;
functionCall
: IDENTIFIER OPAREN {missingIdentExprList();} exprList? CPAREN?
/*{System.out.println("IDENTIFIER CALL");} */ #identifierFunctionCall
| {missingPrint();} PRINT {missingOparen();} OPAREN {missingPrintExprList();}
exprList? CPAREN? #printFunctionCall
;
simple_expression
: expression
;
ifStatement
: ifStat elifStat* elseStat?
;
ifStat
: /*{countingIf();}*/ IF {missingExpression();} expression indentationBlock
;
elifStat
: ELIF {missingExpression();} expression indentationBlock
;
elseStat
: /*{countingElse();}*/ ELSE indentationBlock
;
functionDecl
: DEF {missingIdentifier();} IDENTIFIER {missingOparen();} OPAREN {missingDefinitionFunction();} idList? CPAREN? indentationBlock
;
forStatement
: FOR {erroreSintattico();} IDENTIFIER IN {missingExpression();} expression indentationBlock elseStat?
;
whileStatement
: WHILE {missingExpression();} expression indentationBlock /*{proseguimentoWhile();}*/ elseStat?
;
indentationBlock
: {missingColon();}COLON NEWLINE {missingIndent();} INDENT {missingIndentationBlock();}block {missingDedent();} DEDENT
;
idList
: IDENTIFIER (',' IDENTIFIER)* {missingDefinitionFunction();}
| IDENTIFIER COMMA {missingDefinitionFunction();}
;
exprList
: expression (COMMA {doubleComma();} {missingListExpression();} expression)*
{missingCparen();}
| (expression COMMA)+ {doubleComma();} {missingCparen();}
;
expression
: '-' {erroreSintattico();} expression #unaryMinusExpression
// | '!' {missingExpression();} expression #notExpression
| expression '**' /*{erroreSintattico();}*/ expression #powerExpression
| expression '*' /*{erroreSintattico();}*/ expression #multiplyExpression
| expression '/' /*{erroreSintattico();}*/ expression #divideExpression
| expression '%' /*{erroreSintattico();}*/ expression #modulusExpression
| expression '+' expression #addExpression
| expression '-' expression #subtractExpression
| expression '>=' /*{erroreSintattico();}*/ expression #gtEqExpression
| expression '<=' /*{erroreSintattico();}*/ expression #ltEqExpression
| expression '>' /*{erroreSintattico();}*/ expression #gtExpression
| expression '<' /*{erroreSintattico();}*/ expression #ltExpression
| expression '==' /*{erroreSintattico();}*/ expression #eqExpression
| expression '!=' /*{erroreSintattico();}*/ expression #notEqExpression
| expression AND /*{erroreSintattico();}*/ expression #andExpression
| expression '|' /*{erroreSintattico();}*/ expression #orExpression
//| expression '?' expression ':' expression #ternaryExpression
//| expression IN expression #inExpression
| NUMBER {espressioneRipetuta();}{System.out.println("NUMBER");} #numberExpression
| BOOL /* {System.out.println("BOOL");} */ #boolExpression
| NULL /*{System.out.println("NULL");}*/ #nullExpression
| functionCall /*{System.out.println("FUNC CALL");}*/ #functionCallExpression
| list /*{System.out.println("LIST");}*/ #listExpression
| IDENTIFIER /*{System.out.println("IDENTIFIER");}*/ #identifierExpression
| STRING /*{System.out.println("STRING");}*/ #stringExpression
| INPUT OPAREN expression? COMMA? {missingInput();} CPAREN? /*{System.out.println("INPUT");}*/ #inputExpression
| OPAREN /*{System.out.println("EXPRESSION");}*/ expression {parentesiChiusa();}CPAREN #expressionExpression
;`
老实说,我真的不明白为什么所有的操作输入如下: 2 * 3; 8/4; 2 ** 9; ... 工作,只是输入'+'或' - '令牌不起作用。 实际上像2 + 3这样的输入给了我这个输出: 2 \ n 3.这意味着被认为表达式= NUMBER两次但我真的不明白为什么解析器不考虑以下生产: 表达式'+'表达式。 令人难以置信的是输入2 ++ 3有效。但为什么不考虑第一个'+'标记。