-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
enrico
authored and
enrico
committed
Apr 23, 2015
0 parents
commit 3791bdd
Showing
11 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>GrammarAnalyzer</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.8 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
package grammarAnalyzer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.concurrent.ConcurrentSkipListSet; | ||
|
||
|
||
public class Grammar { | ||
|
||
ArrayList<Terminal> VT; | ||
ArrayList<Metasymbol> VN; | ||
Metasymbol S; | ||
ArrayList<Rule> rules; | ||
ConcurrentSkipListSet<Metasymbol> yesEpsilon; | ||
|
||
public Grammar() { | ||
rules = new ArrayList<>(); | ||
yesEpsilon = new ConcurrentSkipListSet<Metasymbol>(); | ||
} | ||
|
||
public Grammar(ArrayList<Terminal> VT, ArrayList<Metasymbol> VN, | ||
Metasymbol S) { | ||
this(); | ||
this.VT = VT; | ||
this.VN = VN; | ||
this.S = S; | ||
|
||
} | ||
|
||
void newRule(Metasymbol head, PhraseForm body) { | ||
Rule rule = new Rule(this, head, body); | ||
rules.add(rule); | ||
} | ||
|
||
private ConcurrentSkipListSet<Terminal> starterSymbols(Metasymbol M) { | ||
ConcurrentSkipListSet<Terminal> ss = new ConcurrentSkipListSet<Terminal>(); | ||
for (Rule R : rules) { | ||
if (R.getLeftside().equals(M)) { | ||
ss.addAll(digFirstTerminal(R)); | ||
} | ||
} | ||
return ss; | ||
} | ||
|
||
private ConcurrentSkipListSet<Terminal> digFirstTerminal(Rule R) { | ||
ConcurrentSkipListSet<Terminal> termList = new ConcurrentSkipListSet<>(); | ||
ArrayList<Symbol> body = R.getRightside().getArray(); | ||
Iterator<Symbol> iter = body.iterator(); | ||
Symbol h = iter.next(); | ||
if (h.getClass().equals(Terminal.class)) { | ||
termList.add((Terminal) h); | ||
return termList; | ||
} else if (h.getClass().equals(Metasymbol.class)) { | ||
|
||
termList.addAll(starterSymbols((Metasymbol) h)); | ||
|
||
if (yesEpsilon.contains(h)) { | ||
if (iter.hasNext()) { | ||
termList.addAll(starterSymbols((Metasymbol) iter.next())); | ||
} | ||
} | ||
} | ||
return termList; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
package grammarAnalyzer; | ||
|
||
public class Metasymbol extends Symbol { | ||
|
||
String M; | ||
|
||
public Metasymbol() { | ||
M = null; | ||
} | ||
|
||
public Metasymbol(String M) { | ||
this.M = M; | ||
} | ||
|
||
public Metasymbol(char c) { | ||
this.M = c + ""; | ||
} | ||
|
||
boolean equals(Symbol O) { | ||
if (M == O.toString()) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
boolean equals(String S) { | ||
if (M == S) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public String toString() { | ||
return M; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
package grammarAnalyzer; | ||
|
||
import java.util.ArrayList; | ||
|
||
|
||
public class PhraseForm { | ||
|
||
ArrayList<Symbol> arr; | ||
String s; | ||
|
||
public PhraseForm(String S) { | ||
arr = new ArrayList<Symbol>(); | ||
this.s = S; | ||
char[] ca = S.toCharArray(); | ||
for (char c : ca) { | ||
if (Character.isLowerCase(c)) { | ||
arr.add(new Terminal(c)); | ||
} else if (Character.isUpperCase(c)) { | ||
arr.add(new Metasymbol(c)); | ||
} | ||
} | ||
|
||
} | ||
|
||
public Symbol get(int i) { | ||
return arr.get(i); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return s; | ||
} | ||
|
||
public String print() { | ||
String s = ""; | ||
for (Symbol sym : arr) { | ||
s = s + sym.getClass() + " " + sym.toString() + " "; | ||
} | ||
|
||
return s; | ||
} | ||
|
||
public int size() { | ||
return arr.size(); | ||
} | ||
|
||
public ArrayList<Symbol> getArray(){ | ||
return arr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
package grammarAnalyzer; | ||
|
||
import java.util.ArrayList; | ||
|
||
|
||
public class Rule { | ||
|
||
Grammar grammar; | ||
Metasymbol leftside; // This is for context-free+ only... | ||
PhraseForm rightside; // Is this the right way? should I make a | ||
// specific class? If I do, I would be able to specify | ||
// rules through BNF | ||
|
||
// ..Later that day: yeah, you should have | ||
|
||
public Metasymbol getLeftside() { | ||
return this.leftside; | ||
}; | ||
|
||
public PhraseForm getRightside() { | ||
return this.rightside; | ||
}; | ||
|
||
public Rule(Grammar grammar, Metasymbol leftSide, PhraseForm body) { | ||
this.grammar = grammar; | ||
this.leftside = leftSide; | ||
this.rightside = body; | ||
} | ||
|
||
public Rule(String leftside, String rightside) { | ||
grammar = null; | ||
this.leftside = new Metasymbol(leftside); | ||
this.rightside = new PhraseForm(rightside); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
package grammarAnalyzer; | ||
|
||
public abstract class Symbol { | ||
|
||
public abstract String toString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
package grammarAnalyzer; | ||
|
||
public class Terminal extends Symbol { | ||
|
||
char c; | ||
|
||
public Terminal(char c) { | ||
this.c = c; | ||
} | ||
|
||
public Terminal(String string) { | ||
this.c = string.toCharArray()[0]; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return c + ""; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
package grammarAnalyzer; | ||
|
||
public class Test { | ||
|
||
public static void main(String[] args) { | ||
|
||
Rule r = new Rule("A", "bC"); | ||
if (r.getLeftside().equals("A")) { | ||
System.out.println(r.getLeftside().toString()); | ||
Symbol h = r.getRightside().get(0); | ||
if (h.getClass().equals(Terminal.class)) { | ||
System.out.println(r.getRightside().toString()); | ||
System.out.println(r.getRightside().print()); | ||
} | ||
} | ||
} | ||
|
||
} |