Write a Java program that implements a simple scanner for a source file given as
ID: 3699193 • Letter: W
Question
Write a Java program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Your program should build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a list of all the tokens that appeared in the input, the number of times each token appears in the input and the class of each token. Your program should also list how many times tokens of each class appeared in the input. The token format: ?keyword -> if | then | else | begin | end ?identifier -> character | character identifier ?integer -> digit | digit integer ?real -> integer.integer ?special -> ( | ) | [ | ] | + | - | = | , | ; ?digit -> 0|1|2|3|4|5|6|7|8|9 ?character -> a|b|c ... |z|A|B|C ... |Z Write a Java program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Your program should build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a list of all the tokens that appeared in the input, the number of times each token appears in the input and the class of each token. Your program should also list how many times tokens of each class appeared in the input. The token format: ?keyword -> if | then | else | begin | end ?identifier -> character | character identifier ?integer -> digit | digit integer ?real -> integer.integer ?special -> ( | ) | [ | ] | + | - | = | , | ; ?digit -> 0|1|2|3|4|5|6|7|8|9 ?character -> a|b|c ... |z|A|B|C ... |Z Write a Java program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Your program should build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a list of all the tokens that appeared in the input, the number of times each token appears in the input and the class of each token. Your program should also list how many times tokens of each class appeared in the input. The token format: ?keyword -> if | then | else | begin | end ?identifier -> character | character identifier ?integer -> digit | digit integer ?real -> integer.integer ?special -> ( | ) | [ | ] | + | - | = | , | ; ?digit -> 0|1|2|3|4|5|6|7|8|9 ?character -> a|b|c ... |z|A|B|C ... |ZExplanation / Answer
// HERE IS THE CODE OF YOUR PROGRAM
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 Lexical {
/**
* @param args
*/
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 Lexical(String fileName){
Scanner jb = null;
int lineNumber = 0;
try {
jb = new Scanner(new File(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
keywordsTable = new HashMap<String, ArrayList<Integer>>();
for(int i = 0; i < 48; i++){
keywordsTable.put(keywords[i], new ArrayList<Integer>());
}
while(kb.hasNextLine()){
lineNumber++;
String line = jb.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>>> it = 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) {
// TODO Auto-generated method stub
new LexicalAnalysis("textfile.txt");
}
}
//////////////////////////////////////////
public class NewFile {
/**
* @param args the command line arguments
*/
static char T[]=new char[1000];
static char NT[]=new char[1000];
static char P[][]=new char[50][13];
static String T1="(){}ifdcv;e_=+-*/%,0123456789abghjklmnoprstuvwxyqz";
static String[] p1=new String[50];
static String NT1="STBKCLDFGEAHMIJN";
static char FT[][]= new char[50][100];
static char FLWT[][]= new char[50][100];
//char P[8][10]={"E->TA","A->+TA","A->e","T->FB","B->*FB","B->e","F->(E)","F->d"};
public static void main(String[] args) {
// TODO code application logic here
for(int j=0;j<T1.length();j++){
T[j]=T1.charAt(j);
}
for(int i=0;i<NT1.length();i++){
NT[i]=NT1.charAt(i);
}
p1[0]="S->TC(B){F}";
p1[1]="T->i";p1[2]="T->f";p1[3]="T->d";p1[4]="T->c";p1[5]="T->v";p1[6]="B->TCN";p1[7]="B->e";p1[8]="C->LI";p1[9]="C->_I";p1[10]="F->CAGH;";p1[11]="H->MGH";p1[12]="H->e";p1[13]="K->DE";p1[14]="G->K";p1[15]="G->C";
p1[16]="E->DE";p1[17]="E->e";p1[18]="J->L";p1[19]="J->D";p1[20]="J->_";p1[21]="I->JI";p1[22]="I->e";
p1[23]="A->=";p1[24]="M->+";p1[25]="M->-";p1[26]="M->*";p1[27]="M->/";p1[28]="N->,TCN";p1[29]="N->e";
p1[30]="D->0";p1[31]="D->1";p1[32]="D->2";p1[33]="D->3";p1[34]="D->4";p1[35]="D->5";p1[36]="D->6";p1[37]="D->7";p1[38]="D->8";p1[39]="D->9";
p1[40]="L->a";p1[41]="L->b";p1[42]="L->g";p1[43]="L->h";p1[44]="L->j";p1[45]="L->k";p1[46]="L->l";p1[47]="L->m";p1[48]="L->n";p1[49]="L->o";p1[50]="L->p";
p1[51]="L->q";p1[52]="L->r";p1[53]="L->s";p1[54]="L->t";p1[55]="L->u";p1[56]="L->w";p1[57]="L->x";p1[58]="L->y";p1[59]="L->z";
for(int i=0;i<50;i++){
for(int j=0;j<p1[i].length();j++){
//if(p1[i].charAt(j)=='')break;
P[i][j]=p1[i].charAt(j);
}
}
NewFile nf= new NewFile();
int i,j;
nf.ComputeFi();
System.out.print("THE GIVEN GRAMMAR IS:: ");
for(i=0;i<50;i++){
for(int k=0;;k++){
if(P[i][k]=='')break;
System.out.print(""+P[i][k]);}
System.out.println();
}
//System.out.print(P[6][3]);
System.out.print(" The FIRST for all grammar symbols:: ");
for(i=0;i<16;i++)
{
System.out.print("FIRST("+NT[i]+")={");
for(j=0;j<50;j++)
if(FT[i][j]!='')
System.out.print(FT[i][j]+" ");
System.out.print("} ");
}
nf.ComputeFollow();
System.out.print(" The FOLLOW for all grammar symbols:: ");
for(i=0;i<16;i++)
{
System.out.print("FOLLOW("+NT[i]+")={");
for(j=0;j<50;j++)
if(FLWT[i][j]!='')
System.out.print(" "+FLWT[i][j]);
System.out.print("} ");
}
}
void ComputeFol()
{
int len,i,j,k,added=0;
char sym1,sym2,elm1,elm2,sym;
FLWT[getIndex(NT,'S')][17]='$';
for(int nj=0;nj<2;nj++)
{
for(i=0;i<50;i++)
{
len=p1[i].length();
for(j=3;P[i][j]!='';j++)
{
sym1=P[i][j];
sym2=P[i][j+1];
if(isNonTerminal(sym1)==1 && isTerminal(sym2)==1)
FLWT[getIndex(NT,sym1)][getIndex(T,sym2)]=sym2;
}
}
for(i=0;i<50;i++)
{
len=p1[i].length();
for(j=3;;j++)
{if(P[i][j]=='')break;
sym1=P[i][j];
sym2=P[i][j+1];
if(isNonTerminal(sym1)==1 && isNonTerminal(sym2)==1)
{
for(k=0;k<50;k++)
{
elm1=FLWT[getIndex(NT,sym1)][k];
elm2=FT[getIndex(NT,sym2)][k];
if(elm1!=elm2)
if(elm2!='' && elm2!='e')
FLWT[getIndex(NT,sym1)][k]=elm2;
}
}
}
}
for(i=0;i<50;i++)
{
sym1=P[i][p1[i].length()-1];
if(isNonTerminal(sym1)==1)
{
sym2=P[i][0];
for(k=0;k<50;k++)
{
elm1=FLWT[getIndex(NT,sym1)][k];
elm2=FLWT[getIndex(NT,sym2)][k];
if(elm1!=elm2)
{
if(elm2!='' && elm2!='e')
{
added=1;
FLWT[getIndex(NT,sym1)][k]=elm2;
}
}
}
}
sym=P[i][p1[i].length()-1];
if(isNonTerminal(sym)==1 && Elip(sym)==1)
{
sym1=P[i][p1[i].length()-2];
if(isNonTerminal(sym1)==1)
{
sym2=P[i][0];
for(k=0;k<50;k++)
{
elm1=FLWT[getIndex(NT,sym1)][k];
elm2=FLWT[getIndex(NT,sym2)][k];
if(elm1!=elm2)
{
if(elm2!='' && elm2!='e')
{
added=1;
FLWT[getIndex(NT,sym1)][k]=elm2;
}
}
}
}
if(i==51)
{
if(added==1)
{
i=-1;
added=0;
}
}
}
}}
}
void ComputeFi()
{
int added=0,i,j,k;
char X,elm1,elm2;
//clrscr();
for(i=0;i<50;i++)
for(j=0;j<50;j++)
FT[i][j]='';
for(i=0;i<50;i++)
{
X = P[i][3];
if(X=='e')
addElip(P[i][0]);
else if(isTerminal(X)==1)
FT[getIndex(NT,P[i][0])][getIndex(T,X)]=X;
}
for(i=0;i<50;i++)
{
X = P[i][3];
if(isNonTerminal(X)==1)
{
for(j=3;;j++)
{if(P[i][j]=='')break;
X=P[i][j];
for(k=0;k<50;k++)
{
elm1=FT[getIndex(NT,P[i][0])][k];
elm2=FT[getIndex(NT,X)][k];
if(elm1!=elm2)
{
if(elm2!='')
{
added=1;
FT[getIndex(NT,P[i][0])][k]=elm2;
}
}
}
if(Elip(X)==0)
break;
}
if(j==P[i].length)
{
added=1;
FT[getIndex(NT,P[i][0])][17]='e';
}
}
if(i==51)
{
if(added==1)
{
i=-1;
added=0;
}
}
}
}
private int getIndex(char[] A, char c) {
// throw new UnsupportedOperationException("Not yet implemented");
for(int i=0;A[i]!='';i++)
if(A[i]==c)
return i;
return -1;
}
private int isNonTerminal(char c)
{
for(int i=0;NT[i]!='';i++)
if(NT[i]==c)
return 1;
return 0;
}
private int isTerminal(char c)
{
for(int i=0;T[i]!='';i++)
if(T[i]==c)
return 1;
return 0;
}
private int Elip(char c) {
//throw new UnsupportedOperationException("Not yet implemented");
if(FT[getIndex(NT,c)][17]=='e')
return 1;
return 0;
}
private void addElip(char c) {
FT[getIndex(NT,c)][17]='e';
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.