Computer Science 301 - 2007

Tutorial for week 21 - simple grammars

(a) One simple grammar for describing expressions is:

      Goal       = Expression .
      Expression = Term | Expression "-" Term .
      Term       = Factor | Term "*" Factor .
      Factor     = "a" | "b" | "c" | "d" .

Show the parse tree corresponding to the string a - b - c

(b) Another grammar is

      Goal       = Expression .
      Expression = Term | Term "-" Expression .
      Term       = Factor | Factor "*" Term .
      Factor     = "a" | "b" | "c" | "d" .

Show the parse tree corresponding to the string a - b - c and discuss the differences between this grammar and the last.

(c) Yet another grammar for describing expressions might be:

      Goal       = Expression .
      Expression = Term | Expression "*" Term .
      Term       = Factor | Term "-" Factor .
      Factor     = "a" | "b" | "c" | "d" .

Show the parse tree corresponding to the string a - b * c

(d) How does one find a grammar that will allow all four operators (+ - * and /) with the familiar precedence and associativity conventions, and will also allow for parentheses within expressions - so that one can derive strings like

(a - b) * (c + d) ?
(e) What grammar would we need if we wished to allow for leading (unary) operators, as in expressions like
- a + ( -b + ( + c / d ))
(f) Develop a Cocol grammar to describe a list of names of staff who have titles, first names and/or initials, surnames, and (usually) qualifications, for example

          Professor Peter Clayton, PhD .
          Professor P. D. Terry, MSc, PhD .
          George Clifford Wells, BSc(Hons), MSc, PhD .
          Greg G. Foster, PhD .
          Dr Karen Lee Bradshaw, PhD .
          Mrs Madeleine Wright, MSc .
          B. Cleo C. Biko, BCom, BEd .
          Dr Dameon Wagner, PhD .
          Ms Cheryl Fischer .


Home  © P.D. Terry