• Post Reply Bookmark Topic Watch Topic
  • New Topic

Let's Build A Compiler  RSS feed

 
Michael Justice
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I trying to recreate Jack Crenshaws Let's Build a Compiler. I'm using chapter's 15's OOP approach since it is much closer to Java, but actually am only on chapter 1. I have run into a problem, in pascal the class Input has a method called getChar() which retrieves a character and puts it in the member variable lookAhead. However the class Scanner also has a getChar() member method which does the same thing. The problem is each class has it's own instance of lookAhead which the other can't see. This is what I'm come up with so far and I'm stuck. Any assistance in this matter would be greatly appreciated.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you make a small, simple, complete example program to show your problem and post your code here on the forum?   Be sure to wrap the code in code tags.
 
Michael Justice
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Can you make a small, simple, complete example program to show your problem and post your code here on the forum?   Be sure to wrap the code in code tags.

sure:

nullCompiler.java


Input.java


Errors.java


Scanner.java
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
each class has it's own instance of lookAhead 

Can you explain?  I only see one instance of lookAhead in the Input class.

Note: Creating a new instance of the Scanner class for each read of a char may lose data that had been read by the previous instance of Scanner.
 
Michael Justice
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
each class has it's own instance of lookAhead 

Can you explain?  I only see one instance of lookAhead in the Input class.

Note: Creating a new instance of the Scanner class for each read of a char may lose data that had been read by the previous instance of Scanner.


The reason lookAhead is in the Input class is because it belongs there. However member methods getName() and getNum() both need access to the method. If I create a new instance of Input lookAhead will be blank instead of holding the next character. What I need to do is create 1 instance of Input which Scanner will have access to the getChar() method in Scanner and main which is where the actual input is being gathered.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. If I create a new instance of Input lookAhead will be blank instead of holding the next character.

Then only  create one instance of the Input class to  be used by any other classes that need it.
One solution would be to make Input a singleton.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!