📄 logikusparser.java
字号:
a.add(new Symbol('.'));
return a;
}
/**
* Return a parser that recognizes the grammar:
*
* <blockquote><pre>
* list = '[' (listContents | Empty) ']';
* </pre></blockquote>
*
* The class comment gives the complete grammar for lists,
* as part of the Logikus grammar.
*
* @return a parser that recognizes a list
*/
public Sequence list() {
if (list == null) {
list = new Track("list");
list.add(new Symbol('[')); // push this, as a fence
Alternation a = new Alternation();
a.add(listContents());
a.add(new Empty().setAssembler(
new ListAssembler()));
list.add(a);
list.add(new Symbol(']').discard());
}
return list;
}
/**
* Return a parser that recognizes the grammar:
*
* listContents = commaList(term) listTail;
*/
protected Parser listContents() {
Sequence s = commaList(term());
s.add(listTail());
return s;
}
/**
* Return a parser that recognizes the grammar:
*
* listTail = ('|' (variable | list)) | Empty;
*/
protected Parser listTail() {
Alternation tail = new Alternation();
tail.add(variable());
tail.add(list());
Track barTail = new Track("bar tail");
barTail.add(new Symbol('|').discard());
barTail.add(tail);
barTail.setAssembler(new ListWithTailAssembler());
Alternation a = new Alternation();
a.add(barTail);
a.add(new Empty().setAssembler(new ListAssembler()));
return a;
}
/**
* Return a parser that recognizes the grammar:
*
* minusPhrase = '-' phrase;
*/
protected Parser minusPhrase() {
Sequence s = new Sequence("minusPhrase");
s.add(new Symbol('-').discard());
s.add(phrase());
s.setAssembler(new ArithmeticAssembler('-'));
return s;
}
/**
* Return a parser that recognizes the grammar:
*
* not = "not" structure;
*/
protected Parser not() {
Track t = new Track("not");
t.add(new Literal("not").discard());
t.add(structure());
t.setAssembler(new NotAssembler());
return t;
}
/**
* Return a parser that recognizes a number and stacks a corresponding atom.
*/
public Parser num() {
Parser n = new Num();
n.setAssembler(new AtomAssembler());
return n;
}
/**
* Return a parser that recognizes the grammar:
*
* operator = '<' | '>' | '=' | "<=" | ">=" | "!=" ;
*/
protected Parser operator() {
Alternation a = new Alternation("operator");
a.add(new Symbol('<'));
a.add(new Symbol('>'));
a.add(new Symbol('='));
a.add(new Symbol("<="));
a.add(new Symbol(">="));
a.add(new Symbol("!="));
return a;
}
/**
* Return a parser that recognizes the grammar:
*
* phrase = factor ('*' factor | '/' factor)*;
*/
protected Parser phrase() {
Sequence phrase = new Sequence("phrase");
phrase.add(factor());
Alternation a = new Alternation();
a.add(timesFactor());
a.add(divideFactor());
phrase.add(new Repetition(a));
return phrase;
}
/**
* Return a parser that recognizes the grammar:
*
* plusPhrase = '+' phrase;
*/
protected Parser plusPhrase() {
Sequence s = new Sequence("plusPhrase");
s.add(new Symbol('+').discard());
s.add(phrase());
s.setAssembler(new ArithmeticAssembler('+'));
return s;
}
/**
* Return a parser that recognizes the grammar:
*
* <blockquote><pre>
* query = commaList(condition);
* </pre></blockquote>
*
* @return a parser that recognizes a query
*/
public static Parser query() {
Parser p = commaList(new LogikusParser().condition());
p.setAssembler(new AxiomAssembler());
return p;
}
/**
* Return a parser that recognizes the grammar:
*
* ruleDef = ":-" commaList(condition);
*/
protected Parser ruleDef() {
Track t = new Track("rule definition");
t.add(new Symbol(":-").discard());
t.add(commaList(condition()));
return t;
}
/**
* Return a parser that recognizes the grammar:
*
* <blockquote><pre>
* axiom = condition (ruleDefinition | empty);
* </pre></blockquote>
*
* @return a parser that recognizes an axiom
*/
public static Parser start() {
return new LogikusParser().axiom();
}
/**
* Return a parser that recognizes a number and stacks a corresponding atom.
*/
public Parser string() {
Parser str = new QuotedString();
str.setAssembler(new AtomAssembler());
return str;
}
/**
* Return a parser that recognizes the grammar:
*
* structure = functor ('(' commaList(term) ')');
*
* This definition of structure accounts for normal-looking
* structures that have a string as a functor. Strictly
* speaking, numbers and lists are also structures. The
* definition for <code>term</code> includes these.
*/
protected Parser structure() {
if (structure == null) {
structure = new Sequence("structure");
Sequence s = new Sequence("s");
structure.add(functor());
Track t = new Track("list in parens");
t.add(new Symbol('(')); // push this as a fence
t.add(commaList(term()));
t.add(new Symbol(')').discard());
Alternation a = new Alternation();
a.add(t.setAssembler(new StructureWithTermsAssembler()));
// no longer allowing the empty structure, handling variables differently
// a.add(new Empty().setAssembler(new AtomAssembler()));
structure.add(a);
}
return structure;
}
/**
* Return a parser that recognizes the grammar:
*
* term = structure | Num | QuotedString | list | variable;
*/
protected Parser term() {
Alternation a = new Alternation("term");
a.add(structure());
a.add(num());
a.add(string());
a.add(list());
a.add(variable());
return a;
}
/**
* Return a parser that recognizes the grammar:
*
* timesFactor = '*' factor;
*/
protected Parser timesFactor() {
Sequence s = new Sequence("timesFactor");
s.add(new Symbol('*').discard());
s.add(factor());
s.setAssembler(new ArithmeticAssembler('*'));
return s;
}
/**
* Return a parser that recognizes the grammar:
*
* variable = LowercaseWord | UppercaseWord | '_';
*
* The underscore represents and will translate to an
* anonymous variable.
*/
protected Parser variable() {
// Parser word = new Word();
// word.setAssembler(new VariableAssembler());
Parser lowerCase = new LowercaseWord();
lowerCase.setAssembler(new VariableAssembler());
Parser upperCase = new UppercaseWord();
upperCase.setAssembler(new VariableAssembler());
Parser anon = new Symbol('_').discard();
anon.setAssembler(new AnonymousAssembler());
Alternation a = new Alternation();
// a.add(word);
a.add(lowerCase);
a.add(upperCase);
a.add(anon);
return a;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -