diff --git a/parser-tools/private-yacc/yacc-helper.rkt b/parser-tools/private-yacc/yacc-helper.rkt index e8c9663..b4f9a60 100644 --- a/parser-tools/private-yacc/yacc-helper.rkt +++ b/parser-tools/private-yacc/yacc-helper.rkt @@ -3,7 +3,7 @@ yaragg/parser-tools/private-lex/token-syntax) ;; General helper routines -(provide duplicate-list? remove-duplicates overlap? vector-andmap display-yacc) +(provide duplicate-list? remove-duplicates overlap? vector-andmap) (define (vector-andmap pred vec) (for/and ([item (in-vector vec)]) @@ -26,46 +26,3 @@ (for/first ([sym1 (in-list syms1)] #:when (memq sym1 syms2)) sym1)) - - -(define (display-yacc grammar tokens start precs port) - (let-syntax ([p (syntax-rules () - ((_ args ...) (fprintf port args ...)))]) - (let* ([tokens (map syntax-local-value tokens)] - [eterms (filter e-terminals-def? tokens)] - [terms (filter terminals-def? tokens)] - [term-table (make-hasheq)] - [display-rhs - (λ (rhs) - (for ([sym (in-list (car rhs))]) - (p "~a " (hash-ref term-table sym (λ () sym)))) - (when (= 3 (length rhs)) - (p "%prec ~a" (cadadr rhs))) - (p "\n"))]) - (for* ([t (in-list eterms)] - [t (in-list (syntax->datum (e-terminals-def-t t)))]) - (hash-set! term-table t (format "'~a'" t))) - (for* ([t (in-list terms)] - [t (in-list (syntax->datum (terminals-def-t t)))]) - (p "%token ~a\n" t) - (hash-set! term-table t (format "~a" t))) - (when precs - (for ([prec (in-list precs)]) - (p "%~a " (car prec)) - (for ([tok (in-list (cdr prec))]) - (p " ~a" (hash-ref term-table tok))) - (p "\n"))) - (p "%start ~a\n" start) - (p "%%\n") - (for ([prod (in-list grammar)]) - (define nt (car prod)) - (p "~a: " nt) - (display-rhs (cadr prod)) - (for ([rhs (in-list (cddr prod))]) - (p "| ") - (display-rhs rhs)) - (p ";\n")) - (p "%%\n")))) - - - diff --git a/parser-tools/yacc-to-scheme.rkt b/parser-tools/yacc-to-scheme.rkt deleted file mode 100644 index e6b4692..0000000 --- a/parser-tools/yacc-to-scheme.rkt +++ /dev/null @@ -1,129 +0,0 @@ -#lang racket/base -(require yaragg/parser-tools/lex - (prefix-in : yaragg/parser-tools/lex-sre) - yaragg/parser-tools/yacc - syntax/readerr) -(provide trans) - -(define match-double-string - (lexer - [(:+ (:~ #\" #\\)) (append (string->list lexeme) - (match-double-string input-port))] - [(:: #\\ any-char) (cons (string-ref lexeme 1) (match-double-string input-port))] - [#\" null])) - -(define match-single-string - (lexer - [(:+ (:~ #\' #\\)) (append (string->list lexeme) - (match-single-string input-port))] - [(:: #\\ any-char) (cons (string-ref lexeme 1) (match-single-string input-port))] - [#\' null])) - -(define-lex-abbrevs - [letter (:or (:/ "a" "z") (:/ "A" "Z"))] - [digit (:/ "0" "9")] - [initial (:or letter (char-set "!$%&*/<=>?^_~@"))] - [subsequent (:or initial digit (char-set "+-.@"))] - [comment (:: "/*" (complement (:: any-string "*/" any-string)) "*/")]) - -(define-empty-tokens x (EOF PIPE |:| SEMI |%%| %prec)) -(define-tokens y (SYM STRING)) - -(define get-token-grammar - (lexer-src-pos - ["%%" '|%%|] - [":" (string->symbol lexeme)] - ["%prec" (string->symbol lexeme)] - [#\| 'PIPE] - [(:+ (:or #\newline #\tab " " comment (:: "{" (:* (:~ "}")) "}"))) - (return-without-pos (get-token-grammar input-port))] - [#\; 'SEMI] - [#\' (token-STRING (string->symbol (list->string (match-single-string input-port))))] - [#\" (token-STRING (string->symbol (list->string (match-double-string input-port))))] - [(:: initial (:* subsequent)) (token-SYM (string->symbol lexeme))])) - -(define (parse-grammar enter-term enter-empty-term enter-non-term) - (parser - (tokens x y) - (src-pos) - (error (λ (tok-ok tok-name tok-value start-pos end-pos) - (raise-read-error - (format "Error Parsing YACC grammar at token: ~a with value: ~a" tok-name tok-value) - (file-path) - (position-line start-pos) - (position-col start-pos) - (position-offset start-pos) - (- (position-offset end-pos) (position-offset start-pos))))) - - (end |%%|) - (start gram) - (grammar - (gram - ((production) (list $1)) - ((production gram) (cons $1 $2))) - (production - ((SYM |:| prods SEMI) - (begin - (enter-non-term $1) - (cons $1 $3)))) - (prods - ((rhs) (list `(,$1 #f))) - ((rhs prec) (list `(,$1 ,$2 #f))) - ((rhs PIPE prods) (cons `(,$1 #f) $3)) - ((rhs prec PIPE prods) (cons `(,$1 ,$2 #f) $4))) - (prec - ((%prec SYM) - (begin - (enter-term $2) - (list 'prec $2))) - ((%prec STRING) - (begin - (enter-empty-term $2) - (list 'prec $2)))) - (rhs - (() null) - ((SYM rhs) - (begin - (enter-term $1) - (cons $1 $2))) - ((STRING rhs) - (begin - (enter-empty-term $1) - (cons $1 $2))))))) - -(define (symbolstring a) (symbol->string b))) - -(define (trans filename) - (define i (open-input-file filename)) - (define terms (make-hasheq)) - (define eterms (make-hasheq)) - (define nterms (make-hasheq)) - (define (enter-term s) - (when (not (hash-ref nterms s (λ () #f))) - (hash-set! terms s #t))) - (define (enter-empty-term s) - (when (not (hash-ref nterms s (λ () #f))) - (hash-set! eterms s #t))) - (define (enter-non-term s) - (hash-remove! terms s) - (hash-remove! eterms s) - (hash-set! nterms s #t)) - (port-count-lines! i) - (file-path filename) - (regexp-match "%%" i) - (begin0 - (let ([gram ((parse-grammar enter-term enter-empty-term enter-non-term) - (λ () - (define t (get-token-grammar i)) - t))]) - `(begin - (define-tokens t ,(sort (hash-map terms (λ (k v) k)) symbollist #'(ARGS ...)))]) (syntax-case* arg (debug error tokens start end precs grammar - suppress src-pos yacc-output) + suppress src-pos) (λ (a b) (eq? (syntax-e a) (syntax-e b))) [(debug FILENAME) (cond @@ -94,14 +93,6 @@ (if grammar (raise-syntax-error #f "Multiple grammar declarations" stx) (set! grammar (syntax/loc arg (PRODS ...))))] - [(yacc-output FILENAME) - (cond - [(not (string? (syntax-e #'FILENAME))) - (raise-syntax-error #f "Yacc-output filename must be a string" stx #'FILENAME)] - [yacc-output - (raise-syntax-error #f "Multiple yacc-output declarations" stx)] - [else - (set! yacc-output (syntax-e #'FILENAME))])] [_ (raise-syntax-error #f "argument must match (debug filename), (error expression), (tokens def ...), (start non-term), (end tokens ...), (precs decls ...), or (grammar prods ...)" stx arg)])) (unless tokens (raise-syntax-error #f "missing tokens declaration" stx)) @@ -122,17 +113,6 @@ end precs grammar)) - (when (and yacc-output (not (string=? yacc-output ""))) - (with-handlers [(exn:fail:filesystem? - (λ (e) (eprintf "Cannot write yacc-output to file \"~a\"\n" yacc-output)))] - (call-with-output-file yacc-output - (λ (port) - (display-yacc (syntax->datum grammar) - tokens - (map syntax->datum start) - (and precs (syntax->datum precs)) - port)) - #:exists 'truncate))) (with-syntax ([check-syntax-fix check-syntax-fix] [err error] [ends end] diff --git a/yaragg-parser-tools.scrbl b/yaragg-parser-tools.scrbl index 114d073..6e2330c 100644 --- a/yaragg-parser-tools.scrbl +++ b/yaragg-parser-tools.scrbl @@ -516,7 +516,7 @@ be the right choice when using @racket[lexer] in other situations. @defmodule[yaragg/parser-tools/yacc] @defform/subs[#:literals (grammar tokens start end precs src-pos - suppress debug yacc-output prec) + suppress debug prec) (parser clause ...) ([clause (grammar (non-terminal-id ((grammar-id ...) maybe-prec expr) @@ -529,8 +529,7 @@ be the right choice when using @racket[lexer] in other situations. (precs (assoc token-id ...) ...) (src-pos) (suppress) - (debug filename) - (yacc-output filename)] + (debug filename)] [maybe-prec code:blank (prec token-id)] [assoc left right nonassoc])]{ @@ -664,14 +663,6 @@ be the right choice when using @racket[lexer] in other situations. the LALR table file.} - @item{@racket[(yacc-output filename)] @italic{OPTIONAL} - - Causes the parser generator to write a grammar file in - approximately the syntax of @exec{yacc}/@exec{bison}. The file - might not be a valid @exec{yacc} file, because the Racket - grammar can use symbols that are invalid in C.} - - @item{@racket[(suppress)] @italic{OPTIONAL} Causes the parser generator not to report shift/reduce or @@ -717,7 +708,7 @@ library provides a parser generator that is an alternative to that of @racketmodname[yaragg/parser-tools/yacc].} @defform/subs[#:literals (grammar tokens start end precs src-pos - suppress debug yacc-output prec) + suppress debug prec) (cfg-parser clause ...) ([clause (grammar (non-terminal-id ((grammar-id ...) maybe-prec expr) @@ -742,30 +733,9 @@ library provides a parser generator that is an alternative to that of a single non-terminal-id.} @item{The @racket[cfg-parser] form does not support the @racket[precs], - @racket[suppress], @racket[debug], or @racket[yacc-output] - options of @racket[parser].} + @racket[suppress], or @racket[debug] options of @racket[parser].} ] } - -@; ---------------------------------------------------------------------- - -@section{Converting @exec{yacc} or @exec{bison} Grammars} - -@defmodule[yaragg/parser-tools/yacc-to-scheme] - -@defproc[(trans [file path-string?]) any/c]{ - -Reads a C @exec{yacc}/@exec{bison} grammar from @racket[file] and -produces an s-expression that represents a Racket parser for use with -@racket[parser]. - -This function is intended to assist in the manual conversion of -grammars for use with @racket[parser], and not as a fully automatic -conversion tool. It is not entirely robust. For example, if the C -actions in the original grammar have nested blocks, the tool will fail. - -Annotated examples are in the @filepath{examples} subdirectory of the -@filepath{yaragg-parser-tools} collection.} @; ----------------------------------------------------------------------