Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Syntactic Analyzer (assembly) in java. Complete program to cover the following p

ID: 3855520 • Letter: S

Question

Syntactic Analyzer (assembly) in java.

Complete program to cover the following points.

Identify lines that end in ":" (tag), check that there are no spaces in the name and save the name in an arraylist as long as it has not been saved before.
Validate that you do not have two with the same name.


Invalid tags:

Tag :: double ":"
With space: no spaces allowed
1234: can not be only number, must start with letter or underscore _

Program:

package softwaresistemas;

import java.io.*;

public class FileASM {   

public static void main (String args[]){

String linea;

try {

File archivoFuente = new File ("C:/Ensamblador/02TLIGHT.ASM");

FileReader fr = new FileReader (archivoFuente);

BufferedReader br = new BufferedReader(fr);

FileWriter archivoMaquina = new FileWriter("c:/Ensamblador/02TLIGHTNuevo.ASM");

PrintWriter pw = new PrintWriter(archivoMaquina);

while((linea=br.readLine())!=null){

linea = linea.trim();

if(linea.length()>0){

if(!linea.startsWith(";")){

System.out.println(linea);

pw.println(linea);

}

}

}

fr.close();

archivoMaquina.close();

}

catch(Exception e3){

e3.printStackTrace();

}finally{

  

  

}

}

}

Explanation / Answer

Below the complete with additional code as per requirements in question. Added code is in bold . Please rate if satisfied else comment for queries

package chegg.work;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

public class FileASM {

public static void main(String args[]) {

String linea;

List<String> names = new ArrayList<String>();

Set<String> hs = new HashSet<>();

try {

File archivoFuente = new File("C:/Ensamblador/02TLIGHT.ASM");

FileReader fr = new FileReader(archivoFuente);

BufferedReader br = new BufferedReader(fr);

FileWriter archivoMaquina = new FileWriter("c:/Ensamblador/02TLIGHTNuevo.ASM");

PrintWriter pw = new PrintWriter(archivoMaquina);

while ((linea = br.readLine()) != null) {

linea = linea.trim();

if (linea.length() > 0) {

if (!linea.startsWith(";")) {

System.out.println(linea);

pw.println(linea);

}

/**

* Added code to check for lines ending with : tag , checks

* for spaces and saves the name in an array list (names ,

* declared above line 16), I used hashset to remove

* duplicates and copied hashset content to array list..this

* takes care of duplicates

*/

if (linea.endsWith(":")) {

if (!linea.contains(" ")) {

hs.add(linea);

}

}

}

}

names.addAll(hs);

fr.close();

archivoMaquina.close();

} catch (Exception e3) {

e3.printStackTrace();

} finally {

}

}

}