use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class TestInterpret5 method main.
public static void main(String[] args) {
try {
String[] codes = new String[] { "import \"sys.base\";\n" + "var to_list = func ~(a, b, c) {\n" + " foreach (var i : call a(b, c)) {\n" + " call g_printn(i);\n" + " }\n" + "};\n" + "call to_list(\"g_range\", 1, 100);\n" + "//call g_range(1, 6);\n" + "\n", "import \"sys.base\";\n" + "var a = 1;" + "if (a == 1) {" + " call g_printn('x');" + "} else if (a == 2) {" + " call g_printn('y');" + "}" + "\n" };
Interpreter interpreter = new Interpreter();
Grammar grammar = new Grammar(codes[codes.length - 1]);
System.out.println(grammar.toString());
RuntimeCodePage page = grammar.getCodePage();
System.out.println(page.toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
RuntimeCodePage.exportFromStream(page, baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
interpreter.run("test_1", bais);
} catch (RegexException e) {
System.err.println();
System.err.println(e.getPosition() + "," + e.getMessage());
e.printStackTrace();
} catch (SyntaxException e) {
System.err.println();
System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
e.printStackTrace();
} catch (RuntimeException e) {
System.err.println();
System.err.println(e.getPosition() + ": " + e.getInfo());
e.printStackTrace();
} catch (Exception e) {
System.err.println();
System.err.println(e.getMessage());
e.printStackTrace();
}
}
use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class TestInterpret8 method main.
public static void main(String[] args) {
try {
String[] codes = new String[] { "import \"sys.base\"; import \"sys.list\";\n" + "var a = [];\n" + "call g_array_add(a, 5);\n" + "call g_array_set(a, 0, 4);\n" + "call g_printn(call g_array_get(a, 0));\n" + "call g_array_remove(a, 0);\n" + "call g_array_add(a, 50);\n" + "call g_array_add(a, 100);\n" + "call g_array_set(a, 1, 400);\n" + "call g_printn(call g_array_get(a, 1));\n" + "call g_array_pop(a);\n" + "call g_array_pop(a);\n" + "call g_printn(call g_array_size(a));\n" + "\n" + "let a = {};\n" + "call g_map_put(a, \"x\", 5);\n" + "call g_map_put(a, \"y\", 10);\n" + "call g_map_put(a, \"x\", 50);\n" + "call g_printn(call g_map_size(a));\n" + "call g_printn(call g_map_get(a, \"x\"));\n" + "call g_printn(call g_map_get(a, \"y\"));\n" + "call g_printn(call g_map_contains(a, \"x\"));\n" + "call g_map_remove(a, \"x\");\n" + "call g_printn(call g_map_contains(a, \"x\"));\n" + "call g_printn(call g_map_size(a));\n" + "\n", "import \"sys.base\"; import \"sys.list\";\n" + "var create_node = func ~(data) {\n" + " var new_node = g_new_map;\n" + " call g_map_put(new_node, \"data\", data);\n" + " call g_map_put(new_node, \"prev\", g_null);\n" + " call g_map_put(new_node, \"next\", g_null);\n" + " return new_node;\n" + "};\n" + "var append = func ~(head, obj) {\n" + " var new_node = call create_node(obj);\n" + " call g_map_put(new_node, \"next\", head);\n" + " call g_map_put(head, \"prev\", new_node);\n" + " return new_node;" + "};\n" + "var head = call create_node(0);\n" + "foreach (var i : call g_range(1, 10)) {\n" + " let head = call append(head, i);\n" + "}\n" + "var p = head;\n" + "while (!call g_is_null(p)) {\n" + " call g_printn(call g_map_get(p, \"data\"));\n" + " let p = call g_map_get(p, \"next\");\n" + "}\n" + "\n" };
System.out.println(codes[codes.length - 1]);
Interpreter interpreter = new Interpreter();
Grammar grammar = new Grammar(codes[codes.length - 1]);
// System.out.println(grammar.toString());
RuntimeCodePage page = grammar.getCodePage();
// System.out.println(page.toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
RuntimeCodePage.exportFromStream(page, baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
interpreter.run("test_1", bais);
} catch (RegexException e) {
System.err.println();
System.err.println(e.getPosition() + "," + e.getMessage());
e.printStackTrace();
} catch (SyntaxException e) {
System.err.println();
System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
e.printStackTrace();
} catch (RuntimeException e) {
System.err.println();
System.err.println(e.getPosition() + ": " + e.getInfo());
e.printStackTrace();
} catch (Exception e) {
System.err.println();
System.err.println(e.getMessage());
e.printStackTrace();
}
}
use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class Syntax method doTail.
/**
* 处理右端表达式
*
* @throws SyntaxException 词法错误
*/
private void doTail() throws SyntaxException {
/* 获得分析后的表达式根结点 */
ISyntaxComponent exp = doAnalysis(TokenType.EOF, null);
/* 将根结点添加进对应规则 */
RuleItem item = new RuleItem(exp, rule.rule);
onAddRuleItem(item);
rule.rule.arrRules.add(item);
}
use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class Syntax method semanticAnalysis.
/**
* 进行语义分析
*
* @throws SyntaxException 词法错误
*/
private void semanticAnalysis() throws SyntaxException {
/* 非终结符数量 */
int size = arrNonTerminals.size();
/* 计算规则的First集合 */
for (RuleExp exp : arrNonTerminals) {
for (RuleItem item : exp.rule.arrRules) {
FirstsetSolver solver = new FirstsetSolver();
// 计算规则的First集合
item.expression.visit(solver);
if (!solver.solve(item)) {
// 若串长度可能为零,即产生空串
err(SyntaxError.EPSILON, getSingleString(exp.name, item.expression));
}
}
}
/* 建立连通矩阵 */
// First集依赖矩阵
BitVector2 firstsetDependency = new BitVector2(size, size);
firstsetDependency.clear();
/* 计算非终结符First集合包含关系的布尔连通矩阵 */
{
int i = 0;
for (RuleExp exp : arrNonTerminals) {
for (RuleItem item : exp.rule.arrRules) {
for (RuleExp rule : item.setFirstSetRules) {
firstsetDependency.set(i, rule.id);
}
}
i++;
}
}
/* 计算间接左递归 */
{
/* 标记并清除直接左递归 */
for (int i = 0; i < size; i++) {
if (firstsetDependency.test(i, i)) {
// 直接左递归
arrNonTerminals.get(i).rule.iRecursiveLevel = 1;
firstsetDependency.clear(i, i);
}
}
/* 获得拷贝 */
BitVector2 a = (BitVector2) firstsetDependency.clone();
BitVector2 b = (BitVector2) firstsetDependency.clone();
BitVector2 r = new BitVector2(size, size);
/* 检查是否出现环 */
for (int level = 2; level < size; level++) {
/* 进行布尔连通矩阵乘法,即r=aXb */
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
r.clear(i, j);
for (int k = 0; k < size; k++) {
boolean value = r.test(i, j) || (a.test(i, k) && b.test(k, j));
r.set(i, j, value);
}
}
}
/* 检查当前结果是否出现环 */
{
int i = 0;
for (RuleExp exp : arrNonTerminals) {
if (r.test(i, i)) {
if (exp.rule.iRecursiveLevel < 2) {
exp.rule.iRecursiveLevel = level;
}
}
i++;
}
}
/* 保存结果 */
a = (BitVector2) r.clone();
}
/* 检查是否存在环并报告错误 */
for (RuleExp exp : arrNonTerminals) {
if (exp.rule.iRecursiveLevel > 1) {
err(SyntaxError.INDIRECT_RECURSION, exp.name + " level:" + exp.rule.iRecursiveLevel);
}
}
}
/* 计算完整的First集合 */
{
/* 建立处理标记表 */
BitSet processed = new BitSet(size);
processed.clear();
for (RuleExp arrNonTerminal : arrNonTerminals) {
/* 找出一条无最左依赖的规则 */
// 最左依赖的规则索引
int nodependencyRule = -1;
for (int j = 0; j < size; j++) {
if (!processed.get(j)) {
boolean empty = true;
for (int k = 0; k < size; k++) {
if (firstsetDependency.test(j, k)) {
empty = false;
break;
}
}
if (empty) {
// 找到
nodependencyRule = j;
break;
}
}
}
if (nodependencyRule == -1) {
err(SyntaxError.MISS_NODEPENDENCY_RULE, arrNonTerminal.name);
}
/* 计算该规则的终结符First集合 */
{
Rule rule = arrNonTerminals.get(nodependencyRule).rule;
/* 计算规则的终结符First集合 */
for (RuleItem item : rule.arrRules) {
for (RuleExp exp : item.setFirstSetRules) {
item.setFirstSetTokens.addAll(exp.rule.arrTokens);
}
}
/* 计算非终结符的终结符First集合 */
for (RuleItem item : rule.arrRules) {
rule.arrTokens.addAll(item.setFirstSetTokens);
}
/* 修正左递归规则的终结符First集合 */
for (RuleItem item : rule.arrRules) {
if (item.setFirstSetRules.contains(arrNonTerminals.get(nodependencyRule))) {
item.setFirstSetTokens.addAll(rule.arrTokens);
}
}
}
/* 清除该规则 */
processed.set(nodependencyRule);
for (int j = 0; j < size; j++) {
firstsetDependency.clear(j, nodependencyRule);
}
}
}
/* 搜索不能产生字符串的规则 */
for (RuleExp exp : arrNonTerminals) {
for (RuleItem item : exp.rule.arrRules) {
if (item.setFirstSetTokens.isEmpty()) {
err(SyntaxError.FAILED, getSingleString(exp.name, item.expression));
}
}
}
}
use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class TestInterpret method main.
public static void main(String[] args) {
try {
String[] codes = new String[] { "import \"sys.base\";" + "call g_print(call g_is_null(g_null));", "import \"sys.base\";" + "call g_print(\"Hello World!\\\n\");" + "call g_print_err(\"Hello World!\\\n\");", "import \"sys.base\";\n" + "call g_print(\"请输入:\");\n" + "call g_print(call g_stdin_read_line() + g_endl);\n" + "call g_print(\"输入两个数字:\");\n" + "call g_print(\n" + " \"较大数是:\" +" + " call g_to_string(\n" + " call g_max(call g_stdin_read_int(), call g_stdin_read_int()))\n" + " + \"\\n\"\n" + ");\n" + "call g_print(\"输入两个数字:\");\n" + "call g_print(\n" + " \"较小数是:\" +" + " call g_to_string(\n" + " call g_min(call g_stdin_read_int(), call g_stdin_read_int()))\n" + " + \"\\n\"\n" + ");\n", "import \"sys.base\";\n" + "call g_print(call g_doc(\"g_author\") + g_endl);\n" + "call g_print(call g_doc(\"g_print\") + g_endl);\n" + "call g_print(call g_doc(\"g_stdin_read_int\") + g_endl);\n" + "call g_print(call g_doc(\"g_new\") + g_endl);\n" + "call g_print(call g_to_string(call g_new(g_endl)));\n" + "call g_print(call g_to_string(call g_new(5)));\n" // "import \"sys.base\";\n"
// + "var f = func ~(n) ->\n"
// + " n <= 2 ? 1 : call f(n-1) + call f(n-2);"
// + "call g_print(call f(6));\n"
// ,
// "import \"sys.base\";\n"
// + "call g_print(call g_load_func(\"g_max\"));\n"
// ,
};
Interpreter interpreter = new Interpreter();
Grammar grammar = new Grammar(codes[codes.length - 1]);
System.out.println(grammar.toString());
RuntimeCodePage page = grammar.getCodePage();
System.out.println(page.toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
RuntimeCodePage.exportFromStream(page, baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
interpreter.run("test_1", bais);
} catch (RegexException e) {
System.err.println();
System.err.println(e.getPosition() + "," + e.getMessage());
e.printStackTrace();
} catch (SyntaxException e) {
System.err.println();
System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
e.printStackTrace();
} catch (RuntimeException e) {
System.err.println();
System.err.println(e.getPosition() + ": " + e.getInfo());
e.printStackTrace();
} catch (Exception e) {
System.err.println();
System.err.println(e.getMessage());
e.printStackTrace();
}
}
Aggregations