0% found this document useful (0 votes)
8 views1 page

Context-Free Grammars in Java

This document provides instructions for a CS2001 tutorial on grammars. It asks students to define context-free grammars for Java try blocks and interfaces, describe sentences generated by a sample grammar, and draw a parse tree for a sentence from that grammar. The tutorial focuses on context-free grammars, their properties, and applying them to programming language syntax.

Uploaded by

Abhishek Gambhir
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Context-Free Grammars in Java

This document provides instructions for a CS2001 tutorial on grammars. It asks students to define context-free grammars for Java try blocks and interfaces, describe sentences generated by a sample grammar, and draw a parse tree for a sentence from that grammar. The tutorial focuses on context-free grammars, their properties, and applying them to programming language syntax.

Uploaded by

Abhishek Gambhir
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

You might also like