我正在为我正在处理的自定义脚本编写解析器。ANTLR4:错误的输入错误
当我喂低于基本脚本:
if (db.Title == 2) { }
这是假设匹配conditionStatement
。但是当我运行它时,我会遇到错误。当我尝试输入馈电在for循环会出现同样的错误:
line 1:0 extraneous input 'if'
这里是我的语法:
grammar CustomScript;
/*
* Parser Rules
*/
prog
: compoundScript EOF
;
compoundScript
: ('{' script '}')+
| (script)
;
script
: (declaration* statement*)
| statement*
;
iterationStatement
: 'for' '(' identifier 'in' collection ')' statement
;
collection
: list|data | identifier
;
statement
: expressionStatement
| conditionStatement
| iterationStatement
| '{' statement '}'
;
conditionStatement
: 'if' '(' expression ')' statement ('else' statement)?
;
expressionStatement
: expression ';'?
;
declaration
: constantObjectDeclaration+
;
constantObjectDeclaration
: 'set' identifier 'as' constantObject ';'?
;
constantObject
: identifier '.' identifier '[' value ']'
;
expression
: assignmentExpression
| expression ',' assignmentExpression
;
assignmentExpression
: postfixExpression assignmentOperator=AssignmentOperator postfixExpression
| conditionalExpression
;
conditionalExpression
: logicalOrExpression ('?' expression ':' conditionalExpression)?
;
logicalOrExpression
: logicalAndExpression
| logicalOrExpression '||' logicalAndExpression
;
logicalAndExpression
: logicalCondition
| logicalAndExpression '&&' logicalCondition
;
postfixExpression
: primaryExpression
| postfixExpression '[' expression ']'
| postfixExpression '.' Advance '.' Digits '.' identifier
| postfixExpression '.' Main '.' Digits '.' identifier
| postfixExpression '.' Split '.' Digits '.' identifier
| postfixExpression '++' // Ensure only variable++
| postfixExpression '--' // Ensure only variable--
;
primaryExpression
: identifier
| constant
| stringLiteral+
| calcExpr
| list
| '(' expression ')'
;
calcExpr
: calcExpr op=(MulDivOperator | '^') calcExpr # MulDivExpo
| calcExpr op=AddSubOperator calcExpr # AddSub
| number # IntValue
| data # DataValue
| identifier # IdentifierValue
| constant # ConstantValue
| '(' calcExpr ')' # ExprWithParens
;
logicalCondition
: data instructionOption number # LeftDataRightNumber
| number instructionOption data # LeftNumberRightData
| data instructionOption data # LeftDataRightData
| constant instructionOption DateTime # LeftConstantRightDateTime
| data instructionOption DateTime # LeftDataRightDateTime
| DateTime instructionOption data # LeftDateTimeRightData
| DateTime instructionOption constant # LeftDateTimeRightConstant
| data instructionOption String # LeftDataRightString
| String instructionOption data # LeftStringRightData
| String instructionOption constant # LeftStringRightConstant
| constant instructionOption constant # LeftConstantRightConstant
| constant instructionOption String # LeftConstantRightString
| constant instructionOption number # LeftConstantRightNumber
| number instructionOption constant # LeftNumberRightConstant
| constant instructionOption data # LeftConstantRightData
| data instructionOption constant # LeftDataRightConstant
| identifier instructionOption constant # LeftIdentifierRightConstant
| constant instructionOption identifier # LeftConstantRightIdentifier
| identifier instructionOption data # LeftIdentifierRightData
| data instructionOption identifier # LeftDataRightIdentifier
| data In list # LeftDataInRightList
| constant In list # LeftConstantInRightList
| value In list # LeftValueInRightList
| identifier In list # LeftIdentifierInRightList
| '(' logicalCondition ')' # ConditionWithParens
;
instructionOption
: Keyword | Operator | AssignmentOperator | MulDivOperator | AddSubOperator
;
list
: ('[' (identifierList)? ']')
;
identifierList
: (identifier ','?)
| ((identifier ',')+ (identifier ','?))+
;
value
: number
| stringLiteral
;
constant
: (('$''{') (NonDigit | DigitsNondigits) '}')
;
data
: identifier '.' (MetaData '.')? identifier
;
datetime
: Quote Digits '-' Digits '-' Digits Quote
| Quote Digits '/' Digits '/' Digits Quote
| Quote Digits '-' Digits '-' Digits Space Digits ':' Digits Quote
| Quote Digits '/' Digits '/' Digits Space Digits ':' Digits Quote
;
deliveryType
: Advances | Mains | Splits
;
number
: Digits (Dot Digits)?
;
Keyword
: Else
| For
| If
| Before
| Contains
| After
| And
| Or
| In
| Set
| StartsWith
| EndsWith
;
separator
: '-' | '/' | ':'
;
identifier
: NonDigit | DigitsNondigits
;
NonDigit
: [a-zA-Z_]+
;
DigitsNondigits
: [a-zA-Z0-9_]+
;
Digits
: [0-9]+
;
stringLiteral
: Quote SCharSequence? Quote
;
Quote
: ('\"'|'\'') -> channel(HIDDEN)
;
fragment
SCharSequence
: SChar+
;
fragment
SChar
: ~["\\\r\n']
| EscapeSequence
;
fragment
EscapeSequence
: SimpleEscapeSequence
;
fragment
SimpleEscapeSequence
: '\\' ['"?abfnrtv\\]
;
//fragment
AssignmentOperator
: '='
| '*='
| '/='
| '%='
| '+='
| '-='
| '^='
;
MulDivOperator
: '*'
| '/'
;
AddSubOperator
: '+'
| '-'
;
Operator
: '%'
| '&'
| '!'
| '<'
| '>'
| '=='
| '!='
| '<='
| '>='
| '&&'
| '||'
| '++'
| '--'
| ','
;
/*
* Lexer Rules
*/
Deliveries : 'Deliveries';
Advances : 'Advances';
Mains : 'Mains';
Splits : 'Splits';
Else : 'else';
For : 'for';
If : 'if';
Advance : 'Advance';
Main : 'Main';
Split : 'Split';
Before : 'before';
Contains : 'contains';
After : 'after';
And : 'and';
Or : 'or';
In : 'in';
Set : 'Set';
StartsWith : 'startsWith';
EndsWith : 'endsWith';
MetaData : 'MetaData';
//LeftParen : '(';
//RightParen : ')';
LeftBracket : '[';
RightBracket : ']';
//LeftBrace : '{';
//RightBrace : '}';
Less : '<';
LessEqual : '<=';
Greater : '>';
GreaterEqual : '>=';
Plus : '+';
PlusPlus : '++';
Minus : '-';
MinusMinus : '--';
Star : '*';
Div : '/';
Mod : '%';
AndAnd : '&&';
OrOr : '||';
Caret : '^';
Not : '!';
Question : '?';
Colon : ':';
Semi : ';';
Comma : ',';
Assign : '=';
StarAssign : '*=';
ExpoAssign : '^=';
DivAssign : '/=';
ModAssign : '%=';
PlusAssign : '+=';
MinusAssign : '-=';
Equal : '==';
NotEqual : '!=';
Space : ' ' -> skip;
DoubleQuote : '"';
Dot : '.';
NewLine : ('\r' '\n' ? | '\n') -> skip;
BlockComment : '/*' .*? '*/' -> skip;
LineCommentOption1 : '//' ~[\r\n]* -> skip;
LineCommentOption2 : '#' ~[\r\n]* -> skip;
WS : ('\r' | '\n' | '\t') -> channel(HIDDEN);
注
编辑:由于原始表达式输入错误问题解决 - 以上语法。还请注意,在赏金记录中注明的list
表达问题"No viable alternative input"
也可以通过上面编辑的语法解决。
我打了一下与你的语法和[这里](https://开头要点。 github.com/anonymous/e4ee73d8db14dd1870c2)是一些输出(我个人不认为这是一个答案,因为你的语法肯定需要比知道更多的人做更多的工作 - 但你可能会发现这种鼓舞人心的或有趣的)。祝你好运! – vlp
@vlp考虑你的增强和对它的一点改变 - 我能够做出一点点头。但是我的一些表达式产生了“没有可行的替代输入,当你提供'a = [1]'时,例子列表语法有效,但当你提供'a = [1,2,3]'时不会。”等等。如果条件语法 – Bitmap
由于'list'表达式导致“没有可行的替代输入”问题可以通过编辑后的语法来解决 – Bitmap