Doubt with the switch of the Generator of Lexical and Syntactic Analyzers (GALS)

1

GALS generates the code in Java. I want to put it in the eclipse to make a little screen and such. I was told that in method nextState of class lexico , you have to put a switch with a number. But where do these numbers come from?

private int nextState(char c, int state)
{
    switch (state)
    {
        case 0:
            switch (c)
            {
                case 9: return 1;
                case 10: return 1;
                case 13: return 1;
                case 32: return 1;
                case 33: return 2;
                case 34: return 3;
                case 40: return 4;
                case 41: return 5;
                case 42: return 6;
                case 43: return 7;
                case 44: return 8;
                case 45: return 9;
                case 47: return 10;
                case 48: return 11;
                case 49: return 11;
                case 50: return 11;
                case 51: return 11;
                case 52: return 11;
                case 53: return 11;
                case 54: return 11;
                case 55: return 11;
                case 56: return 11;
                case 57: return 11;
                case 59: return 12;
                case 60: return 13;
                case 61: return 14;
                case 62: return 15;
                case 65: return 16;
                case 66: return 16;
                case 67: return 16;
                case 68: return 16;
                case 69: return 16;
                case 70: return 16;
                case 71: return 16;
                case 72: return 16;
                case 73: return 16;
                case 74: return 16;
                case 75: return 16;
                case 76: return 16;
                case 77: return 16;
                case 78: return 16;
                case 79: return 16;
                case 80: return 16;
                case 81: return 16;
                case 82: return 16;
                case 83: return 16;
                case 84: return 16;
                case 85: return 16;
                case 86: return 16;
                case 87: return 16;
                case 88: return 16;
                case 89: return 16;
                case 90: return 16;
                case 97: return 16;
                case 98: return 16;
                case 99: return 16;
                case 100: return 16;
                case 101: return 16;
                case 102: return 16;
                case 103: return 16;
                case 104: return 16;
                case 105: return 16;
                case 106: return 16;
                case 107: return 16;
                case 108: return 16;
                case 109: return 16;
                case 110: return 16;
                case 111: return 16;
                case 112: return 16;
                case 113: return 16;
                case 114: return 16;
                case 115: return 16;
                case 116: return 16;
                case 117: return 16;
                case 118: return 16;
                case 119: return 16;
                case 120: return 16;
                case 121: return 16;
                case 122: return 16;
                case 123: return 17;
                case 125: return 18;
                default: return -1;
            }
        case 1:
            switch (c)
            {
                case 9: return 1;
                case 10: return 1;
                case 13: return 1;
                case 32: return 1;
                default: return -1;
            }
        case 3:
            switch (c)
            {
                case 9: return 3;
                case 10: return 3;
                case 13: return 3;
                case 32: return 3;
                case 33: return 3;
                case 34: return 19;
                case 35: return 3;
                case 36: return 3;
                case 37: return 3;
                case 38: return 3;
                case 39: return 3;
                case 40: return 3;
                case 41: return 3;
                case 42: return 3;
                case 43: return 3;
                case 44: return 3;
                case 45: return 3;
                case 46: return 3;
                case 47: return 3;
                case 48: return 3;
                case 49: return 3;
                case 50: return 3;
                case 51: return 3;
                case 52: return 3;
                case 53: return 3;
                case 54: return 3;
                case 55: return 3;
                case 56: return 3;
                case 57: return 3;
                case 58: return 3;
                case 59: return 3;
                case 60: return 3;
                case 61: return 3;
                case 62: return 3;
                case 63: return 3;
                case 64: return 3;
                case 65: return 3;
                case 66: return 3;
                case 67: return 3;
                case 68: return 3;
                case 69: return 3;
                case 70: return 3;
                case 71: return 3;
                case 72: return 3;
                case 73: return 3;
                case 74: return 3;
                case 75: return 3;
                case 76: return 3;
                case 77: return 3;
                case 78: return 3;
                case 79: return 3;
                case 80: return 3;
                case 81: return 3;
                case 82: return 3;
                case 83: return 3;
                case 84: return 3;
                case 85: return 3;
                case 86: return 3;
                case 87: return 3;
                case 88: return 3;
                case 89: return 3;
                case 90: return 3;
                case 91: return 3;
                case 92: return 3;
                case 93: return 3;
                case 94: return 3;
                case 95: return 3;
                case 96: return 3;
                case 97: return 3;
                case 98: return 3;
                case 99: return 3;
                case 100: return 3;
                case 101: return 3;
                case 102: return 3;
                case 103: return 3;
                case 104: return 3;
                case 105: return 3;
                case 106: return 3;
                case 107: return 3;
                case 108: return 3;
                case 109: return 3;
                case 110: return 3;
                case 111: return 3;
                case 112: return 3;
                case 113: return 3;
                case 114: return 3;
                case 115: return 3;
                case 116: return 3;
                case 117: return 3;
                case 118: return 3;
                case 119: return 3;
                case 120: return 3;
                case 121: return 3;
                case 122: return 3;
                case 123: return 3;
                case 124: return 3;
                case 125: return 3;
                case 126: return 3;
                case 161: return 3;
                case 162: return 3;
                case 163: return 3;
                case 164: return 3;
                case 165: return 3;
                case 166: return 3;
                case 167: return 3;
                case 168: return 3;
                case 169: return 3;
                case 170: return 3;
                case 171: return 3;
                case 172: return 3;
                case 173: return 3;
                case 174: return 3;
                case 175: return 3;
                case 176: return 3;
                case 177: return 3;
                case 178: return 3;
                case 179: return 3;
                case 180: return 3;
                case 181: return 3;
                case 182: return 3;
                case 183: return 3;
                case 184: return 3;
                case 185: return 3;
                case 186: return 3;
                case 187: return 3;
                case 188: return 3;
                case 189: return 3;
                case 190: return 3;
                case 191: return 3;
                case 192: return 3;
                case 193: return 3;
                case 194: return 3;
                case 195: return 3;
                case 196: return 3;
                case 197: return 3;
                case 198: return 3;
                case 199: return 3;
                case 200: return 3;
                case 201: return 3;
                case 202: return 3;
                case 203: return 3;
                case 204: return 3;
                case 205: return 3;
                case 206: return 3;
                case 207: return 3;
                case 208: return 3;
                case 209: return 3;
                case 210: return 3;
                case 211: return 3;
                case 212: return 3;
                case 213: return 3;
                case 214: return 3;
                case 215: return 3;
                case 216: return 3;
                case 217: return 3;
                case 218: return 3;
                case 219: return 3;
                case 220: return 3;
                case 221: return 3;
                case 222: return 3;
                case 223: return 3;
                case 224: return 3;
                case 225: return 3;
                case 226: return 3;
                case 227: return 3;
                case 228: return 3;
                case 229: return 3;
                case 230: return 3;
                case 231: return 3;
                case 232: return 3;
                case 233: return 3;
                case 234: return 3;
                case 235: return 3;
                case 236: return 3;
                case 237: return 3;
                case 238: return 3;
                case 239: return 3;
                case 240: return 3;
                case 241: return 3;
                case 242: return 3;
                case 243: return 3;
                case 244: return 3;
                case 245: return 3;
                case 246: return 3;
                case 247: return 3;
                case 248: return 3;
                case 249: return 3;
                case 250: return 3;
                case 251: return 3;
                case 252: return 3;
                case 253: return 3;
                case 254: return 3;
                case 255: return 3;
                default: return -1;
            }
        case 10:
            switch (c)
            {
                case 42: return 20;
                default: return -1;
            }
        case 11:
            switch (c)
            {
                case 46: return 21;
                case 48: return 11;
                case 49: return 11;
                case 50: return 11;
                case 51: return 11;
                case 52: return 11;
                case 53: return 11;
                case 54: return 11;
                case 55: return 11;
                case 56: return 11;
                case 57: return 11;
                default: return -1;
            }
        case 13:
            switch (c)
            {
                case 61: return 22;
                case 62: return 23;
                default: return -1;
            }
        case 14:
            switch (c)
            {
                case 61: return 24;
                default: return -1;
            }
        case 15:
            switch (c)
            {
                case 61: return 25;
                default: return -1;
            }
        case 16:
            switch (c)
            {
                case 48: return 26;
                case 49: return 26;
                case 50: return 26;
                case 51: return 26;
                case 52: return 26;
                case 53: return 26;
                case 54: return 26;
                case 55: return 26;
                case 56: return 26;
                case 57: return 26;
                case 65: return 16;
                case 66: return 16;
                case 67: return 16;
                case 68: return 16;
                case 69: return 16;
                case 70: return 16;
                case 71: return 16;
                case 72: return 16;
                case 73: return 16;
                case 74: return 16;
                case 75: return 16;
                case 76: return 16;
                case 77: return 16;
                case 78: return 16;
                case 79: return 16;
                case 80: return 16;
                case 81: return 16;
                case 82: return 16;
                case 83: return 16;
                case 84: return 16;
                case 85: return 16;
                case 86: return 16;
                case 87: return 16;
                case 88: return 16;
                case 89: return 16;
                case 90: return 16;
                case 95: return 16;
                case 97: return 16;
                case 98: return 16;
                case 99: return 16;
                case 100: return 16;
                case 101: return 16;
                case 102: return 16;
                case 103: return 16;
                case 104: return 16;
                case 105: return 16;
                case 106: return 16;
                case 107: return 16;
                case 108: return 16;
                case 109: return 16;
                case 110: return 16;
                case 111: return 16;
                case 112: return 16;
                case 113: return 16;
                case 114: return 16;
                case 115: return 16;
                case 116: return 16;
                case 117: return 16;
                case 118: return 16;
                case 119: return 16;
                case 120: return 16;
                case 121: return 16;
                case 122: return 16;
                default: return -1;
            }
        default: return -1;
    }
}
    
asked by anonymous 24.06.2017 / 01:05

1 answer

4

Note that the c variable is of type char . So if we use characters instead of numbers according to the ASCII table codes, we will have the following:

private int nextState(char c, int state)
{
    switch (state)
    {
        case 0:
            switch (c)
            {
                case '\t': return 1;
                case '\n': return 1;
                case '\r': return 1;
                case ' ': return 1;
                case '!': return 2;
                case '\"': return 3;
                case '(': return 4;

That is, these numbers are characters of the text that the lex analyzer is processing.

The numbers 1, 2, 3, and 4 that you have in return s are states of your lex analyzer:

  • The analysis begins in state 0.

  • When you find a blank space, line break or tab, it goes to state 1.

  • When it encounters an exclamation point, it enters state 2. I do not know what that means in its language, but state 2 may indicate that it means only that the operator denoted by the exclamation point has been found. It may also be that the exclamation point indicates the beginning of a comment and state 2 would represent being within a comment. Check out in your grammar what it means to find yourself an exclamation mark.

  • When you find a double quotation mark, it enters state 3, which would probably indicate that it is within a string.

Now that you've posted your most complete code, the code you posted would be equivalent to this:

private int nextState(char c, int state) {
    switch (state) {
        case 0:
            switch (c) {
                case '\t':
                case '\n':
                case '\r':
                case ' ': return 1;
                case '!': return 2;
                case '\"': return 3;
                case '(': return 4;
                case ')': return 5;
                case '*': return 6;
                case '+': return 7;
                case ',': return 8;
                case '-': return 9;
                case '/': return 10;
                case ';': return 12;
                case '<': return 13;
                case '=': return 14;
                case '>': return 15;
                case '{': return 17;
                case '}': return 18;
                default:
                    if (c >= '0' && c <= '9') return 11;
                    if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') return 16;
                    return -1;
            }
        case 1:
            if (c == '\t' || c == '\n' || c ==  \r' || c == ' ') return 1;
            return -1;
        case 3:
            if (c == '\"') return 19;
            if (c == '\t' || c == '\n' || c == '\r' || (c >= ' ' && c <= '~') || (c >= 160 && c <= 255) return 3;
            return -1;
        case 10:
            return c == '*' ? 20 : -1;
        case 11:
            if (c == '.') return 21;
            if (c >= '0' && c <= '9') return 11;
            return -1;
        case 13:
            if (c == '=') return 22;
            if (c == '>') return 23;
            return -1;
        case 14:
            if (c == '=') return 24;
            return -1;
        case 15:
            if (c == '=') return 25;
            return -1;
        case 16:
            if (c >= '0' && c <= '9') return 26;
            if ((c >= 'A' && c <= 'Z') || c == '_' || (c >= 'a' && c <= 'z') return 16;
            return -1;
        default:
            return -1;
    }
}

Looking at these status codes based on this table, we come to this:

private static final int ESTADO_ERRO = -1;
private static final int ESTADO_INICIAL = 0;
private static final int ESTADO_ESPACO = 1;
private static final int ESTADO_ACHOU_EXCLAMACAO = 2;
private static final int ESTADO_STRING = 3;
private static final int ESTADO_ACHOU_ABRE_PARENTESES = 4;
private static final int ESTADO_ACHOU_FECHA_PARENTESES = 5;
private static final int ESTADO_ACHOU_ASTERISCO = 6;
private static final int ESTADO_ACHOU_MAIS = 7;
private static final int ESTADO_ACHOU_VIRGULA = 8;
private static final int ESTADO_ACHOU_MENOS = 9;
private static final int ESTADO_BARRA = 10;
private static final int ESTADO_NUMERO_INTEIRO = 11;
private static final int ESTADO_ACHOU_PONTO_E_VIRGULA = 12;
private static final int ESTADO_MENOR_QUE = 13;
private static final int ESTADO_IGUAL = 14;
private static final int ESTADO_MAIOR_QUE = 15;
private static final int ESTADO_IDENTIFICADOR = 16;
private static final int ESTADO_ABRE_CHAVES = 17;
private static final int ESTADO_FECHA_CHAVES = 18;
private static final int ESTADO_ACHOU_STRING = 19;
private static final int ESTADO_COMENTARIO_BLOCO = 20;
private static final int ESTADO_NUMERO_PONTO_FLUTUANTE = 21;
private static final int ESTADO_ACHOU_MENOR_OU_IGUAL = 22;
private static final int ESTADO_ACHOU_DIFERENTE = 23;
private static final int ESTADO_ACHOU_IGUAL_IGUAL = 24;
private static final int ESTADO_ACHOU_MAIOR_OU_IGUAL = 25;
private static final int ESTADO_IDENTIFICADOR_COM_NUMEROS = 26;

private int nextState(char c, int state) {
    switch (state) {
        case ESTADO_INICIAL:
            switch (c) {
                case '\t':
                case '\n':
                case '\r':
                case ' ': return ESTADO_ESPACO;
                case '!': return ESTADO_ACHOU_EXCLAMACAO;
                case '\"': return ESTADO_STRING;
                case '(': return ESTADO_ACHOU_ABRE_PARENTESES;
                case ')': return ESTADO_ACHOU_FECHA_PARENTESES;
                case '*': return ESTADO_ACHOU_ASTERISCO;
                case '+': return ESTADO_ACHOU_MAIS;
                case ',': return ESTADO_ACHOU_VIRGULA;
                case '-': return ESTADO_ACHOU_MENOS;
                case '/': return ESTADO_BARRA;
                case ';': return ESTADO_ACHOU_PONTO_E_VIRGULA;
                case '<': return ESTADO_MENOR_QUE;
                case '=': return ESTADO_IGUAL;
                case '>': return ESTADO_MAIOR_QUE;
                case '{': return ESTADO_ABRE_CHAVES;
                case '}': return ESTADO_FECHA_CHAVES;
                default:
                    if (c >= '0' && c <= '9') return ESTADO_NUMERO_INTEIRO;
                    if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') return ESTADO_IDENTIFICADOR;
                    return ESTADO_ERRO;
            }
        case ESTADO_ESPACO:
            if (c == '\t' || c == '\n' || c ==  \r' || c == ' ') return ESTADO_ESPACO;
            return ESTADO_ERRO;
        case ESTADO_STRING:
            if (c == '\"') return ESTADO_ACHOU_STRING;
            if (c == '\t' || c == '\n' || c == '\r' || (c >= ' ' && c <= '~') || (c >= 160 && c <= 255) return ESTADO_STRING;
            return ESTADO_ERRO;
        case ESTADO_BARRA:
            if (c == '*') return ESTADO_COMENTARIO_BLOCO;
            return ESTADO_ERRO;
        case ESTADO_NUMERO_INTEIRO:
            if (c == '.') return 21;
            if (c >= '0' && c <= '9') return ESTADO_NUMERO_INTEIRO;
            return ESTADO_ERRO;
        case ESTADO_MENOR_QUE:
            if (c == '=') return ESTADO_ACHOU_MENOR_OU_IGUAL;
            id (c == '>') return ESTADO_ACHOU_DIFERENTE;
            return ESTADO_ERRO;
        case ESTADO_IGUAL:
            if (c == '=') return ESTADO_ACHOU_IGUAL_IGUAL;
            return ESTADO_ERRO;
        case ESTADO_MAIOR_QUE:
            if (c == '=') return ESTADO_ACHOU_MAIOR_OU_IGUAL;
            return ESTADO_ERRO;
        case ESTADO_IDENTIFICADOR:
            if (c >= '0' && c <= '9') return ESTADO_IDENTIFICADOR_COM_NUMEROS;
            if ((c >= 'A' && c <= 'Z') || c == '_' || (c >= 'a' && c <= 'z') return ESTADO_IDENTIFICADOR;
            return ESTADO_ERRO;
        default:
            return ESTADO_ERRO;
    }
}

Note that this still seems to be incomplete because comments started with /* do not end, numbers that find the decimal point do not continue to be parsed, and also do not continue to parse identifiers that have a number in the middle.

Note that this is nothing more than a way to implement the table of the image you gave, where char c is the column and int state is the row. What this function does is look at that table to answer the following question:

  

From a given state when a given character is in the input, what should be the next state?

The -1 seems (but I'm not sure) to indicate both the error state and the state that tells the lex analyzer to stop producing a token in response to the parser, and then restart (in state 0) to search of the next token.

The characters from 32 (space) to 126 (til) and from 160 to 255 of state 3 are characters that can appear within a string, and correspond to the printable characters of the ASCII table. The way the strings are treated in your table does not allow escape sequences for " and no Unicode characters (Russian, Greek, Chinese, Japanese, Arabic, Indian, accented characters, mathematical symbols, emoji, etc.) / p>     

25.06.2017 / 06:14