fool2fish / dragon-book-exercise-answers Goto Github PK
View Code? Open in Web Editor NEWCompilers Principles, Techniques, & Tools (purple dragon book) second edition exercise answers. 编译原理(紫龙书)第2版习题答案。
Compilers Principles, Techniques, & Tools (purple dragon book) second edition exercise answers. 编译原理(紫龙书)第2版习题答案。
第6个语法规则中有 :
F.expr = "(" || E.expr || ")"
F.cleanExpr = E.expr
应该是
F.expr = "(" || E.cleanExpr || ")"
F.cleanExpr = E.cleanExpr
不然,比如 ((a+b)),结果是(a+b) 应该是 a+b
What about comments in quote?
Only a handful of chapters are available in English currently. Are you accepting translation pull requests?
愿意接受更多章节英语翻译的 pull request 么
Should be {$, a, +, *} instead of {a, +, *}
Hello fool2fish,
Thanks for doing this project!
One tricky thing about Roman numerals is that they don't have a way to represent zero (at least, not until later in Roman times, and to my knowledge it was never standardized). The grammar in 2.2.md, as written, could conceivably result in an empty string, but then this wouldn't truly be a Roman "numeral". I suggest changing the following productions for the nonterminals romanNum, digit, and smallDigit:
romanNum -> thousand hundred ten digit | thousand hundred ten
digit -> smallDigit | IV | V | V smallDigit | IX
smallDigit -> I | II | III
-- A.M. Perry
当扫描到单个 '/',后面不跟 '/' 或 '*' 时抛异常,而需要根据语法判断这种情况下是不是把 '/' 作为一个新的 token 返回
这个可以用PushbackReader类解决,先读出一个next,如果peek + next 不是 // ,就可以将next退回输入。
There are no answers to problem 3.5.2,5.2.2(2),5.5.5 and 6.7.1(2,3),and I need them.please send an email to [email protected],thanks.
在 README.md 和 description 里都把书名 Compilers Principles, Techniques, & Tools 中的 Tools 写成了 Toos。
It would be great if this had an english version
題目的要求是:
Write regular definitions for the following languages:
而你寫的是 regular expression
All strings of a 's and b's that do not contain the subsequence abb
感觉可以用 b_a_b?a*来表示
https://github.com/fool2fish/dragon-book-exercise-answers/blob/master/ch08/8.3/8.3.md#833
In the link above, at addresses 108,132,140 and 164 you have used psize and qsize which I think is incorrect. It must be the size of the calling procedure not the size of the called procedure.
Please correct me if I am wrong or else correct the answer.
Thank You
in our class we have some changes in the Answers
in 3.3.5.
the fourth part
p --> [012] | 0 [12] | 1 [02] | 2 [01] | 012 | 021 | 102 | 12 | 201 | 210
and for 3.3.4
select --> [Ss] [Ee] [Ll] [Ee] [Cc] [Tt]
letter --> [Aa] | [Bb] .....[Zz]
id --> letter*
我举不出来表明文法二义性的例子。但是目前答案里的例子好像有问题,因为stmt的产生式并没有else。
{// handle relation sign
if("<=!>".indexOf(peek) > -1){
StringBuffer b = new StringBuffer();
b.append(peek);
peek = (char)stream.read();
if(peek == '='){
b.append(peek);
}
return new Rel(b.toString());
}
}
Should the original string be:
3 ! 2 = 1
'!' and '-' will be recognised by Rel, as it enters this if block, and
peek = (char)stream.read() would put peek = ' ', therefore new Rel("!") and new Rel("=") is to be returned.
So a simple solution may be to check after the if(peek == '=') block what b is like, and throw error if the b is now "!" or "=", and peek != '='.
Aho: Properties of Context-Free Languages is not available. The link is broken.
I am very grateful to u for having provided the solutions of the exercises. I created an account to thank you. I would like to point out an error in the section mentioned above. The second solution is incomplete and a part of the same is in the 3rd answer. Kindly look into it. Thnx again
a) The set of all strings of Os and Is such that every 0 is immediately followed
by at least one 1.
这个答案为什么是(0_1)_? 每个0后面都要跟着至少一个1的话不是说(0?1)* ?
In the problem 3.7.1.2, state D should be {0, 1, 2, 3}.
sorry for my poor english.
我想第5个应该是:数字乘以括号内的项,可以省略乘号。
例如: 5*(5+8) == 5(5+8)
S -> a | S + S | S S | S * | ( S )
I think the language is L = {when digit minus brackets's term, can omit minus signs }
for example:
5*(5+8) == 5(5+8)
The solution of 6.4.1 part b) given in the repo is wrong
Your Answer
E -> E1 * E2 { E.addr = new Temp();
E.code = E1.code || E2.code ||
gen(E.addr '=' E1.addr '*' E2.addr); }
| +E1 { E.addr = E1.addr;
E.code = E1.code; }
But if you refer the book ( pg 381)
The translation should be
E -> E1 * E2 { E.addr = new Temp();
E.code = E1.code || E2.code ||
gen(E.addr '=' E1.addr '*' E2.addr); }
| +E1 { E.addr = new Temp();
E.code = E1.code || gen(E.addr '=''plus'E1.addr) ; }
The same goes for 6.4.2 (the incremental one)
The follow(S) should be [+,,$] instead of [+,,a]
I think first(S) should be only ε alone. '(' is not part of first(S) because it's not the first symbol in any of the production bodies. Is it an error?
In the file README.md:
It is still under consideration. Anyone know witch is suitable?
witch -> which
^_^
I think the table is wrong. The bterm' - and column should include the production bterm' --> and bfactor bterm'
The production S -> S + S | S S | (S) | S * | a, after eliminate left recursion you got
S -> TB
B -> AB | ε
A -> +S | TB | * <--- TB should replace with S
T -> (S) | a
so it turns to be
S -> TB
B -> AB | ε
A -> +S | S | *
T -> (S) | a
And the follow set should be
FOLLOW(S) = {),$}
FOLLOW(B) = {),$}
FOLLOW(A) = {+,*,(,a,),$}
FOLLOW(T) = {+,*,(,a,),$}
首先赞一个,有热情总结答案,龙书的答案真是找半天都没有,本人也在自学龙书,刚看到2.2,4,1)中原题是后缀表达式,2.2.2 5)会不会是以a为变量,+,连接,×,括号的运算表达式集合。
The last line should be
a = e - b
You've written
e = e - b
So, I wrote a main class to implement the lexer class and fed a fileinputstream into the lexer constructor. everything works fine, but i am still trying to figure out how to get to print the tokens. if anybody has implemented this, kindly suggest a way. thanks. PS, I have to submit this assignment on tuesday..
comments write like:
// hello
// world
/* hello */
/* world */
token
May not be parse correctly.
https://github.com/fool2fish/dragon-book-exercise-answers/blob/master/ch02/2.6/2.6.md#answer
S -> 0 S 1 | 0 1 其中0 1应该是两个终结字符,怎么能用 case "01" 来判断呢?
改成 case '1':break; 你看如何?
Hello,
I have found that in 4.3.2(b) solution, you suggested that there is an indirect left recursion in:
S -> 0 A
A -> S 1 | 1
I don't understand.
Can you please check this and let us know if it is right or wrong ?
Thank you.
With productions
0) S' -> S
1) S -> a B
2) B -> a B A B
3) B -> ε
4) A -> +
5) A -> *
you got
FOLLOW(S) = [$]
FOLLOW(B) = [+, * ,$]
FOLLOW(A) = [a, $]
I think this is wrong. since we have
B -> a B A B
B -> ε
according to the third rules, B can be ε, so every element in Follow(B) is in Follow(A). so Follow(A) should be
FOLLOW(A) = [a, +, *, $]
A simple derivation can explain it:
S -> a B -> a a B A B -> a a a B A B A B -> a a a B A A B ( here the middle B goes B -> ε)
here * and + can be symbols follow A
There's a mistake in the answer for first fit for 7.4.1.
The 48 byte object should fit at address 0.
2.3.1
trans lates -> translates
中间多了一个空格,我看了下英文版,原因是trans后换行。估计复制时候出现的问题。
^_^
写出正则表达式 ((ε|a)b_)_ 的状态转换图,觉得状态1应该有双层圈,因为该状态1也算一种最终态。
The answer is -start->{0}-a->{0,1}-a->{0,1,2}-b->{0,2,3}-b->{0,2,3}
, and my solution is -start->{0}-a->{0,1}-a->{0,1,2}-b->{0,1,2,3}-b->{0,1,2,3}
Hi @fool2fish thx for your sharing. That's really helpful.
When I tried 2.4.1.1.c
https://github.com/fool2fish/dragon-book-exercise-answers/blob/master/ch02/2.4/2.4.1.1.c,
I found that when something like "aaa" or ''aaaaa'' was input, no "syntax error" was printed. I fixed this problem in C++:
https://github.com/Mooophy/dragon-book-2nd/blob/master/ch02/2.4/ex2_4_1a.cpp
I've little experience on C, so just make this report instead of pull request.
Good luck~
是不是应该这样:
expr -> {print("(")}expr {print(")+(")} expr {print(")")} +
| {print("(")}expr {print(")-(")} expr {print(")")} -
| {print("(")} expr {print(")*(")} expr {print(")")} *
| {print("(")} expr {print(")/(")} expr {print(")")} /
| digit {print(digit)}
因为 - + 那2行不加括号的话:
321+- => 3-2+1
0 => 2
最后,谢谢你的答案
I have just forked your project and made a gitbook for it.
I don't know whether it will be helpful for you. ^.^
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.