GithubHelp home page GithubHelp logo

fool2fish / dragon-book-exercise-answers Goto Github PK

View Code? Open in Web Editor NEW
6.2K 308.0 1.8K 1.56 MB

Compilers Principles, Techniques, & Tools (purple dragon book) second edition exercise answers. 编译原理(紫龙书)第2版习题答案。

C 19.87% JavaScript 7.80% Java 12.72% Lex 11.63% HTML 47.98%

dragon-book-exercise-answers's Introduction

Compilers Principles, Techniques, & Tools (purple dragon book) second edition exercise answers

编译原理(紫龙书)中文第2版习题答案

Join the chat at https://gitter.im/fool2fish/dragon-book-exercise-answers

Something I hope you know before go into the answers

  • First, please watch or star this repo, I'll be more happy if you follow me.
  • Bug report, questions and discussion are welcome, you can post an issue or pull a request.
  • All graphs are painted by yed, it is simple, cross-platform and free.
  • There are some key-point.md file, key points and difficult points are summarized in them.
  • As we know only teacher can buy the answer book, so I don't know the standard answer, but I tried my best to keep the correctness, if you found any bug, please tell me, thanks.

License

It is still under consideration. Anyone know which is suitable?

dragon-book-exercise-answers's People

Contributors

a596620989 avatar dxhj avatar eliasdiem avatar filipegoncalves avatar fool2fish avatar gangfan avatar gitter-badger avatar jcachuryb avatar kingalen avatar mustekito avatar rednaxelafx avatar sige avatar zornlemma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dragon-book-exercise-answers's Issues

mistake in 7.4.1

There's a mistake in the answer for first fit for 7.4.1.
The 48 byte object should fit at address 0.

8.5.2 part(b)

The last line should be

a = e - b

You've written

e = e - b

2.6.2 treats '!' and '=' as valid relational operators


{// 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 != '='.

2.2.2(5)

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)

3.7.2 (1) right~?

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}

关于2.4节第3)小题

S -> 0 S 1 | 0 1 其中0 1应该是两个终结字符,怎么能用 case "01" 来判断呢?
改成 case '1':break; 你看如何?

2.6 节的练习 中关于注释解析部分的改进

当扫描到单个 '/',后面不跟 '/' 或 '*' 时抛异常,而需要根据语法判断这种情况下是不是把 '/' 作为一个新的 token 返回

这个可以用PushbackReader类解决,先读出一个next,如果peek + next 不是 // ,就可以将next退回输入。

4.3.3问题疑问

我举不出来表明文法二义性的例子。但是目前答案里的例子好像有问题,因为stmt的产生式并没有else。

4.4.5 File not found

Aho: Properties of Context-Free Languages is not available. The link is broken.

Exercise 4.4.1 d)

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,),$}

Exercise 4.6.2

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

Exercise 3.4.1 (2)

写出正则表达式 ((ε|a)b_)_ 的状态转换图,觉得状态1应该有双层圈,因为该状态1也算一种最终态。

2.3.2 translation schemes

是不是应该这样:
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
最后,谢谢你的答案

3.3.5-3

What about comments in quote?

Solution Not Correct Exercise 6.4.1

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)

Gitbook

I have just forked your project and made a gitbook for it.

gitbook

I don't know whether it will be helpful for you. ^.^

4.4.4(3) Answer Issue?

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?

spelling error

2.3.1

trans­ lates -> trans­lates

中间多了一个空格,我看了下英文版,原因是trans后换行。估计复制时候出现的问题。

^_^

Q 4.4.1 f)

I think the table is wrong. The bterm' - and column should include the production bterm' --> and bfactor bterm'

是 Tools 而不是 Toos

在 README.md 和 description 里都把书名 Compilers Principles, Techniques, & Tools 中的 Tools 写成了 Toos

关于一些答案的疑问

首先赞一个,有热情总结答案,龙书的答案真是找半天都没有,本人也在自学龙书,刚看到2.2,4,1)中原题是后缀表达式,2.2.2 5)会不会是以a为变量,+,连接,×,括号的运算表达式集合。

new answers

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*

4.3.2(b)

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.

A little problem

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

spelling error

In the file README.md:

It is still under consideration. Anyone know witch is suitable?

witch -> which

^_^

Showing token output

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..

4.2.3 a

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)* ?

Roman Numeral grammar in 2.2.md

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

5.3.2!

第6个语法规则中有 :

F.expr = "(" || E.expr || ")" 
F.cleanExpr = E.expr

应该是

F.expr = "(" || E.cleanExpr || ")"
F.cleanExpr = E.cleanExpr

不然,比如 ((a+b)),结果是(a+b) 应该是 a+b

习题3.3.5第九小问

All strings of a 's and b's that do not contain the subsequence abb

感觉可以用 b_a_b?a*来表示

More English translation?

Only a handful of chapters are available in English currently. Are you accepting translation pull requests?
愿意接受更多章节英语翻译的 pull request 么

格式错误

  1. 6.2小节 6.2.2第2题间接三元式多了一个空格,导致格式出现了问题。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.