Dana Vrajitoru
C311 Programming Languages

Introduction to Formal Grammars

Formal Grammars

Formal Definition

Production rules

Example

Production Tree

; Lisp implementation of the grammar.
(defun check-S (Lst)
  (if (equal (car Lst) 'a) 
      (check-T (cdr Lst))
    nil))

(defun check-T (Lst)
  (if (equal (car Lst) 'b)
      (or (check-T (cdr Lst))
          (check-V (cdr Lst)))
    nil))

(defun check-V (Lst)
  (if (and (equal (car Lst) 'a))
       (not (cdr Lst))))

(check-S '(a b a)) 		; t
(check-S '(a b b b a)) 	        ; t
(check-S '(b a b)) 		; nil

Types of Grammars

Examples of Rules