diff --git a/beautiful-racket-demo/algebra-demo/grammar.rkt b/beautiful-racket-demo/algebra-demo/grammar.rkt index 7668cee..0bb4b17 100644 --- a/beautiful-racket-demo/algebra-demo/grammar.rkt +++ b/beautiful-racket-demo/algebra-demo/grammar.rkt @@ -2,5 +2,5 @@ top : (fun | app)* fun : /"fun" ID /"(" ID [/"," ID] /")" /"=" expr -expr : ID "+" ID | app +expr : ID /"+" ID | app app : ID /"(" (ID | INT) [/"," ID] /")" \ No newline at end of file diff --git a/beautiful-racket-demo/algebra-demo/main.rkt b/beautiful-racket-demo/algebra-demo/main.rkt index e8dd255..b11ff54 100644 --- a/beautiful-racket-demo/algebra-demo/main.rkt +++ b/beautiful-racket-demo/algebra-demo/main.rkt @@ -9,7 +9,7 @@ (:or "fun" "(" ")" "=" "+" ",")) (define tokenize-1 - (lexer + (lexer-srcloc [whitespace (token lexeme #:skip? #t)] [(from/stop-before "#" "\n") (token 'COMMENT #:skip? #t)] [reserved-toks lexeme] @@ -23,12 +23,13 @@ [(_ VAR ARG0 ARG1 EXPR) #'(define (VAR ARG0 ARG1) EXPR)]) (define-macro-cases expr - [(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)] + [(_ LEFT RIGHT) #'(+ LEFT RIGHT)] [(_ OTHER) #'OTHER]) (define-macro app #'#%app) (define (read-syntax src ip) + (port-count-lines! ip) (define parse-tree (parse src (λ () (tokenize-1 ip)))) (strip-bindings (with-syntax ([PT parse-tree]) diff --git a/beautiful-racket-demo/precalc-demo/grammar.rkt b/beautiful-racket-demo/precalc-demo/grammar.rkt index 53b07e7..22861cc 100644 --- a/beautiful-racket-demo/precalc-demo/grammar.rkt +++ b/beautiful-racket-demo/precalc-demo/grammar.rkt @@ -3,9 +3,9 @@ top : (fun | app)* fun : /"fun" var /"(" argvars /")" /"=" expr /argvars : [var (/"," var)*] -@expr : s-or-d -s-or-d : [s-or-d ("+" | "-")] p-or-q -p-or-q : [p-or-q ("*" | "/")] value +@expr : add-or-sub +add-or-sub : [add-or-sub ("+" | "-")] mult-or-div +mult-or-div : [mult-or-div ("*" | "/")] value @value : var | INT | app | /"(" expr /")" app : var /"(" [expr (/"," expr)*] /")" @var : ID \ No newline at end of file diff --git a/beautiful-racket-demo/precalc-demo/main.rkt b/beautiful-racket-demo/precalc-demo/main.rkt index 4958c41..55baee3 100644 --- a/beautiful-racket-demo/precalc-demo/main.rkt +++ b/beautiful-racket-demo/precalc-demo/main.rkt @@ -1,6 +1,6 @@ #lang br/quicklang (require brag/support "grammar.rkt") -(provide top fun app s-or-d p-or-q) +(provide top fun app add-or-sub mult-or-div) (module+ reader (provide read-syntax)) @@ -16,20 +16,23 @@ [(:or (from/stop-before "#" "\n") (from/to "/*" "*/")) (token 'COMMENT #:skip? #t)] [reserved-toks lexeme] - [(:seq alphabetic (:* (:or alphabetic) digits)) (token 'ID (string->symbol lexeme))] - [(:seq (:? "-") (:+ digits)) (token 'INT (string->number lexeme))])) + [(:seq (:? "-") (:+ (:or alphabetic) digits)) + (let ([maybe-num (string->number lexeme)]) + (if maybe-num + (token 'INT maybe-num) + (token 'ID (string->symbol lexeme))))])) (define-macro top #'#%module-begin) (define-macro (fun VAR (ARGVAR ...) EXPR) #'(define (VAR ARGVAR ...) EXPR)) -(define-macro-cases s-or-d +(define-macro-cases add-or-sub [(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)] [(_ LEFT "-" RIGHT) #'(- LEFT RIGHT)] [(_ OTHER) #'OTHER]) -(define-macro-cases p-or-q +(define-macro-cases mult-or-div [(_ LEFT "*" RIGHT) #'(* LEFT RIGHT)] [(_ LEFT "/" RIGHT) #'(/ LEFT RIGHT)] [(_ OTHER) #'OTHER]) @@ -37,6 +40,7 @@ (define-macro app #'#%app) (define (read-syntax src ip) + (port-count-lines! ip) (define parse-tree (parse src (λ () (tokenize-1 ip)))) (strip-bindings (with-syntax ([PT parse-tree])