Category Archives: Dragon book

Posts of thoughts & answers from The Dragon Book

Lexical Analysis: The Role of the Lexical Analyzer Section 3.1

This chapter was mostly just a recap of things that were already covered about lexers. 3.1.1: Divide the following C++ program into appropriate lexemes. Which lexemes should get associated lexical values? What should those values be? float limitedSquare(x) float x; … Continue reading

Posted in computer science, Dragon book | Leave a comment

Ending Chapter 2

Section 2.7 is about symbol tables and has no exercises. Basically, the technique they talk about is, as you’re parsing the source, you move symbol tables around. So you start out with a null symbol table, and each time you … Continue reading

Posted in computer science, Dragon book | Leave a comment

Exercises from 2.6 are up

All of the exercises are up on my github. Here’s a few things I’ve discovered: 1) Don’t try to be too smart. I’m using C# so fuck it, I’m gonna use int.Parse() and stream.Peek() 2) I’m using inheritance over composition … Continue reading

Posted in Dragon book | Leave a comment

Lexical Analysis (2.6)

Lexers! I’ve sort of written lexers before, although not of this generality, so this material is mostly familiar to me. The chapter provides a simple lexer (below), in Java. Old Java (pre-generics, so, what, 1.4 or someting?). I’m doing the … Continue reading

Posted in computer science, Dragon book | Leave a comment

Unit tests!

Added a few unit tests for the program used in 2.4.1. Check it out here! I’m using the new Visual Studio 2012 Express, which is nice so far – allows me to do unit testing, at least, and I have … Continue reading

Posted in Dragon book | Leave a comment

Syntax-Directed Translation, Parsing (2.3, 2.4)

So I skipped the section 2.3 exercises. They were asking for a bunch of translation schemes that were almost identical to the ones given in the chapter, which is boring. I was thinking about just writing code for them, but … Continue reading

Posted in Dragon book | Leave a comment

2.2 Grammars and Syntax

Consider the context-free grammarS -> S S + | S S * | a Show how the string aa+a* can be generated by this grammarS -> S S * -> S a * -> S S + a * -> … Continue reading

Posted in computer science, Dragon book | Leave a comment

Section 1.6 Exercises

Answers to this section have been verified with gcc on Cygwin. If you’re keeping score at home, I got the answers right before checking them. 1. For the following block of C code, indicate the values assigned to w, x, … Continue reading

Posted in Dragon book | 2 Comments

Introduction – 1.1

What is the difference between a compiler and an interpreter? A compiler outputs some sort of code, either intermediate representation (bytecode, etc) or pure machine code, which is then executed directly by the computer. An interpreter takes source code and … Continue reading

Posted in Dragon book | 1 Comment