package compilers_lab5;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
public class Compilers_lab5 {
private static final int[][] table = new int[][]{
/* f o r a n g e a-z 0-9 X */
/* state 0 */{1, 4, 5, 4, 4, 4, 4, 4, 10, 11},
/* state 1 */ {4, 2, 4, 4, 4, 4, 4, 4, 4, 11},
/* state 2 */ {4, 4, 3, 4, 4, 4, 4, 4, 4, 11},
/* state 3 */ {4, 4, 4, 4, 4, 4, 4, 4, 4, 11},
/* state 4 */ {4, 4, 4, 4, 4, 4, 4, 4, 4, 11},
/* state 5 */ {4, 4, 4, 6, 4, 4, 4, 4, 4, 11},
/* state 6 */ {4, 4, 4, 4, 7, 4, 4, 4, 4, 11},
/* state 7 */ {4, 4, 4, 4, 4, 8, 4, 4, 4, 11},
/* state 8 */ {4, 4, 4, 4, 4, 4, 9, 4, 4, 11},
/* state 9 */ {4, 4, 4, 4, 4, 4, 4, 4, 4, 11},
/* state 10 */ {11, 11, 11, 11, 11, 11, 11, 11, 10, 11},
/* state 11 */ {11, 11, 11, 11, 11, 11, 11, 11, 11, 11}
};
public enum DomainTag {
END, ERROR, KEYW, IDENT, NUMBER
};
private static final DomainTag[] states = new DomainTag[]{
/* state 0 */DomainTag.END,
/* state 1 */ DomainTag.IDENT,
/* state 2 */ DomainTag.IDENT,
/* state 3 */ DomainTag.KEYW,
/* state 4 */ DomainTag.IDENT,
/* state 5 */ DomainTag.IDENT,
/* state 6 */ DomainTag.IDENT,
/* state 7 */ DomainTag.IDENT,
/* state 8 */ DomainTag.IDENT,
/* state 9 */ DomainTag.KEYW,
/* state 10*/ DomainTag.NUMBER,
/* state 11*/ DomainTag.ERROR,};
private static String getText(String path) {
File file = new File(path);
String result = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
while (reader.ready()) {
result += reader.readLine();
result += '\n';
}
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
HashMap<Character, Integer> hashtable = new HashMap<Character, Integer>();
for (char i = 0; i < 256; i++) {
hashtable.put(i, 9);
}
for (char i = 'a'; i <= 'z'; i++) {
hashtable.put(i, 7);
}
for (char i = '0'; i <= '9'; i++) {
hashtable.put(i, 8);
}
hashtable.put('f', 0);
hashtable.put('o', 1);
hashtable.put('r', 2);
hashtable.put('a', 3);
hashtable.put('n', 4);
hashtable.put('g', 5);
hashtable.put('e', 6);
String program = getText("C:\\Users\\Марина\\Documents\\NetBeansProjects\\JavaApplication3\\test\\f");
Position cur = new Position(program);
while (cur.Cp() != (char) -1) {
while (cur.IsWhiteSpace()) {
cur = cur.Inc();
}
int state = 0;
String attr = "";
Position start = cur;
while (!cur.IsWhiteSpace() && !cur.IsNewLine() && cur.Cp() != -1) {
state = table[state][hashtable.get(cur.Cp())];
attr += Character.toString(cur.Cp());
cur = cur.Inc();
}
Position end = cur;
System.out.println(states[state].toString() + " (" + start.toString() + '-'
+ end.toString() + ") " + attr);
}
}
}
package compilers_lab5;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
public class Position implements Comparable<Position> {
private String text;
private int line, pos, index;
public Position(String text) {
this.text = text;
line = pos = 1;
index = 0;
}
private Position(String text, int line, int pos, int index) {
this.text = text;
this.line = line;
this.pos = pos;
this.index = index;
}
public int Line() {
return line;
}
public int Pos() {
return pos;
}
public int Index() {
return index;
}
@Override
public int compareTo(Position other) {
return Integer.compare(index, other.index);
}
@Override
public String toString() {
return "(" + line + "," + pos + ")";
}
public char Cp() {
if (index == text.length()) {
return (char) -1;
} else {
return text.charAt(index);
}
}
public boolean IsWhiteSpace() {
return index != text.length() && Character.isWhitespace(Cp());
}
public boolean IsLetter() {
return index != text.length() && Character.isLetter(Cp());
}
public boolean IsLetterOrDigit() {
return index != text.length() && Character.isLetterOrDigit(Cp());
}
public boolean IsDigit() {
return index != text.length() && Character.isDigit(Cp());
}
public boolean IsNewLine() {
if (index == text.length()) {
return true;
}
if (Cp() == '\r' && this.Inc().index < text.length()) {
return this.Inc().Cp() == '\n';
}
return Cp() == '\n';
}
public Position Inc() {
int indexNew = index, lineNew = line, posNew = pos;
if (index < text.length()) {
if (IsNewLine()) {
if (text.charAt(index) == '\r') {
indexNew++;
}
lineNew++;
posNew = 1;
} else {
posNew++;
}
indexNew++;
}
return new Position(text, lineNew, posNew, indexNew);
}
}