大家好,我在编程方案中非常新,所以我试图在方案中构建一个词法分析器,例如基本上读取一个列表,< SUM + 34 >
输出将是这样的
{ is the left bracket
SUM is an identifier
+ is the plus operator
34 is a Digit
} is the right bracket
我正在使用 dr.scheme 并且该程序并没有准确地告诉我我缺少什么或提示我的错误。一切对我来说都很新鲜
这是我到目前为止所尝试的:
(define alist'( < SUM + 34 >))
(define (token? object)
(and (list? object)
(not (null? object))
(eq? (car object) 'token)))
(define (ident-token? token)
(type-token? token 'IDENTIFIER))
(define (digit-token? token)
(type-token? token 'DIGIT))
(define (op-token? token)
type-token? token 'OPERATOR)
(define (type-token? token type)
(and(type-token? token)
(eq? (name-token token) type)))
(define (name-token token)
(cadr token))
(define (value-token token)
(caddr token))
(define (spot-token)
(cadddr token))
(define (assign-token name value spot)
(list 'token name value spot))
(define *peek* '())
(define (check-token alist)
(let ((token (if (null? *peek*)
(<token> in)
*peek)))
(set! *peek* '())
token))
(define (peek-token alist)
(let ((token (if (null? *peek*)
(read-token in)
*peek*)))
(set! *peek* (if token token '()))
token))
(define (<token> alist)
(let* (next-loc (next-spot in)) (next-char (peek-char in))
(cond ((eof-object? next-char) #f)
((char-whitespace? next-char)
(begin (check-char in)
(<token> in)))
((char-ident-initial? next-char)
(<identifier> (list (check-char in)) next-loc in))
(else
(let ((next-char (check-char in)))
(cond ((char=? #\( next-char)
(assign-token 'LEFT-PAREN "(" next-loc))
((char=? #\) next-char)
(assign-token 'RIGHT-PAREN ")" next-loc))
((char=? #\` next-char)
(assign-token 'ADD-OP "+" next-loc))
((char=? #\] next-char)
(assign-token 'SUB-OP "-" next-loc))
((char=? #\{ next-char)
(assign-token 'MULT-OP "*" next-loc))
((char=? #\} next-char)
(assign-token 'DIV-OP "/" next-loc))
(else
(syntax-error next-loc next-char))))))))
请伙计们,我正在尽力而为,我没有尝试编译。我用谷歌搜索了很多东西,但我找不到什么可以帮助我……即使你有可以帮助我的教程或指南,请分享