100% found this document useful (1 vote)
345 views5 pages

2 2

chap 2 solution excercise 2.2

Uploaded by

Gaurav Pitaliya
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
100% found this document useful (1 vote)
345 views5 pages

2 2

chap 2 solution excercise 2.2

Uploaded by

Gaurav Pitaliya
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
You are on page 1/ 5

fool2fish 11 days ago Simplify the proof of 2.2.5-2.

2 contributors
Sign up Sign in
197 99 Star Fork
fool2fish / dragon-book-exercise-answers
PUBLIC
master
dragon-book-exercise-answers / ch02 / 2.2 / 2.2.md
file file 181 lines (109 sloc) 181 lines (109 sloc) 5.565 kb 5.565 kb
Consider the context-free grammar:
S -> S S + | S S * | a
Show how the string aa+a* can be generated by this grammar. 1.
Construct a parse tree for this string. 2.
What language does this grammar generate? Justify your answer. 3.
S -> S S * -> S S + S * -> a S + S * -> a a + S * -> a a + a * 1.
2.
L = {Postfix expression consisting of digits, plus and multiple signs} 3.
What language is generated by the following grammars? In each case justify your answer.
S -> 0 S 1 | 0 1 1.
S -> + S S | - S S | a 2.
S -> S ( S ) S | 3.
S -> a S b S | b S a S | 4.
S -> a | S + S | S S | S * | ( S ) 5.
L = {0 1 | n>=1} 1.
L = {Prefix expression consisting of plus and minus signs} 2.
L = {Matched brackets of arbitrary arrangement and nesting, includes } 3.
L = {String has the same amount of a and b, includes } 4.
5.
Open Delete
2.2 Exercises for Section 2.2
2.2.1
answer
2.2.2
answer
n n
Explore Explore Features Features Enterprise Enterprise Blog Blog This repository
dragon-book-exercise-answers/ch02/2.2/2.2.md at master fool2fish/dra... https://github.com/fool2fish/dragon-book-exercise-answers
1 of 5 11-May-14 4:38 PM
Which of the grammars in Exercise 2.2.2 are ambiguous
No 1.
No 2.
Yes 3.
Yes 4.
Yes 5.
Construct unambiguous context-free grammars for each of the following languages. In each case show that your grammar is
correct.
2.2.3
answer
2.2.4
dragon-book-exercise-answers/ch02/2.2/2.2.md at master fool2fish/dra... https://github.com/fool2fish/dragon-book-exercise-answers
2 of 5 11-May-14 4:38 PM
Arithmetic expressions in postfix notation. 1.
Left-associative lists of identifiers separated by commas. 2.
Right-associative lists of identifiers separated by commas. 3.
Arithmetic expressions of integers and identifiers with the four binary operators +, - , *, /. 4.
1. E -> E E op | num
2. list -> list , id | id
3. list -> id , list | id
4. expr -> expr + term | expr - term | term
term -> term * factor | term / factor | factor
factor -> id | num | expr!
". expr -> expr + term | expr - term | term
term -> term * unar# | term / unar# | unar#
unar# -> + factor | - factor | factor
factor - > id | num | expr!
Show that all binary strings generated by the following grammar have values divisible by 3. Hint. Use induction on the
number of nodes in a parse tree.
num -> 11 | 1001 | num 0 | num num
1.
Does the grammar generate all binary strings with values divisible by 3? 2.
proof
any string derived from the grammar can be considered to be a sequence consisting of 11 and 1001, where each sequence
element is possibly suffixed with a 0.
let n be the set of positions where 11 is placed. 11 is said to be at position i if the first 1 in 11 is at position i , where
i starts at 0 and grows from least significant to most significant bit.
let m be the equivalent set for 1$$1 .
the sum of any string produced by the grammar is:
sum
= (2 + 2 ) * 2 + (2 + 2 ) * 2
= 3 * 2 + 9 * 2
This is clearly divisible by 3.
1.
No. Consider string "10101", it is divisible by 3, but cannot derived from the grammar.
Readers seeking a more formal proof can read about it below:
Proof:
Every number divisible by 3 can be written in the form 3% . We will consider % > $ (though it would be valid to consider % to
be an arbitrary integer).
2.
answer
2.2.5
answer
n
1 0 n
m
3 0 m
n
n
m
m
dragon-book-exercise-answers/ch02/2.2/2.2.md at master fool2fish/dra... https://github.com/fool2fish/dragon-book-exercise-answers
3 of 5 11-May-14 4:38 PM
Note that every part of num(11, 1001 and 0) is divisible by 3, if the grammar could generate all the numbers divisible by 3,
we can get a production for binary k from num's production:
3% & num -> 11 | 1$$1 | num $ | num num
% & num/3 -> $1 | $$11 | % $ | % %
% -> $1 | $$11 | % $ | % %
It is obvious that any value of % that has more than 2 consecutive bits set to 1 can never be produced. This can be
confirmed by the example given in the beginning:
10101 is 3*7, hence, k = 7 = 111 in binary. Because 111 has more than 2 consecutive 1's in binary, the grammar will never
produce 21
Construct a context-free grammar for roman numerals.
Note: we just consider a subset of roman numerals which is less than 4k.
wikipedia: Roman_numerals
via wikipedia, we can categorize the single roman numerals into 4 groups:
', '', ''' | ' ( | (, ( ', ( '', ( ''' | ' )
then get the production:
di*it -> small+i*it | ' ( | ( small+i*it | ' )
small+i*it -> ' | '' | ''' | ,
and we can find a simple way to map roman to arabic numerals. For example:
XII => X, II => 10 + 2 => 12
CXCIX => C, XC, IX => 100 + 90 + 9 => 199
MDCCCLXXX => M, DCCC, LXXX => 1000 + 800 + 80 => 1880
via the upper two rules, we can derive the production:
romanNum -> thousand hundred ten digit
thousand -> M | MM | MMM |
hundred -> smallHundred | C D | D smallHundred | C M
smallHundred -> C | CC | CCC |
ten -> smallTen | X L | L smallTen | X C
smallTen -> X | XX | XXX |
digit -> smallDigit | I V | V smallDigit | I X
smallDigit -> I | II | III |
2.2.6
answer
dragon-book-exercise-answers/ch02/2.2/2.2.md at master fool2fish/dra... https://github.com/fool2fish/dragon-book-exercise-answers
4 of 5 11-May-14 4:38 PM
Status API Training Shop Blog About 2014 GitHub, Inc. Terms Privacy Security Contact
dragon-book-exercise-answers/ch02/2.2/2.2.md at master fool2fish/dra... https://github.com/fool2fish/dragon-book-exercise-answers
5 of 5 11-May-14 4:38 PM

You might also like