Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
enrico authored and enrico committed Apr 23, 2015
0 parents commit 3791bdd
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 0 deletions.
6 changes: 6 additions & 0 deletions GrammarAnalyzer/.classpath
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>
1 change: 1 addition & 0 deletions GrammarAnalyzer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions GrammarAnalyzer/.project
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>
12 changes: 12 additions & 0 deletions GrammarAnalyzer/.settings/org.eclipse.jdt.core.prefs
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
67 changes: 67 additions & 0 deletions GrammarAnalyzer/src/grammarAnalyzer/Grammar.java
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;
}

}
38 changes: 38 additions & 0 deletions GrammarAnalyzer/src/grammarAnalyzer/Metasymbol.java
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;
}

}
51 changes: 51 additions & 0 deletions GrammarAnalyzer/src/grammarAnalyzer/PhraseForm.java
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;
}
}
37 changes: 37 additions & 0 deletions GrammarAnalyzer/src/grammarAnalyzer/Rule.java
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);
}

}
7 changes: 7 additions & 0 deletions GrammarAnalyzer/src/grammarAnalyzer/Symbol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

package grammarAnalyzer;

public abstract class Symbol {

public abstract String toString();
}
21 changes: 21 additions & 0 deletions GrammarAnalyzer/src/grammarAnalyzer/Terminal.java
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 + "";
}

}
19 changes: 19 additions & 0 deletions GrammarAnalyzer/src/grammarAnalyzer/Test.java
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());
}
}
}

}

0 comments on commit 3791bdd

Please sign in to comment.