Skip to content

Instantly share code, notes, and snippets.

@kmicinski
Created November 1, 2017 15:32
Show Gist options
  • Save kmicinski/67616e00867714e116485acf82a8f972 to your computer and use it in GitHub Desktop.
Save kmicinski/67616e00867714e116485acf82a8f972 to your computer and use it in GitHub Desktop.
; I took most of this from Matt Might's:
; http://matt.might.net/articles/lexers-in-racket/
#lang racket
(require parser-tools/lex)
(require (prefix-in : parser-tools/lex-sre))
;(struct char (c) #:)
(define lex
(lexer
; skip spaces:
[#\space (lex input-port)]
; skip newline:
[#\newline (lex input-port)]
[#\+ 'plus]
[#\- 'minus]
[#\* 'times]
[#\/ 'div]
[(:: (:? #\-) (:+ (char-range #\0 #\9))) (string->number lexeme)]
; an actual character:
[any-char (string-ref lexeme 0)]))
(define ab-test-in (open-input-string "abba")) ;"1 + 2 * 3 + 4 * 1 + 3")); , aabbaa
(define (next-tok)
(lex ab-test-in))
(define curtok (next-tok))
(define (accept c)
(if (not (equal? curtok c))
(raise 'unexpected-token)
(begin
(printf "Accepting ~a\n" c)
(set! curtok (next-tok)))))
;; Example 1
(define (parse-A)
(match curtok
[#\a
(begin
(accept #\a)
(parse-A)
(accept #\a))]
[#\b (parse-B)]))
(define (parse-B)
(begin
(accept #\b)
(accept #\b)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment