Simple lexical analysis java program -
Simple lexical analysis java program -
my little project lexical analysis programme in have take every word found in arbitrary .java file , list every line appears on in file. need have 1 table dedicated reserved words , additional words found in document. programme like:
public class xxxx { int xyz; xyz = 0; }
the output should be:
reserved words: class: 1 int: 2 public: 1 other words: xxxx: 1 xyz: 2, 3
but there lot of problems current programme , have no thought whats going on, amendments programme or finish rewrite welcome. i'm trying hang of java language hobby help welcome long can understand whats going on. i'm sure there simple solution problem effort didn't work :( help ^^
import java.io.file; import java.io.filenotfoundexception; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.list; import java.util.map; import java.util.scanner; public class lexicalanalysis { private string[] keywords = { "abstract", "boolean", "byte", "case", "catch", "char", "class", "continue", "default", "do", "double", "else", "extends", "final", "finally", "float", "for", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while", "false", "true", "null" }; hashmap<string, arraylist<integer>> keywordstable; hashmap<string, arraylist<integer>> otherwords = new hashmap<string, arraylist<integer>>(); public lexicalanalysis(string filename){ scanner kb = null; int linenumber = 0; seek { kb = new scanner(new file(filename)); } grab (filenotfoundexception e) { e.printstacktrace(); } keywordstable = new hashmap<string, arraylist<integer>>(); for(int = 0; < 47; i++){ keywordstable.put(keywords[i], new arraylist<integer>()); } while(kb.hasnextline()){ linenumber++; string line = kb.nextline(); string[] lineparts = line.split("\\s+|\\.+|\\;+|\\(+|\\)+|\\\"+|\\:+|\\[+|\\]+"); for(string x: lineparts){ arraylist<integer> list = keywordstable.get(x); if(list == null){ list = otherwords.get(x); if(list == null){ arraylist<integer> temp = new arraylist<integer>(); temp.add(linenumber); otherwords.put(x,temp); }else{ otherwords.remove(x); arraylist<integer> temp = new arraylist<integer>(); temp.add(linenumber); otherwords.put(x, temp); } }else{ keywordstable.remove(x); arraylist<integer> temp = new arraylist<integer>(); temp.add(linenumber); keywordstable.put(x, temp); } } } system.out.println("keywords:"); printmap(keywordstable); system.out.println(); system.out.println("other words:"); printmap(otherwords); } public static void printmap(map<string, arraylist<integer>> mp) { iterator<map.entry<string, arraylist<integer>>> = mp.entryset().iterator(); while (it.hasnext()) { map.entry<string, arraylist<integer>> pairs = (map.entry<string, arraylist<integer>>)it.next(); system.out.print(pairs.getkey() + " = "); printlist(pairs.getvalue()); system.out.println(); it.remove(); } } public static void printlist(list x){ for(object m : x){ system.out.print(m + ", "); } } public static void main(string args[]){ new lexicalanalysis("lexitest.txt"); } }
the simplest way of doing using jflex right lex file defining keywords. 1 time have that, counting identifiers , keywords trivial.
java lexical-analysis lookup-tables
Comments
Post a Comment