/* * A TRUE EM parser using dependent agents. Version B. * [Ant 23/10/2002] */ writeln("**SCRIPTBEGIN**"); /* rule definitions */ expr = [ ["pivot", "+", "expr", "expr"], ["pivot", "-", "expr", "expr"], ["literal", "id"] ]; mygrammer is [expr]; /* agent count */ ag_count = 0; /* agent function */ func agent { para ag_str, ag_rule; auto i, j, ag_name, script, success, rule_desc, result; script = []; success = FALSE; writeln("**begin agent: ", ag_str, ", ", ag_rule, "**"); /* script = script // script_before */ rule_desc = getvar(ag_rule); for (i = 1; i <= rule_desc#; i++ ) { success = TRUE; writeln("**attempt match: ",rule_desc[i][1],"**"); /* apply rule to ag_str */ switch (rule_desc[i][1]) { case "literal": if (ag_str == rule_desc[i][2]) result = []; else result = "fail"; break; case "pivot": result = pivot(ag_str, rule_desc[i][2], []); break; default: /* error in notation */ result = "fail"; } if (result == "fail") continue; writeln("**template match**"); /* spawn the child agents */ for (j = 1; j <= result#; j++ ) { ag_name = "ag_" // str(ag_count++); writeln(ag_name,",",ag_str,",",ag_rule,",",rule_desc[i]); execute(ag_name // "_str = \"" // result[j] // "\";"); execute(ag_name // "_rule = \"" // rule_desc[i][j+2] // "\";"); execute(ag_name // " is agent(" // ag_name // "_str, " // ag_name // "_rule);"); if (getvar(ag_name) == "fail") { success = FALSE; break; } } if (success) break; } writeln("**finish agent**"); /* script = script // script_later */ if (success) return script; else return "fail"; } func pivot { para instr, pivot, ignore; auto i; i = extract (instr, pivot, ignore, 1); if (i == 0) return "fail"; else return [substr(instr, 1, i - 1), substr(instr, i + pivot#, instr#)]; } myresult = agent("id+id", "expr");