mhulden / foma

Automatically exported from code.google.com/p/foma
115 stars 90 forks source link

revert and longest match #132

Open arademaker opened 3 years ago

arademaker commented 3 years ago

Following the video https://youtu.be/F8F422GldwY, I could reproduce the same behaviour but I really didn't understand why the reverse of the Alphabet worked in the InsertBrackets rule.

For instance, if I got it right, the InsertBrackets will be looking for the sequence hg in angha? I suppose there is some hidden interaction between the @-> and the .r operator not described in the video.. Am I missing something?

define Alphabet [ {a} | {g} | {n} | {ng} | {gh} ];
define InsertBrackets [ Alphabet.r @-> %} ... %{ ].r;
 foma[1]: down ana
{a}{n}{a}
foma[1]: down angha
{a}{n}{gh}{a}
mhulden commented 3 years ago

No hidden interaction. The rule is compiled as left-to-right, then the resulting transducer is reversed (yielding right-to-left longest-match). The alphabet transducer is also reversed to make it all work. The alphabet could have been defined explicitly like the below (without reversal):

def Alphabet [a | g | n | {gn} | {hg}];
def InsertBrackets [ Alphabet @-> %} ... %{  ].r;

yielding the same result.