Can anyone explain to me the working logic of this pattern

Asked

Viewed 245 times

5

I am writing a compiler Scheme R6RS(school work). It is working perfectly with the exception of this standard proposed in the manual 11.19 - Macro Transformes: sintaxe-rules.

I have read and reread and can not understand how this pattern of ellipses(... ...) can return element 4 of the list (1 2 3 4 )?

Code I can’t understand working:

(define-syntax be-like-begin
  (syntax-rules ()

    ((be-like-begin name)

     (define-syntax name

       (syntax-rules ()

         ((name expr (... ...))

          (begin expr (... ...))))))))

(be-like-begin sequence)

(display (sequence 1 2 3 4))

The result is:

4

Someone please explain to me why it worked. That’s what I need to close a chapter of my life.

  • 1

    I have tested on some compilers and some do not recognize this pattern, but those who recognize the result is always 4.

  • Who negatived could please explain why this question is bad because this question is related to my monograph where I wrote a compiler of SCHEME R6RS language in C# and this expansion pattern is unique that I could not understand and is what prevents me from completing the compiler and here is the link in the official documentation for this expansion standard

  • 1

    Another great question for https://cs.stackexchange.com

1 answer

-1

The question is in the expr Begin:

Begin expr (... ...)

The expressions in your example, in case the numbers (1, 2, 3 and 4) are evaluated sequentially from left to right, and in case the result is the number itself (... ...)=template(Ellipsis Ellipsis), and all are ignored with the exception of the last one.

You can change your sequence as it is, the result will always be the last number.

  • I don’t care what Exp does I want to know the BNF to mount the abstract syntax tree that understands this pattern. And example shows Exp but can be any other function instead.See http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-2.html#node_toc_node_sec_11.19

  • But the question is not Exp but Begin, see: "Scheme Begin Expressions aren’t just code Blocks, though, because they are Expressions that Return a value. A Begin Returns the value of the last Expression in the Sequence." https://www.cs.utexas.edu/ftp/garbage/cs345/schintro-v14/schintro_18.html#:~:text=Scheme%20begin%20expressions%20aren’t,like%20begin%20s%20well.

  • This has nothing to do. I am writing a compiler I need the BNF to use in Bison so that I can generate a AST to compile the code the user enters. If the user enters this transformer my compiler does not understand. He understands all language but this macro expansion pattern.

  • The one you mentioned is defined here http://www.r6rs.org/final/html/r6rs/r6rs-Z-H-2.html#node_toc_node_sec_11.4.7 was one of the first nonterminals I made.

  • 1

    For example if you test this code on Racket works smoothly but tests on Biwascheme will give error because the author of the compiler is with the same problem as me, that is not understanding the logic of this macro transformer pattern.

  • It’s not that I’m boring or annoying. I’m sometimes writing another answer and I don’t give it enough attention. Thank you for being interested in my problem.

  • Relax. It might be interesting then to explain what happens when you try to compile this code into your compiler. If it doesn’t even compile or if the execution is what is wrong.

  • I never created the non-terminal symbols for this production, see the parser, because it doesn’t make sense. All language elements are functional except this production which makes my compiler into a toy compiler because it does not create hygienic macros.

  • Explaining why your answer does not answer the question, where is the "..." it can be expanded in the form (<pattern> ...) ou&#xA;(<pattern> <pattern> ... . <pattern>) ou&#xA;(<pattern> ... <pattern> <ellipsis> <pattern> ...) ou&#xA;(<pattern> ... <pattern> <ellipsis> <pattern> ... . <pattern>) ou&#xA;#(<pattern> ...) ou&#xA;#(<pattern> ... <pattern> <ellipsis> <pattern> ...) but in the manual it says that it is a syntactic abstraction but there is no punctual explanation of what. What left lost, I did make a try but the results gave divergent values than expected.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.