guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: my current project: new lalr module


From: Matt Wette
Subject: Re: my current project: new lalr module
Date: Fri, 26 Jun 2015 13:43:21 -0700

On Jun 3, 2015, at 8:40 PM, Matt Wette <address@hidden> wrote:
My current project is a lalr module. 

Here is a slight update on my parser generator. 
1) I have added a "hashify" procedure that allows one to set up the parser and lex'er to use integers instead of symbols.
2) I have updated the specification parser and processor to allow use of strings.  My C-parser is using "++" and "+=" now.
3) I have updated the lexical analyzer tools to allow (I hope) easier construction of lexical analyzers.   The idea is that the user
    generates readers for identifiers, numbers, other character sequences, comments, etc and then throws those, along with a
   match-table provided by the parser-generator, to a procedure to make a lexical analyzer.

Still thinking about a name.  I may call it NYACC: "Not yet another compiler compiler!"

Here is the lexer:
    (lambda ()
      (let iter ((ch (read-char)))
        (cond
         ((eof-object? ch) (mapval (cons '$end ch)))
         ((char-set-contains? c:ws ch) (iter (read-char)))
         ((read-ident ch) => (lambda (s) (or (assq (string->symbol s) symtab)
                                             (cons '$ident s))))
         ((read-num ch) => mapval)
         ((read-chseq ch) => identity)
         ((read-string ch) => mapval)
         ((read-comm ch) => identity)
         ((skip-comm ch) => (iter (read-char)))
         ((assq-ref chrtab ch) => (lambda (t) (cons t ch)))
         (else (cons ch ch)))))))

And here is the parser when using the hashified interface:
       (lambda (lexr)
          (let iter ((state (list 0))           ; state stack
                     (stack (list '$dummy))     ; sval stack
                     (nval #f)     ; prev reduce to non-terminal value
                     (lval (lexr)))        ; lexical value (from lex'er)
            (let* ((tval (if nval (car nval) (car lval))) ; token (syntax value)
                   (sval (if nval (cdr nval) (cdr lval))) ; semantic value
                   (stxl (vector-ref pat-v (car state))) ; state transition list
                   (stx (or (assq-ref stxl tval) ; trans action (e.g. shift 32)
                            (assq-ref stxl 0) ; default action 
                            (cons 'error 0))))    ; else error
              (when pdbug (dmsg (car state) tval stx))
              (cond
                ((positive? stx)
                 (iter (cons stx state) (cons sval stack)
                       #f (if nval lval (lexr))))
                ((negative? stx)
                 (let* ((gx (abs stx)) (gl (vector-ref len-v gx))
                        ($$ (apply (vector-ref act-v gx) stack)))
                   (iter (list-tail state gl)
                         (list-tail stack gl)
                         (cons (vector-ref rto-v gx) $$)
                         lval)))
                ((zero? stx) ;; accept
                 (car stack))))))
Matt

           

reply via email to

[Prev in Thread] Current Thread [Next in Thread]