use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.
the class TestRegex method main.
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
Regex ra = new Regex(str, true);
String context = scanner.nextLine();
String match = ra.match(context);
if (match == null) {
System.err.println("failed");
} else {
System.out.println(match);
}
scanner.close();
} catch (RegexException e) {
System.err.println(e.getPosition() + "," + e.getMessage());
e.printStackTrace();
}
}
use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.
the class CharacterFilter method filter.
@Override
public RegexStringIteratorData filter(IRegexStringIterator iterator) {
// 获取解析组件
RegexStringUtility utility = iterator.utility();
RegexStringIteratorData data = new RegexStringIteratorData();
try {
if (!iterator.available()) {
data.kMeta = MetaType.END;
data.chCurrent = MetaType.END.getChar();
} else {
data.kMeta = iterator.meta();
data.chCurrent = iterator.current();
iterator.next();
if (data.kMeta == MetaType.SINGLE_QUOTE) {
// 过滤单引号
data.kMeta = MetaType.NULL;
} else if (data.kMeta == MetaType.ESCAPE) {
// 处理转义
data.chCurrent = iterator.current();
iterator.next();
data.kMeta = MetaType.MUST_SAVE;
data.chCurrent = utility.fromEscape(data.chCurrent, RegexError.ESCAPE);
}
}
} catch (RegexException e) {
System.err.println(e.getPosition() + " : " + e.getMessage());
data.kMeta = MetaType.ERROR;
data.chCurrent = MetaType.ERROR.getChar();
}
return data;
}
use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.
the class TestSyntaxLexer method main.
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
SyntaxLexer lexer = new SyntaxLexer();
lexer.setContext(str);
Token token;
for (; ; ) {
token = lexer.nextToken();
if (token.kToken == TokenType.EOF) {
break;
}
System.out.println(token.toString());
}
scanner.close();
} catch (RegexException e) {
System.err.println(e.getPosition() + "," + e.getMessage());
e.printStackTrace();
}
}
use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.
the class TestGrammar method main.
public static void main(String[] args) {
// System.out.println("Z -> `a`<,> | B | [`a` `b` Z B]");
try {
// Scanner scanner = new Scanner(System.in);
// Grammar grammar = new Grammar("(i * i) * (i + i) - i");
Grammar grammar = new Grammar("i + i * i");
grammar.addTerminal("SYMBOL", TokenType.ID, "i");
grammar.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
grammar.addTerminal("MINUS", TokenType.OPERATOR, OperatorType.MINUS);
grammar.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
grammar.addTerminal("DIVIDE", TokenType.OPERATOR, OperatorType.DIVIDE);
grammar.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
grammar.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
grammar.setEpsilonName("epsilon");
String[] nons = new String[] { "E", "E1", "T", "T1", "F", "A", "M" };
for (String non : nons) {
grammar.addNonTerminal(non);
}
grammar.infer("E -> T E1");
grammar.infer("E1 -> A T E1 | @epsilon");
grammar.infer("T -> F T1");
grammar.infer("T1 -> M F T1 | @epsilon");
grammar.infer("F -> @LPA E @RPA | @SYMBOL");
grammar.infer("A -> @PLUS | @MINUS");
grammar.infer("M -> @TIMES | @DIVIDE");
grammar.initialize("E");
System.out.println(grammar.toString());
System.out.println(grammar.getPredictionString());
grammar.run();
System.out.println(grammar.getTokenString());
// 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();
} catch (GrammarException e) {
System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
e.printStackTrace();
}
}
use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.
the class TestGrammar2 method main.
public static void main(String[] args) {
// System.out.println("Z -> `a`<,> | B | [`a` `b` Z B]");
try {
// Scanner scanner = new Scanner(System.in);
Grammar syntax = new Grammar("b b b b ");
syntax.addTerminal("a", TokenType.ID, "a");
syntax.addTerminal("b", TokenType.ID, "b");
syntax.setEpsilonName("epsilon");
String[] nons = new String[] { "S", "A", "B" };
for (String non : nons) {
syntax.addNonTerminal(non);
}
syntax.infer("S -> A B A B");
syntax.infer("A -> @a A | @epsilon");
syntax.infer("B -> @b B | @epsilon");
syntax.initialize("S");
System.out.println(syntax.toString());
System.out.println(syntax.getPredictionString());
syntax.run();
System.out.println(syntax.getTokenString());
// 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();
} catch (GrammarException e) {
System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
e.printStackTrace();
}
}
Aggregations