unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Matt Wette <matt.wette@gmail.com>
To: guile-user@gnu.org
Subject: Re: Nyacc question: where are the actions bound?
Date: Sun, 8 Mar 2020 08:10:50 -0700	[thread overview]
Message-ID: <40b09b97-862e-6bb0-3205-45bb9dd35d46@gmail.com> (raw)
In-Reply-To: <20200308101445.GB28250@tuxteam.de>



On 3/8/20 3:14 AM, tomas@tuxteam.de wrote:
> Hi,
>
> I'm playing around with Nyacc: I found a first little use case
> to get my feet wet.
>
> First of all, than you, Matt, for this impressive package.
>
> Shamelessly stolen from the minimal example, playground looks
> roughly like this:
>
> #+begin_source scheme
>    (use-modules (nyacc lalr))
>    (use-modules (nyacc lex))
>    (use-modules (nyacc parse))
>    
>    ;; to be used in some ($$ ...) actions:
>    (define (collect arg) (display arg))
>    
>    (define my-grammar
>      (lalr-spec
>       (start my-file)
>       (grammar
>        (my-file
>         (elt-list))
>        ;; more productions, calling out to ($$... collect)
>        (name
>         ($ident)))))
>    
>    (define mach (make-lalr-machine aq-grammar))
>    (define mtab (lalr-match-table mach))
>    (define gen-lexer (make-lexer-generator mtab))
>    (define raw-parse (make-lalr-parser mach))
>    (define (parse) (raw-parse (gen-lexer)))
>    (parse)
> #+end_source
>
> So I defined some function =collect= which will be called from
> actions in the grammar.
>
> My question is: where is the stuff resolved which is mentioned
> in grammar actions? My first experiments indicate that it's
> looked up at (module) top level, as if (grammar ...) greedily
> weaved that in at compile time.
>
> My idea would be to (pre-) define a grammar and to exchange
> the actions later as needed. Or would I have to re-define the
> grammar whenever I change my mind about actions?
>
> Note that I'm still very much in exploratory mode: "you're
> holding it wrong" would be a perfectly adequate answer, as
> would be "your mumblings are pretty unintelligible" :-)
>
> Cheers & thanks
> -- tomás
Nyacc was envisioned with the paradigm you propose, by using tags.
But I have not tried that in a while.  If you look at the example calc
in examples/nyacc/lang/calc/mach.scm you will see

(define (gen-calc-files)
   (write-lalr-actions
    full-mach "mach.d/calc-full-act.scm" #:prefix "calc-full-")
   (write-lalr-tables
    full-mach "mach.d/calc-full-tab.scm" #:prefix "calc-full-")
   (write-lalr-actions
    stmt-mach "mach.d/calc-stmt-act.scm" #:prefix "calc-stmt-")
   (write-lalr-tables
    stmt-mach "mach.d/calc-stmt-tab.scm" #:prefix "calc-stmt-")
   )

You can run this, then you can use the generated files to provide
the parser (don't need to run make-lalr-machine anymore.  There
are two files generated: one with all the parser tables and one
with the actions (only).   You could (copy and) modify the actions
file to get different parser actions.   That file includes comments
to help:

(define calc-full-act-v
   (vector
    ;; $start => prog
    (lambda ($1 . $rest) $1)
    ;; prog => stmt-list
    (lambda ($1 . $rest) (tl->list $1))
    ;; stmt-list => stmt
    (lambda ($1 . $rest) (make-tl 'stmt-list $1))
    ;; stmt-list => stmt-list stmt
    (lambda ($2 $1 . $rest) (tl-append $1 $2))
    ;; stmt => "\n"
    (lambda ($1 . $rest) `(empty-stmt))
    ...

Hope this helps.

Matt



  reply	other threads:[~2020-03-08 15:10 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-08 10:14 Nyacc question: where are the actions bound? tomas
2020-03-08 15:10 ` Matt Wette [this message]
2020-03-09  9:07   ` tomas
2020-03-14 11:59   ` Nyacc question: [found] " tomas
2020-03-14 14:31     ` Matt Wette
2020-03-14 15:47       ` tomas
2020-07-30 13:50 ` Nyacc question: " Matt Wette
2020-07-30 17:17   ` tomas
2020-08-03 20:42   ` tomas
2020-08-03 23:37     ` Matt Wette

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=40b09b97-862e-6bb0-3205-45bb9dd35d46@gmail.com \
    --to=matt.wette@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).