all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Developing a preprocessor for Arduino
Date: Wed, 11 May 2016 20:22:55 +0200	[thread overview]
Message-ID: <87d1oszc74.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.2653.1462980488.7477.help-gnu-emacs@gnu.org

csanyipal@gmail.com (Csányi Pál) writes:

> Hi,
>
> I wish to develope a preprocessor for the Arduino programming language.
> http://labs.arduino.org/Programming+Language
>
> I wish to write this preprocessor in Emacs Lisp.
> I imagine that that my pupils write the code in Emacs in their native
> language.
> So, eg. the Arduino command
> setup() will be
> beállít()
> .
>
> When they finished the program code, they run preprocessor.
> The preprocessor should convert these commands in to English Arduino commands.
>
> How should I start this project?

Yuri is mostly right.

However, one way to do it would be to write down the grammar of the
language, and extract all the tokens you want to translate in their own
non-terminal rule.

Then you can substitute translations of the tokens in those isolated
rules.

You can then translate in both directions, with:

   p1 = (generate g1 (parse g2 p2))
   p2 = (generate g2 (parse g1 p1))

The point here is that the syntactic trees returned by the (parse g2)
and (parse g1) functions are identical down to the terminals isolated in
those simple translated rules), so they can easily be processed by the
generate function of the other grammar, and you get bidirectional
translation for free.

This neutralize one objection thought by Yuri: your students now will be
able to read and maintain foreign code.

Also, if you take care to distinguish non-terminals for homonyms, you
can translate to the correct word.


For example for the language containing those two sentences:

             Move to London.
             Move the apple on the table.

    g1: start     ::= sentence1 | sentence2 .
        sentence1 ::= ntMove1 ntTo ntLondon .
        sentence2 ::= ntMove2 ntThe1 ntApple ntOn ntThe2 ntTable .
        ntMove1   ::= 'Move'   .
        ntTo      ::= 'to'     .
        ntLondon  ::= 'London' .
        ntMove2   ::= 'Move'   .
        ntThe     ::= 'the'    .
        ntApple   ::= 'apple'  .
        ntOn      ::= 'on'     .
        ntThe     ::= 'the'    .
        ntTable   ::= 'table'  .

    g2: start     ::= sentence1 | sentence2 .
        sentence1 ::= ntMove1 ntTo ntLondon .
        sentence2 ::= ntMove2 ntThe ntApple ntOn ntThe ntTable .
        ntMove1   ::= 'Déménager' .
        ntTo      ::= 'à'         .
        ntLondon  ::= 'Londre'    .
        ntMove2   ::= 'Déplacer'  .
        ntThe1    ::= 'la'        .
        ntApple   ::= 'pomme'     .
        ntOn      ::= 'sur'       .
        ntThe2    ::= 'la'        .
        ntTable   ::= 'table'     .

    
you can see that ntMove1 and ntMove2 don't translate to the same French
word.


For a programming language that should be enough.  However, if the
language is verbose you may want to have also rule transformations.  In
the example above we were lucky that a word-for-word translation
worked.  But with natural languages, there may be different (small) word
numbers, and changes in the order of the matching words, when
translating.

You could have rules such as:

    g1:  rule1 ::= ntA ntB ntX.1 ntC ntX.2 ntD ntY ntE .
    g2:  rule1 ::= ntA ntCD ntY ntB1 ntX ntB2.

where the non-terminals ntB ntC ntD and ntE disappear, ntC and ntD being
translated by a single word ntCD, and ntB being translated by two words
ntB1 and ntB2, and the order of ntX and ntY changes.

In those cases, you would have to edit the rule in the new grammar, and
you could transform the syntactic tree by implementing the substitutions
and permutations infered from the two grammar rules.

    g1:  rule1 ::= ntA ntB ntX ntC ntD ntY ntE .
    g2:  rule1 ::= ntA ntY ntB1 ntX.1 ntCD ntX.2 ntB2.

For this to work, you will have to normalize the grammar so that each
alternative is in its own rule, so you dont' have ambiguity when
matching rules in the other language.

You will have to add a (transform g1 g2) function implementing the parse
tree transformations:

   p1 = (generate g1 (transform g1 g2 (parse g2 p2)))
   p2 = (generate g2 (transform g2 g1 (parse g1 p1)))


So you can see that you can easily translate (again, bidirectionnaly) by
writing only three simple functions; generate, transform and parse.




-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


       reply	other threads:[~2016-05-11 18:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.2653.1462980488.7477.help-gnu-emacs@gnu.org>
2016-05-11 18:22 ` Pascal J. Bourguignon [this message]
2016-05-11  5:40 Developing a preprocessor for Arduino Csányi Pál
2016-05-11 15:35 ` Yuri Khan

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

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

  git send-email \
    --in-reply-to=87d1oszc74.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.