> I am in the process of writing a simple scheme interpreter for a class I have. I am running into an issue with regards to parsing the input properly. I will try and explain what I want to do and tell me if I am way off base or not. I have a procedure set up to read the input from the user. Which is just a simple display read function. I want to pass that read value in, and then read it character by character. If it is a "(" then up a counter(to make sure there is an equal number of ")", if it is a number, i want to read the number and store it into a variable, and if it is an operator I want to pass the variables to the proper functions I have written to evaluate the input.
>
> I'm really stuck at step 2... I can read the input, however I can't seem to pass that input to another function and then break it down character by character.. I thought read-char should do it, but it only seems to work if it is in the function to read the input.. and it doesn't seem to work if passed to another function..
I understand your big picture, but I have no idea what kinds of functions you're trying to write. Can you describe the function you're trying to write in one line, then specify what it takes and what it returns, and provide a few examples of what you expect it to produce on certain inputs?
In the book "How To Design Programs", these are the first few steps of the "Design Recipe," and it sounds like they would help you clarify what it is you're trying to do.
All the best,
John Clements
Hi,
That would not be considered an "example" for at least two reasons. Do you see why?
On Nov 9, 2010 12:17 PM, "Peter Breitsprecher" <pkbr...@lakeheadu.ca> wrote:
As an example, I want have most of the functions for Plus, Minus already written.
Here is the code for it.
(define (plus num1 num2)
(+ num1 num2))
That is childs stuff... Here is where I am having the problem, and please keep in mind I am new to DrRacket...
If my program accepts input such as
(plus 13 5)
I need to parse it so that I know what it is asking for. If It is a left parent, then I need to move on, if it is a right parent, move on to the next character, once i get to the next character I can see it is an alphanumeric character, but then I need to somehow read the whole word, so that I can store that value in a variable so that I can read the next number. If the numbers are single digits, it is easy, because they are only one charater each and read-char works perfect, but I need something like a read-word, or read-number and I can't seem to figure out how to do it.
So really I need to know how do I read the words when there are characters present, and read the whole numbers when there is a number present.
On 9 November 2010 15:00, John Clements <clem...@brinckerhoff.org> wrote:
>
>
> On Nov 9, 2010, a...
--
Kurt Breitsprecher
(807) 474-9601
pkbr...@lakeheadu.ca
_________________________________________________
For list-related administrative tasks:
http://lists.racket-lang.org/listinfo/users
> I'd lie but no.. I was just trying to clarify what I was looking for. I gave a the code for one of the functions I had written, and then an example of the input the user is giving below, and then tried to describe in more detail what I needed help with...
Well, I'm glad you're not lying :).
Can you start by picking a name for the function that you're trying to write?
John Clements
> Ok, here is the code I have written so far for the function I am having trouble with...
>
> (define (rep-loop)
> (newline)
> (display "repl>")
> (read-token))
I'm going to ignore rep-loop.
>
> (define (read-token)
> (let ((expr (read-char)))
> ((cond [(char-whitespace? expr) (read-token1)]
> [(eq? expr #\() (read-token2)]
> [(eq? expr #\)) (read-token3)]
> [(char-alphabetic? expr) (read-token4)]
> [(char-numeric? expr) (read-token5)]
> [else (display "Exiting Loop")]))))
Okay! we've got a name: read-token.
The way it's written, it's going to be very hard to test. This is because it operates on typed-in input.
This program would be much easier to develop if you could write a function that accepts strings as input. Can we break the problem up this way?
John Clements
> Ok, here is the code I have written so far for the function I am having trouble with...
I'm also a bit curious: does your assignment say anything about using or not using 'read' ?
John Clements
Tiny Scheme Details: The Scheme or LISP that you are going to implement should include the following primitives:
T, NIL, CAR, CDR, CONS, ATOM, EQ, NULL, INT,
PLUS, MINUS, TIMES, LESS, GREATER COND, QUOTE, DEFINE.
T and NIL represent true and false. In general atoms may be identifiers or integers. An identifier will start with a letter of the alphabet, and maybe followed by one or more letters or digits; only uppercase letters will be used. No underscores or other characters are allowed in identifiers. Integers may be signed or unsigned,
i.e., 25, +25, −25 are all legal.
CAR, CDR, CONS are the standard Scheme primitive functions. ATOM returns T if its argument is atomic, and returns NIL otherwise. INT returns T if its argument is an atom that is a number. EQ works only on atomic arguments; it returns T if its two atomic arguments are the same [or equal, for numbers] and NIL otherwise. NULL returns T if its argument is NIL and NIL otherwise (non-atomic arguments should be acceptable to NULL).
PLUS, MINUS etc., take two numbers [integers] as their arguments and return the result [an integer]. LESS and GREATER allow you to compare two integers and return T if the first is less or greater than the second respectively.
COND and QUOTE are the standard Scheme forms. DEFINE allows the user to define a new function and use it later. A function definition looks like
(define (f x) fb )
where f is the function being defined; its formal parameter(s) is named x; and its body is the Scheme expression fb. When this is “evaluated”, it should add a pair (f . ((X Y) . fb)) to the d-list [the list of definitions that your interpreter should maintain internally; actually, how you store function definitions on the d-list is up to you; the above is only one possibility]. The features of Tiny Scheme that have not been included are: LAMBDA, LABEL. These are not needed because Define is sufficient for defining new functions.
> Ok, here is the exact assignment.
Blecch, I don't like this assignment :).
Based on the text, it appears that your professor wants you to do both parsing and evaluation. And some GUI stuff. Glug.
Here's what I'd do: since your professor is willing to let you use Scheme, ask to see whether you're allowed to use "read" as well; this will take care of the parsing part in one fell swoop.
I think this is no longer really of general interest to the racket community, so let's take it off-line.
John Clements
hey, it's you lucky day! Tiny Scheme is implemented already and is an
open-source project, so you may take a look at its source code:
http://tinyscheme.sourceforge.net/
;)
On Tue, Nov 9, 2010 at 6:50 PM, Peter Breitsprecherhey, it's you lucky day! Tiny Scheme is implemented already and is an
<pkbr...@lakeheadu.ca> wrote:
> Ok, here is the exact assignment.
>
> Tiny Scheme Details: The Scheme or LISP that you are going to implement
> should include the following primitives:
open-source project, so you may take a look at its source code:
http://tinyscheme.sourceforge.net/
;)