Docsity

Written Assignment 2 - Programming Languages | CS 4610, Assignments of Programming Languages

Toggle sidebar
bg1
Written Assignment 2
This assignment asks you to prepare written answers to questions on
context-free grammars. Each of the questions has a short answer. You
may discuss this assignment with other students and work on the problems
together. However, your write-up should be your own individual work.
Please print your name and email address on your homework!
We need this information so that we can give you credit for the assignment
and so that we can return it to you.
1. Let L1be the language consisting of all non-empty palindromes over
the alphabet ฮฃ = {a, b}. That is, L1consists of all sequences of aโ€™s and
bโ€™s that read the same forward or backward. For example, aba โˆˆL1
and and bb โˆˆL1and aabbbaa โˆˆL1, but abb /โˆˆL1.
Let L2be the language over ฮฃ = {a, b}denotated by the regular ex-
pression a(a|b)โˆ—.
The language L3=L1โˆฉL2is context-free. A string sis in L3if sโˆˆL1
and sโˆˆL2. Write a context-free grammar for the language L3.
Optional Thing To Think About: Is the intersection of a context-
free language and a regular language always context-free?
2. Consider the following grammar:
Sโ†’aSb
Sโ†’Sb
Sโ†’๎˜
(a) Give a one-sentence description of the language generated by this
grammar.
(b) Show that this grammar is ambiguous by giving a single string
that can be parsed in two different ways. Draw both parse trees.
(c) Give an unambiguous grammar that accepts the same language
as the grammar above.
1
pf2

Partial preview of the text

Download Written Assignment 2 - Programming Languages | CS 4610 and more Assignments Programming Languages in PDF only on Docsity!

Written Assignment 2

This assignment asks you to prepare written answers to questions on context-free grammars. Each of the questions has a short answer. You may discuss this assignment with other students and work on the problems together. However, your write-up should be your own individual work.

Please print your name and email address on your homework! We need this information so that we can give you credit for the assignment and so that we can return it to you.

  1. Let L 1 be the language consisting of all non-empty palindromes over the alphabet ฮฃ = {a, b}. That is, L 1 consists of all sequences of aโ€™s and bโ€™s that read the same forward or backward. For example, aba โˆˆ L 1 and and bb โˆˆ L 1 and aabbbaa โˆˆ L 1 , but abb /โˆˆ L 1. Let L 2 be the language over ฮฃ = {a, b} denotated by the regular ex- pression a(a|b)โˆ—. The language L 3 = L 1 โˆฉ L 2 is context-free. A string s is in L 3 if s โˆˆ L 1 and s โˆˆ L 2. Write a context-free grammar for the language L 3. Optional Thing To Think About: Is the intersection of a context- free language and a regular language always context-free?
  2. Consider the following grammar:

S โ†’ aSb S โ†’ Sb S โ†’ 

(a) Give a one-sentence description of the language generated by this grammar. (b) Show that this grammar is ambiguous by giving a single string that can be parsed in two different ways. Draw both parse trees. (c) Give an unambiguous grammar that accepts the same language as the grammar above.

  1. Using the context-free grammar for Cool given in Section 11 of the Cool manual, draw a parse tree for the following expression.

while not (x <- z <- 0) loop y <- z + 2 * x + 1 pool

Note that the context-free grammar by itself is ambiguous, so you will need to use the precedence and associativity rules in Section 11.1 to get the correct tree.

  1. Give an example of a grammar that is LL(3) but not LL(2).