ANTLR:他の数値リテラルも可能であるとき、2桁の数字を解析
-
23-08-2019 - |
質問
私は中規模の言語の文法を書いている、と私は、フォームのhh:mm:ss
の時間リテラルを実装しようとしています。
12:34:56
としてtimeLiteral
を解析しようとするたびに、しかし、私は数字上の不一致のトークンの例外を取得します。誰もが私が間違っているかもしれないものを知っていますか?
ここでは、現在定義されている関連のルールがあります:
timeLiteral
: timePair COLON timePair COLON timePair -> ^(TIMELIT timePair*)
;
timePair
: DecimalDigit DecimalDigit
;
NumericLiteral
: DecimalLiteral
;
fragment DecimalLiteral
: DecimalDigit+ ('.' DecimalDigit+)?
;
fragment DecimalDigit
: ('0'..'9')
;
解決
問題は、レクサーはDecimalDigitをgobblingとNumericLiteralを返しているということです。
それはフラグメントルールですので、パーサはDecimalDigitsを参照することはありません。
私は(その名を大文字)レクサーにtimeLiteralを移動することをお勧めします。だからあなたのようなものを持っていると思います。
timeLiteral
: TimeLiteral -> ^(TIMELIT TimeLiteral*)
;
number
: DecimalLiteral
;
TimeLiteral
: DecimalDigit DecimalDigit COLON
DecimalDigit DecimalDigit COLON
DecimalDigit DecimalDigit
;
DecimalLiteral
: DecimalDigit+ ('.' DecimalDigit+)?
;
fragment DecimalDigit
: ('0'..'9')
;
レクサーとパーサが完全に独立していることに注意してください。レクサーははパーサがグループにそれらを取得し、の、パーサーに渡さされるトークンを決定します。
所属していません StackOverflow