Crenshaw rewrite template design “let's build a compiler”?

I am rewriting Jack Crenshaw "let me build the compiler" from Turbo Pascal 4.0 to JAVA. This is motivating because the classic book does not yet have an OO version.

Is there a more modern version of OO for “Let's create a compiler”?

The book contains 15 chapters. The compiler is presented in stages: in the chapter I give code codes for the entire compiler, then each chapter adds some lines to the Pascal procedures in the previous chapter.

I have already translated the first two chapters, each chapter as a package, each Pascal procedure as a static protected method in JAVA, the procedures of one chapter are collected in one class, which extends the class translated from its previous chapter.

package td1; public class Cradle { protected final static char TAB='\t'; protected static char look; protected static void getChar() throws IOException { look=(char) System.in.read(); } ... } package td2; public class Cradle extends td1.Cradle{ protected static void factor() throws IOException { ... } ... } 

However, when I come to td3, I need to update factor () td2.Cradle, but I don't want to change factor () in td2.Cradle, because factor () will do this in td2 do more than it should have imagined in td2 . I was thinking about the "extension" of td2.Cradle (), however, it seems impossible to extend the static class.

My related question is here

Maybe I should change each static method to non-static?

I definitely need some kind of design template, can anyone help? I hope I get it. As such, this project step by step presents more and more instructions for each procedure, and I hope to record intermediate steps using some JAVA mechanism, such as inheritance.

Pascal code is a classic book, here is LBC . I try to use inheritance because

  • Each chapter calls / adds several lines to the procedures defined in the use case chapters
  • I hope to make the JAVA source code available to anyone who wants to follow the LBC step by step. Therefore, it is impractical to use one class to host the final source code of the author's compiler. It is imperative to divide the codes into chapters and gradually increase them, as Crenshaw did.

My actual solution is to keep tp1.Cradle methods as static. Methods in tp2.Cradle, tp3.Cradle, ..., before tp15.Cradle will be non-stationary, and all of them will statically import tp1.Cradle. *. Moreover, for every integer i greater than 2, tp [i]. Cradle extends tp [i-1]. Cradle.

Feel free to tell me the best solution.

+6
source share
1 answer

You seem to be on the right track. To be able to override these methods, they must be instance methods. And so you should move away from the model that relies on these "global methods", to the instance-based model, where you create an instance of the Cradle class and call the appropriate methods on that instance.

+1
source

Source: https://habr.com/ru/post/914969/


All Articles