[Python] Matching rule of antlr regex

Discussão em 'Python' iniciado por Stack, Janeiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    Im working on a lexer of a school project, using antlr4. The string literals of the language defined like this: grammar MiniGo;

    @lexer::header {
    from lexererr import *
    }

    @lexer::members {
    def emit(self):
    tk = self.type
    if tk == self.UNCLOSE_STRING:
    result = super().emit();
    raise UncloseString(result.text[1:]);
    elif tk == self.ILLEGAL_ESCAPE:
    result = super().emit();
    raise IllegalEscape(result.text[1:]);
    elif tk == self.ERROR_CHAR:
    result = super().emit();
    raise ErrorToken(result.text);
    else:
    return super().emit();
    }

    options{
    language=Python3;
    }

    program : decl+ EOF ;

    decl: funcdecl | vardecl ;

    vardecl: 'var' ID 'int' ';' ;

    NL: '\n' -> skip; //skip newlines
    WS : [ \t\r]+ -> skip ; // skip spaces, tabs

    funcdecl: 'func' ID '(' ')' '{' '}' ';' ;
    // identifiers
    ID: [a-zA-Z_] [a-zA-Z0-9_]*;

    // String literals
    ESC: '\\' [ntr"\\];
    STRLIT: '"' (~["\\] | ESC)* '"';

    // Exceptions
    ERROR_CHAR: .;
    ILLEGAL_ESCAPE: '"' (~["\\])* ('\\' ~["\\rnt]);
    UNCLOSE_STRING: '"' (~["\\] | ESC)* ('\n' | '\r' | EOF);


    When i go an input like hello\. It matches the STRLIT, not the UNCLOSED_STRING one. But I can't understand why it can match the STRLIT? If it matches by the \ character, it cannot match the last ". But it also can't match the last " as the close quote because i dont allows \ stand alone in the input as well. Can someone explain this to me? May be i missed something in the antlr matching rules?

    Continue reading...

Compartilhe esta Página