Вы находитесь на странице: 1из 4

12/21/13 dragon-book-exercise-answers/ch02/2.2/2.2.

md at master fool2fish/dragon-book-exercise-answers GitHub


https://github.com/fool2fish/dragon-book-exercise-answers/blob/master/ch02/2.2/2.2.md 1/4


fool2fish 9 months ago Modify 2.2.2-4
1 Contributor

Sign up Sign in
105 57
Star y Fork
fool2fish / Dragon-book-exercise-answers
PUBLIC

Q
+
@
y
y master branch: Dragon-book-exercise-answers / ch02 / 2.2 / 2.2.md _
_ File 159 lines (93 sloc) 4.483 KB
Consider the following context-free grammar:
S -> SS + | SS * | a
1. Explain how to use the grammar generates the string aa + a *
2. Construct a test for this string parse tree
3. What language is generated by the grammar? Prove
1. S -> S S * -> S S + S * -> a S + S * -> aa + S * -> aa + a *
2.
3. The operation is as a number, L = {expression supports the addition and multiplication representation suffix}
What language is generated below each grammar? Every answer your proof
1. S -> 0 S 1 | 0 1
2. S -> + SS | - SS | a
3. S -> S (S) S |
4. S -> a S b S | b S a S |
5. S -> a | S + S | SS | S * | (S)
1. L = {0 1 | N> = 1}
2. L = {prefix expressions of support for the addition and subtraction of expressions}
3. L = {matching parentheses and nested parentheses arbitrary string arrangement, including }
4. L = {a and b are the same number of symbol string consisting include }
5. ?
g Open Edit Raw Blame History Delete
2.2 Exercise
2.2.1
Answer
2.2.2
Answer
N N
Explore Features Enterprise Blog Search or type a command This repository
12/21/13 dragon-book-exercise-answers/ch02/2.2/2.2.md at master fool2fish/dragon-book-exercise-answers GitHub
https://github.com/fool2fish/dragon-book-exercise-answers/blob/master/ch02/2.2/2.2.md 2/4
Previous question in which grammar is ambiguous
1. No
2. No
3. Have
4. Have
5. Have
Build unambiguous context for each language grammar below. Prove your grammar is correct.
1. Arithmetic expression represented by the suffix method
2.2.3
Answer
2.2.4
12/21/13 dragon-book-exercise-answers/ch02/2.2/2.2.md at master fool2fish/dragon-book-exercise-answers GitHub
https://github.com/fool2fish/dragon-book-exercise-answers/blob/master/ch02/2.2/2.2.md 3/4
2. Comma-separated list of identifiers left associative (identifier id said, the same below)
3. List of identifiers separated by commas right combination
4. There integer identifier, four binary operator +, -, *, / arithmetic expression composed
5. ! Increase Monocular Monocular + and the operator of a problem - consisting of arithmetic expressions
1. E -> EE 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)
5. Note: The highest priority subtraction of monocular
expr -> expr + term | expr - term | term
term -> term * unary | term / unary | unary
unary -> + factor | - factor
factor -> id | num | (expr)
1. Proof: The following grammar generating all the values of the binary string can be divisible by 3. (Hint: parse tree node tree
using mathematical induction)
num -> 11 | 1001 | num 0 | num num
2. Are the above grammar can generate all binary strings divisible by 3?
1. Proof
Comply with the leftmost bit binary string grammar must be composed of any number of 11,1001 and 0 is not zero sequence
The decimal sequence is:
sum
Sigma = (2 + 2 ) * 2 + sigma (2 + 2 ) * 2
Sigma = 3 2 * + sigma 2 * 9
Is clearly divisible by 3
2. Is not. 10101 binary string, a value of 21 is divisible by 3, but can not deduce from the grammar.
Note: There are more general law permits it?
Build a context-free grammar is the Roman numeral
Note: The grammar does not consider Roman numerals on the underlined, it can only produce a number less than 4k
Answer
2.2.5
Answer
N
1 0 N
m
3 0 m
N
N
m
m
2.2.6
12/21/13 dragon-book-exercise-answers/ch02/2.2/2.2.md at master fool2fish/dragon-book-exercise-answers GitHub
https://github.com/fool2fish/dragon-book-exercise-answers/blob/master/ch02/2.2/2.2.md 4/4
Rules Reference Wikipedia: Roman numerals
According to the rules described in the wiki can be found digit representation can be divided into four categories:
I, II, III | IV | V, VI, V II, V III | IX
Ie production:
digit -> smallDigit | IV | V smallDigit | IX
smallDigit -> I | II | III |
Other digital representation similar.
You can also find: Roman numerals and Arabic numerals have relatively simple digital correspondence.
For example:
XII - X, II - 10 + 2 - 12
CXCIX - C, XC, IX - 100 + 90 + 9 - 199
MDCCCLXXX - M, DCCC, LXXX - 1000 + 800 + 80 - 1880
According to these two laws deduced production:
romanNum -> thousand hundred ten digit
thousand -> M | MM | MMM |
hundred -> smallHundred | CD | D smallHundred | CM
smallHundred -> C | CC | CCC |
ten -> smallTen | XL | L smallTen | XC
smallTen -> X | XX | XXX |
digit -> smallDigit | IV | V smallDigit | IX
smallDigit -> I | II | III |
Answer
Status API Training Shop Blog About 2013 GitHub , Inc.. Terms Privacy Security Contact

Вам также может понравиться