質問

私は中規模の言語の文法を書いている、と私は、フォームの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')
    ;

レクサーとパーサが完全に独立していることに注意してください。レクサーははパーサがグループにそれらを取得し、の、パーサーに渡さされるトークンを決定します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top