1. Engineering
  2. Computer Science
  3. cscan is a lexical analyzer for a c compiler it...

Question: cscan is a lexical analyzer for a c compiler it...

Question details

cscan is a lexical analyzer for a C compiler. It identifies the C tokens from its standard input and writes them to its standard output, one per line. Afterwards it prints the number of occurrences of each type of token (number, ident, char, string, or the lexeme for the remaining types of tokens) in descending order. Ties should first be broken by the string length of the token name and then by lexical order (phone book order of the token name). It writes invalid characters (with a diagnostic) to its error output instead of its standard output. Tokens are defined below. Terminals are enclosed between dittos (double quotes). Dittos are escaped inside terminal strings by preceding them with a backslash

use this c++ template to solve:

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cassert>
#define MAXTOK 256 /* maximum token size */

int cur; /* current character being processed */
int peek; /* next charcter to be processed */

int skip() {
/* SKIP OVER WHITESPACE */
while (isspace(cur)) {
cur = peek;
peek = std::fgetc(stdin);
}
return cur;
}

/* Find a token and store the chars into lexeme buffer */
int scan(char *lexeme) {
int i = 0;
/* skip over whitespaces and check for EOF */
if (skip() == EOF)
return EOF;
else if (isalpha(cur) || cur == '_') {
// ID TOKEN
while (isalpha(cur) || isdigit(cur) || cur == '_') {
lexeme[i++] = cur;
cur = peek;
if (peek != EOF)
peek = std::fgetc(stdin);

}
lexeme[i] = '\0';
return i; // return any value other than EOF
}
else {
// YOU NEED TO IMEPLEMENT THE REST
assert(0 && "YOU NEED TO IMPLEMENT THE REST);
}
return 0;
}

int main(int argc, char *argv[])
{
char lexeme[MAXTOK];
int result;

/* setup for scanning */
cur = peek = std::fgetc(stdin);
if (cur != EOF)
peek = std::fgetc(stdin);

while ((result = scan(lexeme)) != EOF) {
std::cout << lexeme << std::endl;
}
return 0;
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution