Doubt with the Lexical and Syntactic Analyzer Generator (GALS) switch

Asked

Viewed 240 times

1

inserir a descrição da imagem aqui GALS generates the code in Java. I want to put it in the eclipse to make a screen and such. I was told that in the method nextState class lexico, have to put a switch with some numbers. 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;
    }
}
  • Gabriella, are you studying for some compiler book? I have the good new dragon book here, I can catch an explanation from there because it might be better than mine, but if I can talk about where you’re contextualized, I can try an answer within the context

  • 1

    So... This work was the teacher who passed... after doing in GALS, you have to generate the java code to create a small screen, and then the interface will show if there was a lexical or syntactic error. From the previous works I saw of the teacher, you had to put these numbers in the nextState method, but I don’t know where this number comes from. You know?

  • if I tell you that comes from the deterministic stack automaton that is interpreting the input, will I be speaking Greek or Aramaic? Or was this concept good enough for what you know? I would like to give a deep answer to that, I love formal languages, but I’m not available right now

  • boy and how do I get these numbers from the deterministic stack automaton?

1 answer

4


Note that the variable c is the type char. So if we use characters instead of numbers according to the codes of ascii table, 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 text characters that the lexical parser is processing.

The numbers 1, 2, 3 and 4 you have in returns are states of your lexical analyzer:

  • The analysis begins at state 0.

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

  • When finding an exclamation mark, it enters state 2. I don’t know what that means in its language, but state 2 may indicate that this means only that the operator denoted by the exclamation mark has been found. It may also be that the exclamation mark indicates the beginning of a comment and state 2 would be within a comment. Check your grammar for an exclamation mark.

  • When finding a double quote, it enters state 3, which would probably indicate being inside a string.

  • When finding the parentheses, it goes to state 4, which probably means it is only the state that indicates that an opening parentheses has been found.

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;
    }
}

By observing these state 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, as comments started with /* do not end, the numbers that find the decimal point do not continue to be analyzed and also do not continue to be analyzed the identifiers that have a number in the middle.

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

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

The -1 appears (but I’m not sure) to indicate both the error state, and the state that tells the lexical parser to stop, produce a token as a response to the parser, and then restart (in state 0) in search of the next token.

Characters from 32 (space) to 126 (tilde) and from 160 to 255 of state 3 are characters that can appear within a string, and correspond to printable characters from the ASCII table. The way string is handled in your table does not allow escape sequences for the " and not Unicode characters (Russian, Greek, Chinese, Japanese, Arabic, Indian alphabet, accented characters, mathematical symbols, emojis, etc).

  • See, and these 'cases' how many should I put? where does this amount come from? is the amount of character I defined? and in each case, how many numbers do I put? where does this definition come from?

  • @Gabriella, this is a little more complex. See finite state machines. Unfortunately, compilers are not a simple subject; compilers are one of the final applications of the study of formal languages, automata theory, parsing, etc. These numbers are the states of the finite automaton, the states you get after transforming the regular expression into a nondeterministic automaton and then removing the ambiguous transactions and removing the epsilon transactions/lambda transactions.

  • but in GALS you can see this?

  • there I did the lexical and syntactic analysis

  • has something to do with this image that GALS generates?

  • @Gabriella Resposta edited.

  • Friend, I understood that part. But then when I played in the eclipse, the class ParserConstants generates a HUGE table. is greater than the allowed byte limit which is 65535. what to do in such cases?

  • the code for the Static initializer is exceeding the 65535 bytes limit

  • @Gabriella This is because in Java, no method can exceed the size of 65535 bytes when compiled. The only solutions I see are (1) abandon GALS, (2) manually rewrite the code generated by GALS as I did or (3) perform tricks and tricks in the resulting code because of this limitation. For class ParserConstants, a possible solution would be to divide it into several classes. I have a lot of very negative considerations about the approach chosen by GALS to use switches within switches, and use a ParserConstants, but that from there is something that would go far.

  • It’s a college job, it has to be done even at GALS... as it would be '' divide it into several classes'' ?

  • @Gabriella Put half constants in a class or interface ParserConstants2 and make ParserConstants extends ParserConstants2 or ParserConstants implements ParserConstants2. If that’s not enough, do the ParserConstants3, ParserConstants4, etc. So that’s a very ugly and disgusting gambiarra, but if you want to eliminate the gambiarras would probably have to eliminate the whole GALS.

  • I’ll try here, split

  • but when it’s time to call int[] cmd = PARSER_TABLE[state][token-1]; the code will return any parser_table? I had to create 5 parserconstants!

  • @Gabriella How about reading this tab from a file instead of having it directly in the source code?

  • how to do this?

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.