Compiler Construction, Code and test a compiler in java | Practice Set

Code and test a compiler in java that should perform following actions:

  • Read input source code from the following file: “input.txt”

  • Give user the option to perform Lexical Analysis only or Lexical and Syntax Analysis both. (in case of Lexical and Syntax Analysis both, take one line input from the screen)

  • Recognize following tokens for the lexemes in the input:

– Keywords: int, char, string, if, else, do, while.

– Identifiers (id): letter followed by zero or more letters or digits.

– String Literal: (sl): anything that is surrounded by double quotes,

for example “Hi”

– Int value (in): example: 155

– Relational Operators (ro): <, <=, ==, <>, >= and >

– Arithmetic Operators (ao): +, -, *, /

– Other Operators (oo): assignment: =, parenthesis: (, ), braces: {, },

line terminator: ;

  • Upon recognizing a token, should print the lexeme, the token name and the attribute value based on the above table

  • Store tokens in symbol table based on the above table.

  • Should properly handle the comments enclosed in /* and */ or following // .

  • Should properly handle the white spaces (blanks, tabs or newlines).

  • Print all data stored in the symbol table just before exiting.

  • If the input to your compiler contains something other than the lexemes mentioned in the table above, then it should generate and display the error with:

- The line number

- The unrecognized lexeme in the source code and

- A meaningful error message


For Syntax Analysis, if the input contains a legal expression involving operators + and * and parenthesis ( and ), your compiler should accept it and display “Compiled Successfully”, othewise give out error message with correct line number and a meaningful error message: Use the CFG and the Parsing Table given below for Syntax Analysis of the input.










Sample Input Source Code for Lexical Analysis only:

int val1 = 21; // to store a value 
int val2; 
int total; 
string purpose = “Calculation”; /* to store calculation */ 
if ( val1 > 10 ) 
{  
	val2 = 5; 
} 
else  
{  
	val2 = 10; 
} 
total = (val1 + val2) * val1; 










Sample Input Source Code for Lexical Analysis and Syntax Analysis both:


(val2 + val1) * val3











Submission Instructions:

  • Do NOT use package statement in your code.

  • Input file named input.txt should be kept in same folder where your main method code is.

  • Submit your compiler code (.java file(s)) in the form of one .rar file having format: “RegistrationID_Section_FullName_Project.rar”

  • Mention your full name, registration Id and section in your email Subject title also.

  • If you send me multiple emails of project, 2 marks will be deducted on each extra email.

  • If you send me code that does not compile, you will get zero.

  • If you send me copied or partially copied code, you will get zero.

  • Late submissions will get 2 marks deducted per day.

  • Questions will be asked during evaluation by running your code in class.


If you need any programming assignment help in Java programming, Java project or Java homework or need solution of above problem then we are ready to help you.

Send your request at realcode4you@gmail.com and get instant help with an affordable price.

We are always focus to delivered unique or without plagiarism code which is written by our highly educated professional which provide well structured code within your given time frame.


If you are looking other programming language help like C, C++, Java, Python, PHP, Asp.Net, NodeJs, ReactJs, etc. with the different types of databases like MySQL, MongoDB, SQL Server, Oracle, etc. then also contact us.

7 views0 comments