00001
00025 #include <stdio.h>
00026 #include "magic/mlsystem.h"
00027
00028
00029
00031
00032
00033
00034
00035
00036
00037
00039
00040 void LGrammar::addRule (const String& left, const String& right) {
00041 rules.add (new LRule (left, right));
00042 }
00043
00044 void LGrammar::applyTo (String& str, int times) const {
00045 for (int t=0; t<times; t++) {
00046 String nextstr = "";
00047
00048 for (uint pos=0; pos < str.length(); pos++) {
00049
00050 bool matched = false;
00051 for (int rule=0; rule<rules.size(); rule++) {
00052 const String& left = rules[rule].left;
00053 if (str.mid (pos, left.length()) == left) {
00054
00055 nextstr += rules[rule].right;
00056 matched = true;
00057 break;
00058 }
00059 }
00060
00061
00062 if (!matched)
00063 nextstr += str.mid (pos, 1);
00064 }
00065 str = nextstr;
00066 }
00067 }
00068