CS374: Programming Language Principles - Grammars

Activity Goals

The goals of this activity are:
  1. To describe a grammar in terms of tokens in BNF form

Supplemental Reading

Feel free to visit these resources for supplemental background reading material.

The Activity


Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.

Model 1: Chomsky Hierarchy of Languages

Grammar Languages Automaton Production rules (constraints)* Examples
Type-0 Recursively enumerable Turing machine {\displaystyle \gamma \rightarrow \alpha }
(no constraints)
{\displaystyle L=\{w|w}
describes a terminating Turing machine
Type-1 Context-sensitive Linear-bounded non-deterministic Turing machine \alpha A \beta \rightarrow \alpha \gamma \beta {\displaystyle L=\{a^{n}b^{n}c^{n}|n>0\}}
Type-2 Context-free Non-deterministic pushdown automaton {\displaystyle A\rightarrow \alpha } {\displaystyle L=\{a^{n}b^{n}|n>0\}}
Type-3 Regular Finite state automaton {\displaystyle A\rightarrow {\text{a}}}
{\displaystyle A\rightarrow {\text{a}}B}
{\displaystyle L=\{a^{n}|n\geq 0\}}


  1. What kind of language and machine would be used to balance parenthesis, and why?
  2. Why can't the context-free language be expressed with a regular language or finite state machine?
  3. What do you think is added to a finite state machine to create a push-down automata?

Model 2: Grammar Definition


  1. What does this language produce?
  2. How can this be modified to specify a language that accepts Strings of n a characters followed by n b characters?
  3. How might we parse a grammar? How can we make it easy to parse a grammar? For example, would it help if every production began with a unique terminal?
  4. Modify the above grammar to match all sets of balanced parenthesis, for example: (()) but not ()).


I encourage you to submit your answers to the questions (and ask your own questions!) using the Class Activity Questions discussion board. You may also respond to questions or comments made by others, or ask follow-up questions there. Answer any reflective prompt questions in the Reflective Journal section of your OneNote Classroom personal section. You can find the link to the class notebook on the syllabus.