;; Token types (define LPAREN ‘LPAREN) (define RPAREN ‘RPAREN) (define MULT ‘*) (define DIV ‘/’) (define PLUS ‘+) (define MINUS ‘-) (define EXP ‘^) (define ID ‘ID) (define EOF ‘EOF) ;; Token structure (struct token (type value)) ;; Lexer: Convert input string into a list of tokens (define (lexer input) (define (classify-char c) (cond [(char=? c #() LPAREN] [(char=? c #)) RPAREN] [(char=? c #*) MULT] [(char=? c #/) DIV] [(char=? c #+) PLUS] [(char=? c #-) MINUS] [(char=? c #^) EXP] [(char-numeric? c) ID] [else (error “Unknown character:” c)])) (define (next-token str start) (let loop ([start start]) (if (>= start (string-length str)) (token EOF ‘()) (let* ([char (string-ref str start)] [type (classify-char char)]) (cond [(or (eq? type ID) (eq? type EXP)) (define-values (num-str rest-start) (number-token str start)) (token ID (string->number num-str))] [(not (eq? type ‘LPAREN)) ;; Skip parentheses (token type char)] [else (error “Unknown token:” char)]))))) (define (number-token str start) (define (number-token-helper str start) (if (or (>= start (string-length str)) (not (char-numeric? (string-ref str start)))) start (number-token-helper str (add1 start)))) (values (substring str start (number-token-helper str start)) (number-token-helper str start))) (let loop ([start 0] [tokens ‘()]) (define token (next-token input start)) (if (eq? (token-type token) EOF) (reverse tokens) (let* ([char (token-value token)] [type (token-type token)] [next-start (if (or (eq? type LPAREN) (eq? type RPAREN) (eq? type ID)) (add1 start) (add1 start 2))]) (loop next-start (cons token tokens)))))) ;; Parsing functions (define (parse-E tokens) (let ([parse-t-result (parse-T tokens)]) (if (pair? parse-t-result) (begin (define t1 (car parse-t-result)) (define rest (cdr parse-t-result))) (error “Error parsing T”))) (cond [(and (not (null? rest)) (eq? (token-type (car rest)) PLUS)) (parse-E (cdr rest))] [(and (not (null? rest)) (eq? (token-type (car rest)) MINUS)) (parse-E (cdr rest))] [else (null? rest)])) (define (parse-T tokens) (define-values (f1 rest) (parse-F tokens)) (cond [(and (not (null? rest)) (eq? (token-type (car rest)) MULT)) (parse-T (cdr rest))] [(and (not (null? rest)) (eq? (token-type (car rest)) DIV)) (parse-T (cdr rest))] [else (null? rest)])) (define (parse-F tokens) (cond [(eq? (token-type (car tokens)) LPAREN) (let ([sub-parse (parse-E (cdr tokens))]) (if (and (not (null? sub-parse)) (eq? (token-type (car sub-parse)) RPAREN)) (cons (car tokens) (cdr sub-parse)) ‘()))] [(eq? (token-type (car tokens)) MINUS) (let ([sub-parse (parse-F (cdr tokens))]) (if (not (null? sub-parse)) (cons (car tokens) (cdr sub-parse)) ‘()))] [(eq? (token-type (car tokens)) ID) (cons (car tokens) (parse-F (cdr tokens)))] [else ‘()])) ;; Main function to evaluate input (define (evaluate input) (let ([tokens (lexer input)]) (if (parse-E tokens) ‘t ‘f))) ;; Example usage (displayln (evaluate “(1 + 2) * (3 + 4)”)) ; Output: ‘t (displayln (evaluate “10 – (4 + 3) * 2”)) ; Output: ‘t (displayln (evaluate “48 / 2 / 3”)) ; Output: ‘t (displayln (evaluate “(5 + 5) * (5 – 3)”)) ; Output: ‘t (displayln (evaluate “3+4*2”)) ; Output: ‘f
i am not able to run it
Sumit Dubey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.