WA2-solutions

WA2-solutions - CS 143 Compilers Handout 7 Solutions to...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Handout 7 Solutions to Written Assignment 2 1. Give a context-free grammar (CFG) for each of the following languages over the alphabet Σ = { 0 , 1 } : (a) All strings in the language L : { 0 i 1 j | i j 2 i i 0 } S 0 S 1 | 0 S 11 | ε (b) All strings with an equal number of 0’s and 1’s. S ε | 0 S 1 | 1 S 0 | SS (c) All strings with more 1’s than 0’s. S A 1 | MS | SMA A A 1 | ε M ε | MM | 0 M 1 | 1 M 0 2. Consider the following CFG. S AED | F A Aa | a B Bb | b C Cc | c D Dd | d E bEc | bc F aFd | BC (a) What is the language generated by this grammar? { a i b j c k d i | ( i 0) ( j,k > 0) } ∪ { a i b j c j d k | i,j,k > 0 } (b) Show that this grammar is ambiguous by giving a string that can be parsed in two different ways. Draw both parse trees. The string abcd has the two parse trees shown in Figure 1. (c) Give an unambiguous grammar that generates the same language as the grammar above. S
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

WA2-solutions - CS 143 Compilers Handout 7 Solutions to...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online