syntax-parse examples

125 views
Skip to first unread message

Dan Liebgold

unread,
Nov 1, 2016, 6:01:44 PM11/1/16
to Racket Users

I'm working with syntax-parse, and the examples in the docs are quite useful, but several seem stop before they get to the good part. Is there any way someone could supply examples of using the syntax classes developed in these sections:

- https://docs.racket-lang.org/syntax/varied-meanings.html?q=define-syntax-class#%28part._.Non-syntax-valued_.Attributes%29

- https://docs.racket-lang.org/syntax/More_Keyword_Arguments.html?q=define-syntax-class

I'm a little lost on how to properly access the attribute values in those classes...

Thanks,
Dan

Dan Liebgold

unread,
Nov 1, 2016, 6:08:20 PM11/1/16
to Racket Users
On Tuesday, November 1, 2016 at 3:01:44 PM UTC-7, Dan Liebgold wrote:
>
> - https://docs.racket-lang.org/syntax/varied-meanings.html?q=define-syntax-class#%28part._.Non-syntax-valued_.Attributes%29
>

For example, I can't seem to access the "ast" attribute in this particular example without a "bad attribute value for syntax template" error...

http://pasterack.org/pastes/55006

Jay McCarthy

unread,
Nov 2, 2016, 8:25:03 AM11/2/16
to Dan Liebgold, Racket Users
If you write this instead:

(define-syntax (myfor stx)
  (syntax-parse stx
    [(_ (clause:for-clause ...) body:expr)
     #`'(#,(attribute clause.ast) body)]))

Then you will get beyond that error (and get a new one about putting objects that aren't syntax into the syntax.)

Another approach is to do something like:

(define-syntax (myfor stx)
  (syntax-parse stx
    [(_ (clause:for-clause ...) body:expr)
     (with-syntax ([(the-ast ...) (attribute clause.ast)])
       #''(the-ast ... body))]))

As for examples, one of my favorite ones is this regular expression compiler implemented as a syntax-class where each attribute is kind of like a "method" that is called on the sub-components during the compilation:


Jay



--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jay McCarthy
Associate Professor
PLT @ CS @ UMass Lowell
http://jeapostrophe.github.io

           "Wherefore, be not weary in well-doing,
      for ye are laying the foundation of a great work.
And out of small things proceedeth that which is great."
                          - D&C 64:33

Dan Liebgold

unread,
Nov 2, 2016, 2:53:57 PM11/2/16
to Racket Users, dan_li...@naughtydog.com
On Wednesday, November 2, 2016 at 5:25:03 AM UTC-7, Jay McCarthy wrote:
>
> Another approach is to do something like:
>
>
> (define-syntax (myfor stx)
>   (syntax-parse stx
>     [(_ (clause:for-clause ...) body:expr)
>      (with-syntax ([(the-ast ...) (attribute clause.ast)])
>        #''(the-ast ... body))]))
>

This seems promising, but I'm not sure how to access the contents of structure referred to by the-ast (or clause.ast)... it seems like the struct bindings need to be accessible at both syntax and runtime phases?

>
> As for examples, one of my favorite ones is this regular expression compiler implemented as a syntax-class where each attribute is kind of like a "method" that is called on the sub-components during the compilation:
>
>
> https://github.com/jeapostrophe/automata/blob/master/automata-lib/re-compile.rkt
>

This is interesting, and seems to point the way to implementing more logic in the syntax phase. I'm wondering if this is how the examples from the docs would be best completed...

Jay McCarthy

unread,
Nov 2, 2016, 3:19:21 PM11/2/16
to Dan Liebgold, Racket Users
On Wed, Nov 2, 2016 at 2:53 PM, Dan Liebgold
<dan_li...@naughtydog.com> wrote:
> On Wednesday, November 2, 2016 at 5:25:03 AM UTC-7, Jay McCarthy wrote:
>>
>> Another approach is to do something like:
>>
>>
>> (define-syntax (myfor stx)
>> (syntax-parse stx
>> [(_ (clause:for-clause ...) body:expr)
>> (with-syntax ([(the-ast ...) (attribute clause.ast)])
>> #''(the-ast ... body))]))
>>
>
> This seems promising, but I'm not sure how to access the contents of structure referred to by the-ast (or clause.ast)... it seems like the struct bindings need to be accessible at both syntax and runtime phases?

(attribute clause.ast) is the list of structures. So you could do
something like:

(filter guard? (attribute clause.ast)) and then do something different
in each one

>>
>> As for examples, one of my favorite ones is this regular expression compiler implemented as a syntax-class where each attribute is kind of like a "method" that is called on the sub-components during the compilation:
>>
>>
>> https://github.com/jeapostrophe/automata/blob/master/automata-lib/re-compile.rkt
>>
>
> This is interesting, and seems to point the way to implementing more logic in the syntax phase.

I think of it as the syntax attribute being like one of the many
denotations of the syntax and the main macro pulls them together

> I'm wondering if this is how the examples from the docs would be best completed...
>
> --
> You received this message because you are subscribed to the Google Groups "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages