0% found this document useful (0 votes)
91 views4 pages

Factorial Calculation in Java Code

This document contains code for a software construction lab assignment submitted by Danish Abbassi. The code defines a class called SC_LabAssignmen1 with a main method that contains C code to calculate the factorial of a number input by the user. The code then uses regular expressions to identify the keywords, literals, and identifiers in the C code and prints them out.

Uploaded by

ZerghamAli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views4 pages

Factorial Calculation in Java Code

This document contains code for a software construction lab assignment submitted by Danish Abbassi. The code defines a class called SC_LabAssignmen1 with a main method that contains C code to calculate the factorial of a number input by the user. The code then uses regular expressions to identify the keywords, literals, and identifiers in the C code and prints them out.

Uploaded by

ZerghamAli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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);


}

You might also like