CS2001 week 5 tutorial:
Grammars
1. What is a context-free grammar? Explain what we mean by context in this context, and why
a context-free grammar is free of it.
2. Design a context-free grammar for recognising Java try blocks. You may assume that we
have already defined terminals TRY and EXCEPT, as well as a non-terminal for block that
recognises statements in curly brackets. Make any other assumptions you need.
3. Design a context-free grammar that describes a Java interface, making any simplifying
assumptions you need. Try to make sure the result isn't ambiguous.
4. Describe the sentences induced by the following grammar, and give some examples. Is the
grammar ambiguous? Does it generate only a finite set of strings? Why (not)?
1. X → A N1 V A N2
2. N1 → cat | dog | bird | frog
3. N2 → mat | table | stove
4. A → a | the
5. V → sat S | jumped D | jumped S | spun S
6. S → on
7. D → onto
5. Use the grammar from 4 to show both the derivation and the parse tree for a sample
sentence.