LAB ASSIGNMENT # 1
SOFTWARE CONTRUCTION
Danish Abbassi
FA14-BSE-040
CODE :
public class SC_LabAssignmen1 {
public static void main(String[] args) {
String Codding = "#include <stdio.h>\n"
+ "int main()\n"
+ "{\n"
+" int n, i;\n"
+" unsigned long long factorial = 1;\n"
+ "\n"
+" printf(\"Enter an integer: \");\n"
+" scanf(\"%d\",&n);\n"
+ "\n"
+" // show error if the user enters a negative integer\n"
+" if (n < 0)\n"
+" printf(\"Error! Factorial of a negative number doesn't
exist.\");\n"
+ "\n"
+" else\n"
+" {\n"
+" for(i=1; i<=n; ++i)\n"
+" {\n"
+" factorial *= i; // factorial = factorial*i;\n"
+" }\n"
+" printf(\"Factorial of %d = %llu\", n, factorial);\n"
+" }\n"
+ "\n"
+" return 0;\n"
+ "}";
String keys = "double|int|struct\n"
+ "break|else|long|switch\n"
+ "case|enum|typedef\n"
+ "char|extern|return|union\n"
+ "const|float|short|unsigned\n"
+ "continue|for|signed|void\n"
+ "default|goto|sizeof|volatile\n"
+ "do|if|static|while";
String literal = "0|1";
String identifier = "n|i|factorial";
//Keywords
String Kwords = "";
Pattern p = Pattern.compile(keys);
Matcher m = p.matcher(Codding);
while (m.find()) {
Kwords = Kwords+ " " + m.group(0);
//Literals
String literals = "";
Pattern pa1 = Pattern.compile(literal);
Matcher ma1 = pa1.matcher(Codding);
while (ma1.find()) {
literals = literals + " " + ma1.group(0);
//Identifiers
String iden = "";
Pattern p2 = Pattern.compile(identifier);
Matcher m2 = p2.matcher(Codding);
while (m2.find()) {
iden = iden + " " + m2.group(0);
System.out.println("Identifiers Are: \n" + iden);
System.out.println("Keywords Are: \n" + Kwords);
System.out.println("Literals Are: \n" + literals);
}