use of priv.bajdcc.LL1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class TestSyntax2 method main.
public static void main(String[] args) {
try {
// Scanner scanner = new Scanner(System.in);
Syntax syntax = new Syntax();
syntax.addTerminal("a", TokenType.ID, "a");
syntax.addTerminal("b", TokenType.ID, "b");
syntax.addNonTerminal("Z");
syntax.addNonTerminal("S");
syntax.addNonTerminal("B");
syntax.addErrorHandler("sample", null);
syntax.infer("Z -> S");
syntax.infer("S -> B B");
syntax.infer("B -> @a B");
syntax.infer("B -> @b");
syntax.initialize("Z");
System.out.println(syntax.toString());
System.out.println(syntax.getNGAString());
System.out.println(syntax.getNPAString());
// scanner.close();
} catch (RegexException e) {
System.err.println(e.getPosition() + "," + e.getMessage());
e.printStackTrace();
} catch (SyntaxException e) {
System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
e.printStackTrace();
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class TestSyntax3 method main.
public static void main(String[] args) {
try {
// Scanner scanner = new Scanner(System.in);
Syntax syntax = new Syntax();
syntax.addTerminal("a", TokenType.ID, "a");
syntax.addTerminal("c", TokenType.ID, "c");
syntax.addTerminal("d", TokenType.ID, "d");
syntax.addNonTerminal("S");
syntax.addNonTerminal("A");
syntax.addErrorHandler("sample", null);
syntax.infer("S -> @c A @d");
syntax.infer("A -> @a");
syntax.infer("A -> A @a");
syntax.initialize("S");
System.out.println(syntax.toString());
System.out.println(syntax.getNGAString());
System.out.println(syntax.getNPAString());
// scanner.close();
} catch (RegexException e) {
System.err.println(e.getPosition() + "," + e.getMessage());
e.printStackTrace();
} catch (SyntaxException e) {
System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
e.printStackTrace();
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class UIMainFrame method startOS.
private void startOS(UIGraphics g) {
IOSCodePage[] pages = new IOSCodePage[] { // OS
new OSEntry(), new OSIrq(), new OSTask(), // IRQ
new IRPrint(), new IRRemote(), new IRTask(), new IRSignal(), // TASK
new TKSystem(), new TKUtil(), new TKUI(), new TKNet(), // UI
new UIMain(), new UIClock(), new UIHitokoto(), new UIMonitor(), // USER
new UserMain(), // USER ROUTINE
new URShell(), new UREcho(), new URPipe(), new URDup(), new URGrep(), new URRange(), new URProc(), new URTask(), new URSleep(), new URTime(), new URCount(), new URTest(), new URMsg(), new URNews(), new URBash(), new URReplace(), new URUtil(), new URAI(), // USER FILE
new URFileLoad(), new URFileSave(), new URFileAppend() };
try {
String code = "import \"sys.base\";\n" + "import \"sys.proc\";\n" + "call g_load_sync_x(\"/kern/entry\");\n";
interpreter = new Interpreter();
for (IOSCodePage page : pages) {
interpreter.load(page);
}
Grammar grammar = new Grammar(code);
// 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("@main", 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(String.format("模块名:%s. 位置:%s. 错误:%s-%s(%s:%d)", e.getPageName(), e.getPosition(), e.getMessage(), e.getInfo(), e.getFileName(), e.getPosition().iLine + 1));
e.printStackTrace();
} catch (RuntimeException e) {
System.err.println();
System.err.println(e.getkError().getMessage() + " " + e.getPosition() + ": " + e.getInfo());
e.printStackTrace();
} catch (Exception e) {
System.err.println();
System.err.println(e.getMessage());
e.printStackTrace();
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class Syntax method addNonTerminal.
/**
* 添加非终结符
*
* @param name
* 非终结符名称
* @throws SyntaxException 词法错误
*/
public void addNonTerminal(String name) throws SyntaxException {
RuleExp exp = new RuleExp(arrNonTerminals.size(), name);
if (!mapNonTerminals.containsKey(name)) {
mapNonTerminals.put(name, exp);
arrNonTerminals.add(exp);
} else {
err(SyntaxError.REDECLARATION);
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class Syntax method doAnalysis.
/**
* 分析表达式
*
* @param type
* 结束类型
* @param obj
* 结束数据
* @return 表达式树根结点
* @throws SyntaxException 词法错误
*/
private ISyntaxComponent doAnalysis(TokenType type, Object obj) throws SyntaxException {
/* 新建序列用于存放结果 */
SequenceExp sequence = new SequenceExp();
/* 可能会使用的分支 */
BranchExp branch = null;
/* 添加子结点接口 */
IExpCollction collection = sequence;
/* 表达式通用接口 */
ISyntaxComponent result = sequence;
for (; ; ) {
if ((token.kToken == type && (token.object == null || token.object.equals(obj)))) {
// 结束字符
if (syntaxLexer.index() == 0) {
// 表达式为空
err(SyntaxError.NULL);
} else if (collection.isEmpty()) {
// 部件为空
err(SyntaxError.INCOMPLETE);
} else {
next();
// 正常终止
break;
}
} else if (token.kToken == TokenType.EOF) {
err(SyntaxError.INCOMPLETE);
}
// 当前待赋值的表达式
ISyntaxComponent exp = null;
switch(token.kToken) {
case OPERATOR:
OperatorType op = (OperatorType) token.object;
next();
switch(op) {
case ALTERNATIVE:
if (// 在此之前没有存储表达式 (|...)
collection.isEmpty()) {
err(SyntaxError.INCOMPLETE);
} else {
if (branch == null) {
// 分支为空,则建立分支
branch = new BranchExp();
// 用新建的分支包含并替代当前序列
branch.add(sequence);
result = branch;
}
// 新建一个序列
sequence = new SequenceExp();
branch.add(sequence);
continue;
}
break;
default:
err(SyntaxError.SYNTAX);
break;
}
break;
case EOF:
return result;
case TERMINAL:
exp = matchTerminal();
next();
break;
case NONTERMINAL:
exp = matchNonTerminal();
next();
break;
default:
err(SyntaxError.SYNTAX);
break;
}
if (exp != null) {
sequence.add(exp);
}
}
return result;
}
Aggregations