Interview Questions

Implement a lexical analyzer Input string .....

Software QA/Tests Interview Questions from Microsoft


(Continued from previous question...)

Implement a lexical analyzer Input string .....

Question:
Implement a lexical analyzer
Input string - a C program with Comments
Output- the input C program string without comments



maybe an answer1:

public class CommentStripper {

public static void main(String[] args) {
final int CODE = 0; // parsing normal code
final int SLASH = 1; // found a leading '/'
final int BLOCK = 2; // in a block c-style comment
final int LINE = 3; // in a line comment
final int STAR = 4; // found a trailing * in a block comment
final int QUOTE = 5; // in a quote
final int ESCAPE = 6; // found the escape character (i.e., \)
int state = CODE; // current state
final char EOL = System.getProperty("line.separator").charAt(0);
// End-of-line
// const
ant final int EOF = -1; // End-of-file constant
int whatever; // current read char
while ((whatever = StdInput.readChar()) != EOF) {
char c = (char) whatever;
switch (state) {
case CODE:
if (c == '"') {
state = QUOTE;
System.out.print(c);
} else if (c == '/') {
state = SLASH;
} else {
System.out.print(c);
}
break;
case SLASH:
if (c == '*') {
state = BLOCK;
} else if (c == '/') {
state = LINE;
} else {
state = CODE;
System.out.print("/" + c);
}
break;
case BLOCK:
if (c == '*') {
state = STAR;
}
break;
case STAR:
if (c == '/') {
state = CODE;
System.out.print(" ");
} else if (c == '*') {
state = STAR;
} else {
state = BLOCK;
}
break;
case LINE:
if (c == EOL) {
state = CODE;
System.out.println();
}
break;
case QUOTE:
if (c == '"') {
state = CODE;
System.out.print(c);
} else if (c == '\\') {
state = ESCAPE;
System.out.print(c);
} else {
System.out.print(c);
}
break;
case ESCAPE: {
state = QUOTE;
System.out.print(c);
}
}
}
}
}

class StdInput {
public static int readChar() {
try {
return System.in.read();
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}



maybe an answer2:

Just basic /**/ and // operations. Didn't handle other operations such as comments inside string or printf

void LexicalAnalyzer(string path)
{
string line;
char* code_file = (char*)malloc(1000*sizeof(char));
int len = 0;
int j = 0;
int comment_index = 0;
std::ifstream myfile (path.c_str());
while(myfile.good())
{
getline(myfile, line);
for(int i = 0; i < line.size(); i++)
{
code_file[j] = line[i];
j++;
len++;
}
}
int len_variable = len;
for(j = 0; j < len_variable; j++)
{
if(code_file[j] == '/' &&& code_file[j + 1] == '*')
{
comment_index = j;
while(!(code_file[j] == '*' && code_file[j + 1] == '/'))
{
j++;
}
memcpy((char*)code_file + comment_index, (char*)code_file + j + 2, len - j);
len_variable = len_variable -(j + 2 - comment_index);
j = 0;
}
if(code_file[j] == '/' && code_file[j + 1] == '/')
{
comment_index = j;
while(code_file[j] != '\r')
{
j++;
}
memcpy((char*)code_file + comment_index, (char*)code_file + j + 2, len - j);
len_variable = len_variable -(j + 2 - comment_index);
j = 0;
}
}
code_file[len_variable] = NULL;

(Continued on next question...)

Other Interview Questions