Exercício 07.80

Desenvolva uma Gramática Livre do Contexto (GLC) que reconheça a instrução de controle switch-case.


Resposta com recursividade à esquerda

G = ({statement, declarator, declarator-int, declarator-int-list, declarator-float, declarator-float-list,
      statement-switch, statement-case, statement-break, assignment, expression, term, factor, operand,
      variable, constant-float, constant-int, number, signal, letter, digit}, {a, b, c, d, e, f, g, h, i,
      j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -, +, *, /, (, ), =,
      {, }, :, ;, ., ,,}, P, statement)
P = {< statement >  ->  < declarator >
                    |   < statement-switch >
     < declarator >  ->  < declarator >  < declarator-int >
                     |   < declarator >  < declarator-float >
                     |   ε
     < declarator-int >  ->  int < declarator-int-list > ;
     < declarator-int-list >  ->  < declarator-int-list > , < variable >
                              |   < declarator-int-list > , < variable > = < constant-integer >
                              |   < variable >
                              |   < variable > = < constant-integer > 
     < declarator-float >  ->  float < declarator-float-list > ;
     < declarator-float-list >  ->  < declarator-float-list > , < variable >
                                |   < declarator-float-list > , < variable > = < constant-float >
                                |   < variable >
                                |   < variable > = < constant-float >
     < statement-switch >  ->  switch ( < variable > ) { < statement-case > }
     < statement-case >  ->  < statement-case > case < constant-integer > : < statement-break >
                         |   case < constant-integer > : < statement-break >
                         |   default : < statement-break >
     < statement-break >  ->  < assignment > break ;
                          |   < assignment >
                          |   break ;
                          |   ε
     < assignment >  ->  < assignment > < variable > = < expression > ;
                     |   < variable > = < expression > ;
     < expression >  ->  < expression > + < term >
                     |   < term >
     < term >  ->  < term > * < factor >
               |   < factor >
     < factor >  ->  ( < expression > )
                 |   < operand >
     < operand >  ->  < variable >
                  |   < constant-float >
     < variable >  ->  < variable > < letter >
                   |   < variable > < digit >
                   |   < letter >
     < constant-float >  ->  < signal > . < number >
                         |   < constant-int > . < number >
                         |   < constant-int >
     < constant-int >  ->  < signal > < number >
     < number >  ->  < number > < digit >
                 |   < digit >
     < signal >  ->  +  |  -  |  ε
     < letter >  ->  a  |  b  |  c  |  d  |  e  |  f  |  g  |  h  |  i  |  j  |  k  |  l  |  m
                 |   n  |  o  |  p  |  q  |  r  |  s  |  t  |  u  |  v  |  w  |  x  |  y  |  z
     < digit >  ->  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  }

Recomendamos

Revista FOSSGIS Brasil Revista LibreOffice Magazine Revista Segurança Digital