Question

Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase...

Please Do it In Java:

Objectives:

  • To Java programming language
  • To understand the lexical analysis phase of program compilation

Assignment:

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 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. Optionally, your program can 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 category appeared in the input.

Sample 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

 

More details:

  • Case is not used to distinguish tokens.
  • The delimiters can be space, tab, newline, and special characters. You only need to use one delimiter in your implementation.
  • Choice at least 5 token classes or use the sample ones above.
  • Your program output can be ordered in any way you want.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer;

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define KW_COUNT 5
#define SC_COUNT 9
#define MAX_SYMBOLS 100
#define MAX_TOK_SIZE 256
class LexicalParser{
private:
static const string keywords[];
static const char specialCharacters[];
enum tokenClass { KEYWORD, IDENTIFIER, INTEGER, REAL, SPECIAL};
struct symbol{
string token;
int count;
tokenClass tc;
};
symbol symbolTable[MAX_SYMBOLS];
int symbolCount;
char nextToken;
string error;
public:
LexicalParser(){
symbolCount = 0;
error = "";
nextToken = 0;
}
bool parse(char* fn){
ifstream fin;
string s;
fin.open(fn);
if( fin.bad() ){
error = "Cannot open the file";
fin.close();
return false;
}
nextToken = fin.get();
while( !fin.eof() ){
readToken( fin, s );
if( isKeyword(s) )
addSymbol(s, KEYWORD);
else if ( isInteger(s) ){
if ( nextToken == '.' ){
string s2;
nextToken = fin.get();
readToken(fin, s2);
s = s + "." + s2;
addSymbol( s, REAL );
} else
addSymbol(s, INTEGER);
}
else if ( isSpecialChar(s) )
addSymbol( s, SPECIAL );
else if( isIdentifier(s) )
addSymbol(s, IDENTIFIER);
else{
error = "Error occurred. Unknown token \"" + s + "\"";
return false;
}
}
fin.close();
return true;
}
string getError(void){
return error;
}
void readToken(ifstream& fin, string &s){
char scanstring[] = "\n\t ()[]+-=,;.\0";
char spacestring[] = "\n\t ";
char specialstring[] = "()[]+-=,;";
char token[MAX_TOK_SIZE];
int i=0;
if( strchr(specialstring, nextToken) ){
token[i++] = nextToken;
nextToken = fin.get();
while( strchr(spacestring, nextToken) && fin.good() ){
nextToken = fin.get();
}
token[i] = '\0';
s = string(token);
return;
}
while ( fin.good() ){
if( strchr(scanstring, nextToken) ){
while( strchr(spacestring, nextToken) && fin.good() ){
nextToken = fin.get();
}
token[i] = '\0';
s = string(token);
return;
}
else
token[i++] = nextToken;
nextToken = fin.get();
}
}
bool isKeyword(string& s){
for(int i=0; i<KW_COUNT; i++)
if( !s.compare(keywords[i]) )
return true;
return false;
}
bool isSpecialChar( string& s){
if( s.length() !=1 )
return false;
char sc = s.at(0);
for(int i=0; i<SC_COUNT; i++)
if( sc == specialCharacters[i] )
return true;
return false;
}
bool isIdentifier( string& s){
for(int i=0; i<s.length(); i++)
if( !( (s.at(i) >='a' && s.at(i) <='z') ||
(s.at(i) >='A' && s.at(i) <='Z') ) )
return false;
return true;
}
bool isInteger(string& s){
for(int i=0; i<s.length(); i++)
if( s.at(i) < '0' || s.at(i)>'9')
return false;
return true;
}
void addSymbol(string& tok, tokenClass tc){
for( int i=0; i<symbolCount; i++)
if( !tok.compare(symbolTable[i].token) ){
symbolTable[i].count++;
return;
}
symbolTable[symbolCount].token = tok;
symbolTable[symbolCount].count = 1;
symbolTable[symbolCount++].tc = tc;
return;
}
void printTokenList(){
cout << "Symbol \t Count \t TokenType\n\n";
for(int i=0; i<symbolCount; i++){
cout <<symbolTable[i].token<<"\t" <<symbolTable[i].count<<"\t";
if( symbolTable[i].tc == INTEGER )
cout<< "INTEGER"<<endl;
else if( symbolTable[i].tc == REAL )
cout<< "REAL"<<endl;
else if( symbolTable[i].tc == IDENTIFIER )
cout<< "IDENTIFIER"<<endl;
else if( symbolTable[i].tc == KEYWORD )
cout<< "KEYWORD"<<endl;
else
cout<<"SPECIAL"<<endl;
}
}
};
const string LexicalParser::keywords[] = { "if", "then", "else", "begin", "end" };
const char LexicalParser::specialCharacters[] = { '(', ')', '[', ']', '+', '-', '=', ',', ';'};
int main(int argc, char* argv[])
{
LexicalParser parser;
if ( !parser.parse( argv[1] ) ){
cout<<"Parser returned error: "<<parser.getError();
exit(0);
}
parser.printTokenList();
return 0;
}

Add a comment
Answer #2

If ( peek holds a digit ) {

            v = 0;

            do {

                        v = v * 10 + integer value of digit peek;

                        peek = next input character;

            } while ( peek holds a digit );

            return token  (num , v)

}


source: paper
answered by: muhammad ali
Add a comment
Know the answer?
Add Answer to:
Please Do it In Java: Objectives: To Java programming language To understand the lexical analysis phase...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The first project involves modifying the attached lexical analyzer and the compilation listing ge...

    The first project involves modifying the attached lexical analyzer and the compilation listing generator code. You need to make the following modifications to the lexical analyzer, scanner.l: 1. A second type of comment should be added that begins with // and ends with the end of line. As with the existing comment, no token should be returned. 2. The definition for the identifiers should be modified so that underscores can be included, however, consecutive underscores, leading and trailing underscores should...

  • Compiler Design Please help me with this lexical analysis In C Using FInite automata and a...

    Compiler Design Please help me with this lexical analysis In C Using FInite automata and a driver. With file test scanner , main, scanner, token.h All case sensitive Each scanner error should display "Scanner Error:" followed by details including the line number ff available Alphabet all English letters (upper and lower), digits, plus the extra characters as seen below, plus WS No other characters allowed and they should generate errors Identifiers begin with a lower case letter and continue with...

  • Write a java project that reads a sequence of up to 25 pairs of names and...

    Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes, street address, city, state, and 10-digit phone number for individuals. Store the data in an object designed to store a first name (string), last name (string), and postal code (integer), street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' . Assume each...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas...

    Let’s build a dynamic string tokenizer! Start with the existing template and work on the areas marked with TODO in the comments: Homework 8 Template.c Note: If you turn the template back into me without adding any original work you will receive a 0. By itself the template does nothing. You need to fill in the code to dynamically allocate an array of strings that are returned to the user. Remember: A string is an array. A tokenizer goes through...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • program should be in c language. thank you for helping me. 1 of 4 CSCI 281...

    program should be in c language. thank you for helping me. 1 of 4 CSCI 281 LAB 5 -STACKS AND A CALCULATOR ASSIGNMENT Using 2 stacks to implement a calculator CALCULATOR 1. User input of two types. Integer values and associated tokens(+ 2. As the user needs to enter the input in the following manner: +5/17-3 9+2 8+7 You should use a looping scanf to allow the user to enter digit, then a char, then a digit, then a car...

  • Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input...

    Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input from the keyboard (using a Scanner) and split the incoming tokens into integers, floating point numbers, and strings. The incoming tokens will be added to one of three accumulators which start out at zero, and which keep a running total. Thus, the class will have an accumulator for integers. It starts out with the value zero. Every time another integer is read in, it...

  • JAVA LANGUAGE For this lab you are required to fulfill all requirements exactly as described in...

    JAVA LANGUAGE For this lab you are required to fulfill all requirements exactly as described in this provided document, no less, no more. Question: Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed...

  • What this Assignment Is About: Review on Java I topics, such as primitive data types, basic...

    What this Assignment Is About: Review on Java I topics, such as primitive data types, basic I/O, conditional and logical expressions, etc. Review on Java loops. Documentation Requirements to get full credits in Documentation The assignment number, your name, StudentID, Lecture number(time), and a class description need to be included at the top of each file/class. A description of each method is also needed. Some additional comments inside of methods (especially for a "main" method) to explain code that are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT