Computer Science 301 - 2000


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

You can find the source for these grammars on the WWW page in the file TUT24.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 | IfStatement
                             | CaseStatement | Assignment ].
       Declaration       = identifier { "," identifier } ":" Type .
       IfStatement       = "IF" Expression [ "THEN" ] Statement [ "ELSE" Statement ] .
       Expression        = identifier . /* for illustration only */
       CaseStatement     = "CASE" Expression "OF" Case { "|" Case }
                           [ "ELSE" StatementSequence ] "END" .
       Case              = [ CaseLabelList ":" StatementSequence ] .
       CaseLabelList     = CaseLabels { "," CaseLabels } .
       CaseLabels        = ConstExpression [ ".." ConstExpression ] .
       ConstExpression   = Expression .
       Assignment        = identifier ":=" Expression .
       Type              = "INTEGER" | "BOOLEAN" | "CHAR" .
   END Block.

Would it have been wiser to try to write the grammar with either of the following alternatives for IfStatement?

       IfStatement = "IF" Expression [ "THEN" ] StatementSequence
                     [ "ELSE" StatementSequence ] .

or

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