Abdullah047

Lab Task

Apr 15th, 2019
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. package labtask9;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Labtask9 {
  8.  
  9. public static void main(String[] args) {
  10. Scanner scn = new Scanner(System.in);
  11. String Input = scn.nextLine();
  12. if (declaration(Input)) {
  13. System.out.println("This is a declaration statement");
  14. } else if (Mathop(Input)) {
  15. System.out.println("This is a Math Operation statement");
  16. } else if (Digit(Input)) {
  17. System.out.println("This is a Digit statement");
  18. } else if (Digits(Input)) {
  19. System.out.println("This is DIgits statement");
  20. } else if (datatype(Input)) {
  21. System.out.println("This is Dstatype statement");
  22. } else if (logicalOp(Input)) {
  23. System.out.println("This is logicalOp statement");
  24. }else if (identifier(Input)) {
  25. System.out.println("This is Identifier statement");
  26. }else {
  27. System.out.println("This is Invalid statement");
  28. }
  29.  
  30. }
  31.  
  32. public static boolean declaration(String Inp) {
  33. String regexInt = "((int)(\\s+)([_a-zA-Z][_a-zA-Z0-9]*)(\\s*)(=)(\\s*)([0-9]+);)|";
  34. String regexfloat = "((float)(\\s+)([_a-zA-Z][_a-zA-Z0-9]*)(\\s*)(=)(\\s*)([0-9]+)(.)([0-9]+);)|";
  35. String regexString = "((String)(\\s+)([_a-zA-Z][_a-zA-Z0-9]*)(\\s*)(=)(\\s*)([\\w\\W\\s]*);)|";
  36. String regexChar = "((char)(\\s+)([_a-zA-Z][_a-zA-Z0-9]*)(\\s*)(=)(\\s*)([\\w\\W\\s]);)";
  37.  
  38. boolean matches = regexChecker(regexInt + regexfloat + regexString + regexChar, Inp);
  39. return matches;
  40.  
  41. }
  42.  
  43. public static boolean Mathop(String Inp) {
  44.  
  45. boolean matches = regexChecker("\\+|\\*|\\/|\\-", Inp);
  46. return matches;
  47.  
  48. }
  49.  
  50. public static boolean Digit(String Inp) {
  51.  
  52. boolean matches = regexChecker("(\\d)", Inp);
  53. return matches;
  54.  
  55. }
  56.  
  57. public static boolean Digits(String Inp) {
  58.  
  59. boolean matches = regexChecker("(\\d)+", Inp);
  60. return matches;
  61.  
  62. }
  63.  
  64. public static boolean datatype(String Inp) {
  65.  
  66. boolean matches = regexChecker("(int)|(float)|(String)|(char)|(boolean)|(double)", Inp);
  67. return matches;
  68.  
  69. }
  70.  
  71. public static boolean logicalOp(String Inp) {
  72.  
  73. boolean matches = regexChecker("&&|\\|||", Inp);
  74. return matches;
  75.  
  76. }
  77.  
  78. public static boolean identifier(String Inp) {
  79.  
  80. boolean matches = regexChecker("([_a-zA-Z][_a-zA-Z0-9]*)", Inp);
  81. return matches;
  82.  
  83. }
  84.  
  85. public static boolean InvalidIP(String Inp) {
  86.  
  87. boolean matches = regexChecker("^([_a-zA-Z][_a-zA-Z0-9]*)", Inp);
  88. return matches;
  89.  
  90. }
  91.  
  92.  
  93.  
  94. public static boolean regexChecker(String theRegex, String Input) {
  95.  
  96. Pattern checkRegex = Pattern.compile(theRegex);
  97.  
  98. Matcher regexMatcher = checkRegex.matcher(Input);
  99. int count = 0;
  100. boolean matches = false;
  101.  
  102. while (regexMatcher.matches()) {
  103. if (regexMatcher.group().length() != 0) {
  104. //String inp= regexMatcher.group().replaceFirst("(int)|(float)|(String)|char", "");
  105. //System.out.println( Input.trim() );
  106. count++;
  107. matches = true;
  108. return matches;
  109. }
  110.  
  111. System.out.println();
  112.  
  113. }
  114. if (count == 0) {
  115. matches = false;
  116.  
  117. return false;
  118. }
  119. return false;
  120. }
  121. }
Add Comment
Please, Sign In to add comment