Computer Science 301 - 2002


Tutorial for week 10 - more practice in checking LL(1) compliance

You can find the source for these grammars on the WWW page in the file TUT10.ZIP.

Are the following grammars LL(1) compliant? If not, why not?

    COMPILER A $TF
      PRODUCTIONS
        A = B ( "x" | "z" ) ( "w" | "z" ) B .
        B = "x" B "z" | { "y" } .
    END A.


   COMPILER S $FT
     PRODUCTIONS
       S = A | B .
       A = C D | E "f" .
       B = "d" | "e" .
       C = [ "a" | "b" ] .
       D = [ "c" | "e" ] .
       E = "f" | "g" .
   END S.


   COMPILER Z $TF
     PRODUCTIONS
       Z = B C | D E .
       B = [ E ] .
       C = "x" E .
       D = E { Z } ( E | "w" ) .
       E = "y" | "z" .

   END Z.

The following describes a silly computer language. Study it carefully and decide whether it is LL(1) compliant. If not, explain why, and make suggestions as to how it might be slightly modified in that case so that a recursive descent parser could be constructed for it.

   COMPILER Block $TF

     IGNORE CHR(1) .. CHR(31)

     CHARACTERS
       letter     = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" .

     TOKENS
       identifier = letter { letter } .

     PRODUCTIONS
       Block             = "BEGIN" StatementSequence "END" .
       StatementSequence = Statement { ";" Statement } .
       Statement         = [   Block | Declaration | Assignment
                             | WhileStatement | DoStatement | IfStatement ].
       Declaration       = identifier { "," identifier } ":" Type .
       IfStatement       = "IF" Expression
                             [ "THEN" ] StatementSequence
                             [ "ELSE" StatementSequence ] .
       Expression        = identifier . /* for illustration only */
       WhileStatement    = "WHILE" Expression "DO" StatementSequence "END" .
       DoStatement       = "DO" StatementSequence "WHILE" Expression  .
       Assignment        = identifier ":=" Expression .
       Type              = "INTEGER" | "BOOLEAN" | "CHAR" .
   END Block.

Would it perhaps have been wiser to try to write the grammar with the following alternative for IfStatement?

       IfStatement = "IF" Expression
                     ( Statement | "THEN" Statement "ELSE" Statement ) .


Home  © P.D. Terry