我正在尝试构建一个简单的语法,但没有成功。我阅读了ANTLR官方文档,但无法弄清问题是什么。以下代码是语法。
grammar Splitter;
splitter : statements EOF;
statements : statement statement*;
statement: (yieldStatement | variableDeclarationStatement | forStatement) ';';
block : '{' statements? '}';
yieldStatement: 'yield' expression;
variableDeclarationStatement: VariableDeclaratorId '=' expression;
VariableDeclaratorId: Identifier;
forStatement: 'for' VariableDeclaratorId 'in' forIterable block;
forIterable: expression;
methodInvocation: MethodName '(' argumentList? ')';
MethodName: Identifier;
argumentList: argumentName (',' argumentName)*;
argumentName: expression;
expression: objectExpression | numericExpression | textExpression | variableAcessor | methodInvocation;
variableAcessor: VariableDeclaratorId ('[' expression ']')*;
objectExpression: '{' keyValueStatementList? '}';
keyValueStatementList: keyValueStatement (',' keyValueStatement)*;
keyValueStatement: keyStatement ':' valueStatement;
keyStatement: textExpression;
valueStatement: expression;
Identifier: [a-zA-Z_]+;
numericExpression : IntegerLiteral;
IntegerLiteral : [0-9]+;
textExpression : '"' StringCharacters? '"';
StringCharacters: StringCharacter StringCharacter*;
fragment StringCharacter: ~["\\] | EscapeSequence;
fragment EscapeSequence: '\\' [btnfr"'\\];
SPACE: [ \t\r\n]+ -> skip;
我的测试是非常简单的输入,不使用所有语法,例如 yield 1; , yield“a”; 和 DATA = { “关键”:“Hello world”};产量数据; 。
任何?