Last active
March 26, 2024 03:12
-
-
Save hakxcore/42ac1742b294134878c584c9dfd817b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Input File (input.txt) | |
int main() { | |
int a=20,65b=10; | |
cout << "Hello" << endl; | |
return 0; | |
} | |
## Lexical Analyser Code | |
import java.io.*; | |
public class Lexic_analyzer { | |
String keywords[]={"int","float","cout","cin","for","endl","return"}; | |
char symbols[]={'.','+','-','*','/','=','(',')',',','{','}',':',';','>','<'}; | |
String cur,temp=""; | |
boolean iskeyword(String str){ | |
for(int i=0;i<keywords.length;i++){ | |
if(str.equals(keywords[i])){ | |
return true; | |
} | |
} | |
return false; | |
} | |
boolean isSymbol(char ch){ | |
for(int i=0;i<symbols.length;i++){ | |
if(ch == symbols[i]){ | |
return true; | |
} | |
} | |
return false; | |
} | |
void seprator(String str){ | |
int i=0; | |
String temp=""; | |
while(i< str.length()){ | |
char ch=str.charAt(i); | |
if((Character.isAlphabetic(ch)) || (Character.isDigit(ch))){ | |
temp=temp+ch; | |
if(iskeyword(temp)){ | |
System.out.println(temp+" is a keyword"); | |
temp=""; | |
} | |
if((i == str.length()-1) && (temp.length() !=0)){ | |
if(Character.isDigit(temp.charAt(0))){ | |
System.out.println(temp+" is a constant"); | |
temp=""; | |
} | |
else{ | |
System.out.println(temp+" is a identifier"); | |
temp=""; | |
} | |
} | |
} | |
else if(isSymbol(ch)){ | |
if(temp.length() !=0){ | |
if(iskeyword(temp)){ | |
System.out.println(temp+" is a keyword"); | |
temp=""; | |
} | |
else if(Character.isDigit(temp.charAt(0))){ | |
System.out.println(temp+" is a constant"); | |
temp=""; | |
} | |
else{ | |
System.out.println(temp+" is a identifier"); | |
temp=""; | |
} | |
} | |
System.out.println(ch+" is a symbol"); | |
} | |
i++; | |
} | |
} | |
public static void main(String[] args) { | |
try{ | |
FileReader in=new FileReader("input.txt"); | |
BufferedReader br=new BufferedReader(in); | |
Lexic_analyzer la=new Lexic_analyzer(); | |
la.cur=br.readLine(); | |
while(la.cur != null){ | |
int i=0; | |
int len=la.cur.length(); | |
while(i < len){ | |
char ch=la.cur.charAt(i); | |
if(ch != ' '){ | |
la.temp=la.temp+ch; | |
} | |
else{ | |
la.seprator(la.temp); | |
la.temp=""; | |
} | |
i++; | |
} | |
la.seprator(la.temp); | |
la.temp=""; | |
la.cur=br.readLine(); | |
} | |
br.close(); | |
}catch(Exception e){ | |
System.out.println(e); | |
} | |
} | |
} | |
## Output | |
int is a keyword | |
main is a identifier | |
( is a symbol | |
) is a symbol | |
{ is a symbol | |
int is a keyword | |
a is a identifier | |
= is a symbol | |
20 is a constant | |
, is a symbol | |
65b is a constant | |
= is a symbol | |
10 is a constant | |
; is a symbol | |
cout is a keyword | |
< is a symbol | |
< is a symbol | |
< is a symbol | |
< is a symbol | |
endl is a keyword | |
; is a symbol | |
return is a keyword | |
0 is a constant | |
; is a symbol | |
} is a symbol |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment