Parsing fortran-style .op. operators

后端 未结 1 782
南旧
南旧 2021-01-17 04:18

I\'m trying to write an ANTLR4 grammar for a fortran-inspired DSL. I\'m having difficulty with the \'ole classic \".op.\" operators:

if (1.and.1) then
         


        
1条回答
  •  臣服心动
    2021-01-17 05:17

    To disambiguate DOT, add a lexer rule with a predicate just before the DOT rule.

    DIT : DOT { isDIT() }? ;
    DOT : '.' ;
    

    Change the 'andOp'

    andOp : DIT 'and' DIT ;
    

    Then add a predicate method

    @lexer::members {
    
    public boolean isDIT() {
        int offset = _tokenStartCharIndex;
        String r = _input.getText(Interval.of(offset-4, offset));
        String s = _input.getText(Interval.of(offset, offset+4));
        if (".and.".equals(s) || ".and.".equals(r)) {
            return true;
        }
        return false;
    }
    
    }
    

    But, that is not really the source of your current problem. The integer parser rule defines lexer constants effectively outside of the lexer, which is why 'b' is not matched to an ID.

    Change it to

    integer : INT ;
    
    INT:  DIGITS ('q'|'Q'|'l'|'L'|'h'|'H'|'b'|'B'|'i'|'I')? ;
    

    and the lexer will figure out the rest.

    0 讨论(0)
提交回复
热议问题