From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: Developing a preprocessor for Arduino Date: Wed, 11 May 2016 20:22:55 +0200 Organization: Informatimago Message-ID: <87d1oszc74.fsf@kuiper.lan.informatimago.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1464881133 8808 80.91.229.3 (2 Jun 2016 15:25:33 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 2 Jun 2016 15:25:33 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jun 02 17:25:25 2016 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1b8UUs-0007WY-It for geh-help-gnu-emacs@m.gmane.org; Thu, 02 Jun 2016 17:25:22 +0200 Original-Received: from localhost ([::1]:48015 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b8UUr-0006Wg-NB for geh-help-gnu-emacs@m.gmane.org; Thu, 02 Jun 2016 11:25:21 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 128 Original-X-Trace: individual.net Ltp0bPM4Hp1JYCR5WNcBGg1w5ZepmVYaZIwUbYktoj0q8BhoM/ Cancel-Lock: sha1:NDBlNTkyOTUwNDk3NjVkOWM2YzBmYTVhZWYyNmZiYjhmYzU1MjEzOQ== sha1:4nBZMXWOESRjazshUYITM2hYh9s= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:217675 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:110172 Archived-At: 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