Objectives of the project 1. To develop your understanding of lexical analyzer i
ID: 3921926 • Letter: O
Question
Objectives of the project 1. To develop your understanding of lexical analyzer in compilers of program languages. 2. To improve you knowledge of Java programming. Topic of assignment: Lexical analyzer Write Java programs for a lexical analyzer which can identify the following tokens: 1 INT_LIT: consists of digits 2 ID_CODE: starts with a letter followed by letters and digits, and has a limited length of 10 3 ASSIGN_CODE: = 4 PLUS_CODE: + 5 MINUS_CODE: - 6 DIVISON_CODE: / 7 MULTIP_CODE: * Programs You will design two classes: TestLexAnalyzer: contains main method which performs input/output operations. LexAnalyzer: contains all the methods related to lexical analysis. Refer to the C programs in the text book for algorithm design. Input: a string of assignment statement Output: tokens of each lexeme. A running example: Input string: sum = sum + num; Output: Lexeme Token sum ID_CODE = ASSIGN_CODE sum ID_CODE + PLUS_CODE num ID_CODE using java source code
Explanation / Answer
LexAnalyzer class
public class LexAnalyzer {
String str;
public LexAnalyzer(String str1) {
str=new String();
str=str1;
}
void tokenize()
{ boolean check=true;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='+'&&check)
{
System.out.print(" + PLUS_CODE ");
}
if(str.charAt(i)=='-'&&check)
{
System.out.print(" - PLUS_CODE ");
}
if(str.charAt(i)=='*'&&check)
{
System.out.print(" * PLUS_CODE ");
}
if(str.charAt(i)=='/'&&check)
{
System.out.print(" / PLUS_CODE ");
}
if(str.charAt(i)=='='&&check)
{
System.out.print(" = ASSIGN_CODE ");
}
if(str.charAt(i)=='0'||str.charAt(i)=='1'||str.charAt(i)=='2'||str.charAt(i)=='3'||str.charAt(i)=='4'||str.charAt(i)=='5'||str.charAt(i)=='6'||str.charAt(i)=='7'||str.charAt(i)=='8'||str.charAt(i)=='9')
{
if(check==true)
{
check=false;
}
else
{
if(str.charAt(i)=='0'||str.charAt(i)=='1'||str.charAt(i)=='2'||str.charAt(i)=='3'||str.charAt(i)=='4'||str.charAt(i)=='5'||str.charAt(i)=='6'||str.charAt(i)=='7'||str.charAt(i)=='8'||str.charAt(i)=='9')
{
System.out.print(str.charAt(i-1)+""+str.charAt(i)+" INT_LIT");
check=true;
}
else
{
System.out.println("String contain invalid token: digit followed by character");
break;
}
}
}
if(Character.isAlphabetic(str.charAt(i)))
{
int j=i;
boolean flag=true;
while(i<str.length()&&(Character.isAlphabetic(str.charAt(i))||Character.isDigit(str.charAt(i))))
{
i++;
if(i-j>10)
{
System.out.println("String contain invalid token: It contain to");
flag=false;
break;
}
}
if(!flag)
{
break;
}
else
{
System.out.print(str.substring(j,i)+" ID_CODE");
i--;
}
}
}
}
}
TestLexAnalyzer class
import java.util.Scanner;
public class TestLexAnalyzer {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string to tokenize");
String str=sc.nextLine();
LexAnalyzer lexAnalyzer=new LexAnalyzer(str);
lexAnalyzer.tokenize();
sc.close();
}
}
Output:
Enter the string to tokenize
sum=sum+num
sum ID_CODE = ASSIGN_CODE sum ID_CODE + PLUS_CODE num ID_CODE
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.