MarpaX::Languages::C::AST on stackoverflow

32 views
Skip to first unread message

Jeffrey Kegler

unread,
Dec 26, 2013, 4:28:56 PM12/26/13
to marpa-...@googlegroups.com
On stackoverflow, someone asked how to find all the strings in a C program.  Amon has written two very nice tutorials in response. -- jeffrey

amon

unread,
Dec 28, 2013, 5:01:30 PM12/28/13
to marpa-...@googlegroups.com
Ah, I was wondering where that late upvote was coming from. I think my MarpaX::Languages::C::AST answer showcases the best of Perl (“glue language”) and Marpa to offer a concise – and more importantly: correct – solution to an otherwise difficult problem.

On the other hand it shows how fundamentally awkward it can be to work with a Marpa-generated AST. The SLIF offers the "bless" adverb, but only supports arrays as reference type. For a Marpa user, this causes problems when…
  1. …wanting to use standard hash-based objects, or
  2. …using more advanced object systems like Moose, Mouse, Moo, or p5-mop: external "bless"ings violate encapsulation.
It is also a Perl 5-ism that does not translate well to other host languages. I am of the opinion that it might have been a better design choice to invoke the appropriate constructor instead of "bless"ing a reference (which would have had the side effect of forcing Jean-Damien to write handy accessors for MarpaX::Languages::C::AST…). Is it your opinion that this problem has a satisfactory solution by wrapping the constructors in "action"s, or do you concur that syntactic sugar like a "new" adverb could make the SLIF more expressive?

As an aside, I am currently considering to write another tutorial in the spirit of “affordable languages”: The LLVM Kaleidoscope tutorial spends a bit too much time writing a simplistic parser which could have been used on more interesting topics of language implementation. I went through the OCaml version of that tutorial some time ago, so it would be fun to use the Perl bindings this time, using Marpa for the parser.

Jeffrey Kegler

unread,
Dec 28, 2013, 5:39:41 PM12/28/13
to marpa-...@googlegroups.com
Re the strategy of blessed array for AST's:� Early in Marpa's history I used callbacks of Perl code from C, but I abandoned that after a good deal of effort getting everything to work.� The issue for me is the debugging of applications.� In a callback the information returned crosses language boundaries twice, each time changing conventions for memory management, data structures, debugging, etc.� (If the callback takes arguments or examines context, as will often be the case, information crossed boundaries 4 times.)� Debugging code in callbacks can be done but at minimum it's one more layer of difficulty, and in other cases it can make things very difficult.

As for the arrays, my thought was that they are efficient, and that an array of paired name-values is trivially converted to a hash in Perl.

I think wrapping the constructors in actions would be OK.� I don't see what the difference in semantics would be between a "new" and the current "action" adverb, so don't see the value added.� Perhaps I misunderstood your point.

One approach that might be a "best of both worlds" approach might be to "wrapper" Marpa::R2 and post-process its AST's according to your best lights.� I don't think that approach is bad from the efficiency standpoint -- one reason for the choices of arrays over hashes is that if my AST's were used as an intermediate layer, their cost is low.� Also, it takes the interface experimentation outside of Marpa::R2 itself� If it's done inside Marpa::R2, for the moment that means only I can do it, and there are stability and compatibility issues.� Do it as a wrapper and you avoid all three problems.

The "affordable languages" theme is one I'd love to see addressed.� (You might recall my "What if languages were free?" blog post.)� Currently those of us in Marpa-land live is a kind of parallel universe, one very like the other one except that certain potentially important programming techniques, one that are almost unusably hard in their universe, are quite simple in ours.� It'd be nice to collapse the two universes.

-- jeffrey

On 12/28/2013 02:01 PM, amon wrote:
Ah, I was wondering where that late upvote was coming from. I think my MarpaX::Languages::C::AST�answer showcases the best of Perl (�glue language�) and Marpa to offer a concise � and more importantly: correct � solution to an otherwise difficult problem.

On the other hand it shows how fundamentally awkward it can be to work with a Marpa-generated AST. The SLIF offers the "bless" adverb, but only supports arrays as reference type. For a Marpa user, this causes problems when�
  1. �wanting to use standard hash-based objects, or
  2. �using more advanced object systems like Moose, Mouse, Moo, or p5-mop: external "bless"ings violate encapsulation.
It is also a Perl 5-ism that does not translate well to other host languages. I am of the opinion that it might have been a better design choice to invoke the appropriate constructor instead of "bless"ing a reference (which would have had the side effect of forcing Jean-Damien to write handy accessors for MarpaX::Languages::C::AST�). Is it�your opinion that this problem has a satisfactory solution by wrapping the constructors in "action"s, or do you concur that syntactic sugar like a "new" adverb could make the SLIF more expressive?

As an aside, I am currently considering to write another tutorial in the spirit of �affordable languages�: The LLVM Kaleidoscope tutorial�spends a bit too much time writing a simplistic parser which could have been used on more interesting topics of language implementation. I went through the OCaml version of that tutorial some time ago, so it would be fun to use the Perl bindings this time, using Marpa for the parser.

Am Donnerstag, 26. Dezember 2013 22:28:56 UTC+1 schrieb Jeffrey Kegler:
On stackoverflow, someone asked how to find all the strings in a C program.� Amon has written two very nice tutorials in response. -- jeffrey
--
You received this message because you are subscribed to the Google Groups "marpa parser" group.
To unsubscribe from this group and stop receiving emails from it, send an email to marpa-parser...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Durand Jean-Damien

unread,
Dec 28, 2013, 7:23:56 PM12/28/13
to marpa-...@googlegroups.com
FYI AST post-processing is what is done in c2ast.pl, and what will be done in the incoming MarpaX::Languages::C::Scan (an alternative to C::Scan providing the same methods - except the constructor up to now).
From what i am concerned I am fined with the blessed arrays, the rest, OO-like or seems decoration on Marpa::R2 - not a bad idea on itself -;
Thanks / JD.


Le samedi 28 décembre 2013 23:39:41 UTC+1, Jeffrey Kegler a écrit :
One approach that might be a "best of both worlds" approach might be to "wrapper" Marpa::R2 and post-process its AST's according to your best lights.
Reply all
Reply to author
Forward
0 new messages