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

The first phase of compilation is called scanning or lexical analysis. This phas

ID: 3540883 • Letter: T

Question

The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser.

Write a C/C++ 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:

Explanation / Answer

import java.io.*;

import java.lang.*;

import java.util.*;

class Format

{

public static void main (String[]args)

{

String winput, sinput, finput, output;

int winput2 = 0;

Console console = System.console();

winput = console.readLine("Please enter the width of the line. This must be less than or equal to 80: ");

winput2 = Integer.parseInt(winput);

while (winput2 > 80)

{

winput = console.readLine("Please enter the width of the line. This must be less than or equal to 80: ");

winput2 = Integer.parseInt(winput);

}

sinput = console.readLine("Please enter the text you want to be formatted: ");

StringTokenizer sinput2 = new StringTokenizer(sinput);

finput = console.readLine("How do you want to format your text? Choose 'L' for left-justified, 'R' for right-justified, 'C' for centered, or 'Q' to quit the program: ");

if (finput.equals("L"))

{

Lformat(winput2, sinput);

}

else if (finput.equals("R"))

{

Rformat(winput2, sinput);

}

}

public static void Lformat (int winput, String sinput)

{

int i, count = 0;

for (i=1; i <= winput; i++)

{

System.out.print("a");

}

System.out.println();

StringTokenizer sinput2 = new StringTokenizer(sinput);

while(sinput2.hasMoreTokens())

{

System.out.print(sinput2.nextToken() + " ");

}

}

public static void Rformat (int winput, String sinput)

{

System.out.println(sinput);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote