Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

A minilanguage with the least effort?

by esk555 (Beadle)
on Feb 17, 2009 at 13:16 UTC ( #744394=perlquestion: print w/ replies, xml ) Need Help??
esk555 has asked for the wisdom of the Perl Monks concerning the following question:

In the interest of laziness, how would one go about making
a minilanguage with the least amount of knowledge and effort?

The minilanguage can be about anything, as long as it is
of some use.

Comment on A minilanguage with the least effort?
Re: A minilanguage with the least effort?
by moritz (Cardinal) on Feb 17, 2009 at 13:21 UTC
    The least effort is eval, which can be used to implement a mini-language called "Perl". (Note that this comes with security implications).

    The second least effort is to use a CPAN module that implements a minilanguage.

    Or of course you can write a parser for it (if it's really mini that's going to be easy) and a backend.

    The exact approach varies greatly on what you actually want your minilanguage to do.

      "Security implications"? Surely it would be just as secure as everyone's favourite language: Perl.

      That aside, here's an implementation (complete with syntax checking) for the mini-est language I can think of.

      $_ = <>; die "Syntax Error\n" unless m/^[10]$/; die "Syntax Error\n" if defined <>; exit $_;
      I'll leave it up to you to work out what the valid input is.

      --
      use JAPH;
      print JAPH::asString();

Re: A minilanguage with the least effort?
by gwadej (Chaplain) on Feb 17, 2009 at 14:11 UTC

    One approach I've used for really simple languages is a command hash. The keys of the hash are commands and the value is a code ref that executes the command. This approach is really not very flexible, but it fills the niche below a full scale language implementation.

    my %command = ( foo => \&do_foo, bar => sub { print "doing bar with parameters: @_\n"; }, # ... );

    If you decide you really need a language, rather than a set of commands, look for parsing modules on CPAN.

    The best advice I ever got on this issue was to write some examples of how you would use the language if it worked like you want. Then, go make it look like that.

    Without some idea of the complexity of the language you want to implement, it is hard to guess which tool would be the least effort.

    G. Wade
Re: A minilanguage with the least effort?
by roboticus (Canon) on Feb 17, 2009 at 14:24 UTC
    esk555:

    <geezer_mode><irrelevent_mode>

    Some years back (late 70's?) in an issue of Byte (IIRC) there was an amusing article called "Watduzitdo?" (or something similar) in which the author implemented a simplified version of Pilot in a mere 256 bytes of assembler.

    I remember spending an amusing afternoon modifying it to run on a brand-new TRS-80...

    </irrelevent_mode></geezer_mode>

    ...roboticus

    Almost on topic...
Re: A minilanguage with the least effort?
by ELISHEVA (Prior) on Feb 17, 2009 at 14:34 UTC
    The simplest parser begins with the simplest language design. A language that has a one token (or even two) look-ahead will always be easier to parse than a language that requires consuming long runs of tokens before you know what the run means. For example, suppose we had a language that looked like this:
    action article target "\n" action ::= "WALK" | "FEED" | "PLAY WITH" target ::= "DOG" | "CAT" | "CANARY" | "FISH" article ::= "A" | "THE"

    Language samples would look like this:

    WALK THE DOG FEED THE CANARY PLAY WITH A FISH

    and a parser (sans error detection) could be as simple as this:

    use strict; use warnings; while (my $sLine = <DATA>) { my @aFields = split(/\s+/, $sLine); #the general case my $sAction = shift @aFields; my $sArticle = shift @aFields; my $sTarget = shift @aFields; #special cases - PLAY WITH has two tokens if ($sAction eq 'PLAY') { $sAction .= " $sArticle"; #$sArticle is 'WITH' $sArticle = $sTarget; #$sTarget is an article $sTarget = shift @aFields; #target never got read } doSomethingWithStatement($sAction, $sArticle, $sTarget); } sub doSomethingWithStatement { my ($sAction, $sArticle, $sTarget) = @_; print "action=<$sAction> article=<$sArticle> target=<$sTarget>\n"; } __DATA__ WALK THE DOG FEED THE FISH PLAY WITH A CANARY

    Of course, for production purposes you would probably want to do some error checking. Also you might want to make certain combinations of verbs and targets illegal. For example, it doesn't make much sense to "WALK A FISH". And if you really want to get fancy you can explore the wonderful world of lexers. But hopefully this will illustrate the basic idea.

    I wanted to provide you with a link to an easy to understand non-jargon filled article on designing an easy to parse language, but I'm having little luck googling for that. Perhaps another Monk with better search term fu can help you with that.

    Best, beth

Re: A minilanguage with the least effort?
by BrowserUk (Pope) on Feb 17, 2009 at 14:57 UTC
      avast claims that the link is malware :(

      A Trojan Horse Was Found ... File name: http://billhails.net/Book/ Malware name: JS:Agent-AV [Trj] Malware type: Trojan Horse VPS version: 090216-1, 16/02/2009
      So take care

        The link itself? Something on the page pointed to? Something linked to from the page pointed to?

        Google doesn't flag it?

        What's "avast"? An how much faith do you have in it?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: A minilanguage with the least effort?
by JavaFan (Canon) on Feb 17, 2009 at 15:46 UTC
    Morse only has three characters: dot, dash, space, which combine into a small number of valid tokens.
Re: A minilanguage with the least effort?
by jethro (Monsignor) on Feb 17, 2009 at 15:57 UTC

    Anecdotal evidence: I wrote a sort of "miniperl" interpreter that I believed (or lets say hoped) to be secure by transforming and filtering the miniperl into executable perl and using eval to execute it. It operated upon a hierarchical set of hashes and was crippled down to essentially mathematical operations, strings, control structures and calling of functions I allowed or provided.

    It was for a local management program for a play-by-mail space opera game, so security was not really necessary, just a bonus so that config files could be exchanged between player and game master without worries

    A typical example of what it did:

    #bi is population index planet 'Taurus I' : bi+= 200+10*3 : system.scoutdata='Heavily armed' system taurus : code { if ( scoutdata eq '' ) { owned=1 } } translated to: $planet{'taurus i'}{'bi'}+= 200+10*3; $planet{'system'}{'scoutdata'}= 'Heavily armed'; if ($system{'taurus'}{'scoutdata'} eq '') { $system{'taurus'}{'owned'} +=1; }

    All the hashes were tied so that the %system-hash of a planet could be reached by simply saying system.something= .... This made it possible to do consitency checks or further actions when a variable was changed. Also if a variable like 'owned' was configured as hierarchical, not only was taurus.owned set to 1 in the example above but also the 'owned' of every planet in this system, i.e. 'taurus i'.owned would have been set too

    As you can see I had to exclude most of the special characters from my language, especially all the sigils. Since arrays or hashes couldn't be specified directly, looping was only possible through function calls. No regex was possible

    It was fun programming it, but you really have to cripple the language to put perl into a sandbox (and I still can't be completely sure I plugged all holes). While it fitted my needs in this case perfectly, I wouldn't really trust it on a public interface in the web for example

    By the way, I used Parse::RecDescent for the parsing of the language (actually it was really two languages that were parsed)

Re: A minilanguage with the least effort?
by targetsmart (Curate) on Feb 17, 2009 at 16:14 UTC
    IMO Why do you want to re-invent the wheel, a lot of languages already exist and if you want to solve problems you can write programs rather than the language itself.
    on the other side, good initiative, it is an interesting goal to have for oneself, 'writing a mini language of my own', but if you have lot of time left apart from spending time for your normal earning to live; you can very well spend time for this mini language.
    even I have goals like 'take the latest linux kernel, understand it thoroughly, then contribute or write my own version of kernel' to call myself as a geek and to get some satisfaction for life/intelligence. But it depends on how much extra time I have apart from my earning for living a decent life.
    I really admire those people who have already taken up those goals and lived up to that besides whatever struggles they face.
    because If they have not taken these initiatives, we have nothing to work on now(eg, all open source stuff).
    your this post is triggering lot of thoughts, at the same time I am not exaggerating anything about your post.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: A minilanguage with the least effort?
by Your Mother (Canon) on Feb 18, 2009 at 00:29 UTC
Re: A minilanguage with the least effort?
by NetWallah (Abbot) on Feb 18, 2009 at 00:42 UTC
    A great example of a language implemented in perl is LOLCODE implemented using the Parse::RecDescent module, called yali. There is also a perl6 implementation - it seems to be one of the languages released with Parrot.

         ..to maintain is to slowly feel your soul, sanity and sentience ebb away as you become one with the Evil.

Re: A minilanguage with the least effort?
by planetscape (Canon) on Feb 18, 2009 at 04:41 UTC

    I just came across this article in January's ACM Queue online magazine:

    Mike Shapiro: "Purpose-Built Languages."

    Tagline:

    While often breaking the rules of traditional language design, the growing ecosystem of purpose-built “little” languages is an essential part of systems development.

    HTH,

    planetscape
Re: A minilanguage with the least effort?
by cutlass2006 (Pilgrim) on Feb 19, 2009 at 10:53 UTC

    if xml is ok as syntax, then I have found using XML::Descent to be simple enough to grok and use.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://744394]
Approved by Corion
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (10)
As of 2014-09-23 22:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (241 votes), past polls