The content of this book is
WriteCompiler and interpreter. Emphasize"
WriteIt is because a large amount of code is written. If you want to learn how to write compilers, interpreters, interactive source code-level debugger, and integrated development environment (IDE) with GUI, this book is perfect for you. I will explain the details of all the code written in Java. This book is not about the theory implied by the compiler, But what textbooks do. If you want to learn more, it is not suitable for you. However, I hope that after you complete the code in this book, you will be impulsive to learn the corresponding basic theories.
The first version of this book uses C as the implementation language, the second version uses C ++, and the third version uses Java. Although I will retain the structure, concepts, and methods of earlier versions, this is a truly thorough rewrite.
What will you learn?
Through this book, you will learn how to write compilers and interpreters for processing programs in advanced languages. (1) You will write an interpreter that can execute the program. (2) After the debugger is introduced, you can set a breakpoint to interact with the interpreter during program execution, display the call stack, view and modify the variable value, there is also a single-step debugging line by line. (3) After IDE is introduced, you can use the mouse to do everything while observing the screen program. (4) You will also learn to write the compiler for JVM to generate the target code (that is, to generate the JVM assembly code), so that you can run the compiled program on multiple platforms. Of course, because compilers, interpreters, and ides are all written in Java, they can also run on multiple platforms.
The compiler interpreter you write will process the source program language Pascal. I chose Pascal for several reasons. First of all, it is a real programming language, not written for this book. Pascal is a process-oriented High-level Language in the middle of 1970s and 1980s. This language has a relatively straightforward syntax, but contains many interesting language features, such as structured user-defined types, nested scopes, value passing parameters/reference passing parameters, A complete set of control statements. Pascal is still alive today. You can download the free PASCAL Compiler and interpreter online to compare it with what you write.
Software Engineering Method
The compiler and interpreter are complex and difficult to succeed. To deal with this complexity, I use software engineering methods such as design patterns, Unified Modeling Language (UML) diagrams, and other modern object-oriented design practices to better understand and manage code.
Throughout the book, especially the early chapters,Design NotesPoint out the relevant design points, such as the design pattern used, and why I chose this method for architecture code.
I firmly believe that the method isIncremental development,Complete a small amount of code in each step, and build the code that can be run in the next step based on the previous step. Basically, each chapter contains a main working program and other concise incremental code. The program construction in each chapter is based on the Code in the above chapter. (Remind you not to skip reading)
Organization of books
The first chapter is the introduction. The second chapter describes the framework of the compiler and interpreter. How to design and test the framework lays a good foundation for the subsequent successful compilation. The next two chapters focus on basic (language) Conversion: lexical scanning (Chapter 3) and symbol table (chapter 4 ). The following chapter builds a runnable interpreter. Based on the incremental development method, these chapters iterate the syntax analysis and interpretation several times, each time adding new language features. Chapter 5 analyzes expressions and value assignment statements, Chapter 6 explains them, Chapter 7 analyzes control statements, Chapter 8 explanations, Chapter 9 analysis statements, and chapter 10 performs syntax checks, chapter 1 analysis procedure, functions, and programs ). Chapter 4 improve the interpreter so that it can run the whole PASCAL program.
The next two chapters are based on the previous work. Chapter 2 adds a source code-level debugger that can interact with commands through command line input. Chapter 2 encapsulates the command line debugger as an IDE using a GUI. You can skip these two chapters for the first time without losing continuity, but you must watch them later, because it talks about a lot of really Nb development tools.
The last part of this book describes how to develop compilers based on the previous chapter. Chapter 2 introduces the structure of the Java Virtual Machine (JVM) and the JVM assembly language generated by the compiler by Jasmin. It is also an incremental method. Chapter 1 compiles programs, value assignment statements, and expressions. Chapter 17 compilation process, function calls, and string operations. Chapter 18 describes how to complete the compiler by compiling control statements, arrays, and record.
Chapter 19 at the end briefly introduces compiler compilation topics not covered in the previous chapter, such as code optimization and table-driven lexical scanning and syntax analyzer.
Where can I get the source program?
You can download all the source programs used in this book in the Web http://www.apropos-logic.com/wci/ (but it is recommended that you read the Chinese version of the revised Code in each chapter, more comprehensive notes. You will find how to download, compile, install, and run the program. There are also a lot of Pascal testing programs above.