Answers to Self-Study Questions

Test Yourself #1

   grammar: S -> epsilon | ( S ) | [ S ]

   parse table:
      
           (         )       [        ]       EOF
       +--------------------------------------------
     S | ( S ) | epsilon | [ S ] | epsilon | epsilon
       +--------------------------------------------


   input seen so far   stack         action
   -----------------   -----         ------
         [              S EOF       pop, push "[S]"
         [              [S] EOF     pop, scan (top-of-stack term matches curr token)
         [[             S] EOF      pop, push "[S]"
         [[             [S]] EOF    pop, scan (top-of-stack term matches curr token)
         [[]            S]] EOF     pop, push epsilon (no push)
         [[]            ]] EOF      pop, scan (top-of-stack term matches curr token)
         [[]]           ] EOF       pop, scan (top-of-stack term matches curr token)
         [[]] EOF       EOF         pop, scan (top-of-stack term matches curr token)
         [[]] EOF                   empty stack: input accepted!

Test Yourself #2


Test Yourself #3

Question 1:

X                                   First(X)                Follow(X)
---------------------------------------------------------------------
methodHeader                        VOID                    EOF

paramList                           epsilon, ID             RPAREN

nonEmptyParamList                   ID                      RPAREN

VOID ID LPAREN paramList RPAREN     VOID

epsilon                             epsilon

nonEmptyParamList                   ID

ID ID                               ID

ID ID COMMA nonEmptyParamList       ID

Question 2: Note: To save space, the tokens "(" and ")" are used instead of LPAREN and RPAREN.

input seen so far   stack                           action
-----------------   -----                           ------
 VOID               methodHeader EOF                pop,
                                                    push "VOID ID ( paramList )"

 VOID               VOID ID ( paramList ) EOF       pop, scan (top-of-stack term matches curr token)

 VOID ID            ID ( paramList ) EOF            pop, scan (top-of-stack term matches curr token)

 VOID ID (          ( paramList ) EOF               pop, scan (top-of-stack ter

 VOID ID ( )        paramList ) EOF                 pop,
                                                    push nothing; i.e.,
                                                    choose production paramList -> epsilon
                                                    because ) is in Follow(paramList)
                                                    
 VOID ID ( )        ) EOF                           pop, scan (top-of-stack term matches curr token)

 VOID ID ( ) EOF    EOF                             pop, scan (top-of-stack term matches curr token)

 VOID ID ( ) EOF                                    empty stack: input accepted!

Test Yourself #4

Test Yourself #5

Question 1(a):