2

私は適度なサイズの言語の文法を書いています、そして私は形式の時間リテラルを実装しようとしています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')
    ;
4

1 に答える 1

4

問題は、レクサーがDecimalDigitをゴブリングし、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')
    ;

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

于 2009-05-20T13:37:23.147 に答える