hroof
نـوعـي : عدد المساهمات : 3 نقاط : 15 تاريخ التسجيل : 07/11/2013
| موضوع: split lexems into token in java type decleration 07/11/13, 10:42 am | |
|
السلام عليكم لوسمحتوا مطلووب مني اكتب بروقرام بالجافا بحيث المستخدم يدخل int a,y; ويقووم البرنامج بفصل كل من int is a keyword a is identifier , is separator y is identifier ; is terminator ااتمنى تساعدوني ... وهنا محاولتي - الكود:
-
import java.util.Scanner; public class lexical
{ public static int i,c; public static String exp; public static final String id = "[a-z A-Z]|[0-9]|[a-z A-Z | 0-9]*";; public static void main(String args[]) { Scanner s=new Scanner(System.in); String[] key={"int","double","String","float"}; String[] operator={"+","-","*","/","="}; String[] comma={","}; String[] terminator={";"}; System.out.print("Enter decleration : "); exp=s.nextLine(); String delims = "[ , ]"; String[] array=exp.split(delims); for(i=0;i<array.length;) { //for keyword for(c=0;c<key.length;c++) { if(array[i].contains(key[c])) { System.out.println(key[c] +" is a keyword"); } } //for operator for(c=0;c<operator.length;c++) { if(array[i].contains(operator[c])) { System.out.println(operator[c] +" is an operator"); } } //for identifier if(array[i].matches(id)) { System.out.println(array[i]+" is an identifier"); } //for comma for(c=0;c<comma.length;c++) { if(array[i].contains(comma[c])) { System.out.println(comma[c]+" is a seperator"); } } //for terminator for(c=0;c<terminator.length;c++) { if(array[i].contains(terminator[c])) { System.out.println(terminator[c]+" is a terminator"); } } i++; } } }
| |
|