Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package labtask9;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Labtask9 {
- public static void main(String[] args) {
- Scanner scn = new Scanner(System.in);
- String Input = scn.nextLine();
- if (declaration(Input)) {
- System.out.println("This is a declaration statement");
- } else if (Mathop(Input)) {
- System.out.println("This is a Math Operation statement");
- } else if (Digit(Input)) {
- System.out.println("This is a Digit statement");
- } else if (Digits(Input)) {
- System.out.println("This is DIgits statement");
- } else if (datatype(Input)) {
- System.out.println("This is Dstatype statement");
- } else if (logicalOp(Input)) {
- System.out.println("This is logicalOp statement");
- }else if (identifier(Input)) {
- System.out.println("This is Identifier statement");
- }else {
- System.out.println("This is Invalid statement");
- }
- }
- public static boolean declaration(String Inp) {
- String regexInt = "((int)(\\s+)([_a-zA-Z][_a-zA-Z0-9]*)(\\s*)(=)(\\s*)([0-9]+);)|";
- String regexfloat = "((float)(\\s+)([_a-zA-Z][_a-zA-Z0-9]*)(\\s*)(=)(\\s*)([0-9]+)(.)([0-9]+);)|";
- String regexString = "((String)(\\s+)([_a-zA-Z][_a-zA-Z0-9]*)(\\s*)(=)(\\s*)([\\w\\W\\s]*);)|";
- String regexChar = "((char)(\\s+)([_a-zA-Z][_a-zA-Z0-9]*)(\\s*)(=)(\\s*)([\\w\\W\\s]);)";
- boolean matches = regexChecker(regexInt + regexfloat + regexString + regexChar, Inp);
- return matches;
- }
- public static boolean Mathop(String Inp) {
- boolean matches = regexChecker("\\+|\\*|\\/|\\-", Inp);
- return matches;
- }
- public static boolean Digit(String Inp) {
- boolean matches = regexChecker("(\\d)", Inp);
- return matches;
- }
- public static boolean Digits(String Inp) {
- boolean matches = regexChecker("(\\d)+", Inp);
- return matches;
- }
- public static boolean datatype(String Inp) {
- boolean matches = regexChecker("(int)|(float)|(String)|(char)|(boolean)|(double)", Inp);
- return matches;
- }
- public static boolean logicalOp(String Inp) {
- boolean matches = regexChecker("&&|\\|||", Inp);
- return matches;
- }
- public static boolean identifier(String Inp) {
- boolean matches = regexChecker("([_a-zA-Z][_a-zA-Z0-9]*)", Inp);
- return matches;
- }
- public static boolean InvalidIP(String Inp) {
- boolean matches = regexChecker("^([_a-zA-Z][_a-zA-Z0-9]*)", Inp);
- return matches;
- }
- public static boolean regexChecker(String theRegex, String Input) {
- Pattern checkRegex = Pattern.compile(theRegex);
- Matcher regexMatcher = checkRegex.matcher(Input);
- int count = 0;
- boolean matches = false;
- while (regexMatcher.matches()) {
- if (regexMatcher.group().length() != 0) {
- //String inp= regexMatcher.group().replaceFirst("(int)|(float)|(String)|char", "");
- //System.out.println( Input.trim() );
- count++;
- matches = true;
- return matches;
- }
- System.out.println();
- }
- if (count == 0) {
- matches = false;
- return false;
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment