Created
October 20, 2012 07:27
-
-
Save Cellane/3922507 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package PreklProjektSol; | |
| // ********* THIS CODE IS AUTO PORTED FROM C# TO JAVA USING CODEPORTING.COM TECHNOLOGY ********* | |
| import com.codeporting.csharp2java.java.Enum; | |
| import com.codeporting.csharp2java.System.msString; | |
| import java.util.ArrayList; | |
| /*enum*/ final class ElementType extends Enum | |
| { | |
| private ElementType(){} | |
| public static final int VARIABLE = 0; | |
| public static final int CONSTANT = 1; | |
| public static final int NEGATION = 2; | |
| public static final int OPEN = 3; | |
| public static final int CLOSE = 4; | |
| public static final int ALLOWED = 5; | |
| public static final int FLOATING_POINT = 6; | |
| public static final int UNDEFINED = 1000; | |
| static { | |
| Enum.register(new Enum.SimpleEnum(ElementType.class, Integer.class) {{ | |
| addConstant("VARIABLE", VARIABLE); | |
| addConstant("CONSTANT", CONSTANT); | |
| addConstant("NEGATION", NEGATION); | |
| addConstant("OPEN", OPEN); | |
| addConstant("CLOSE", CLOSE); | |
| addConstant("ALLOWED", ALLOWED); | |
| addConstant("FLOATING_POINT", FLOATING_POINT); | |
| addConstant("UNDEFINED", UNDEFINED); | |
| }}); | |
| } | |
| } | |
| class Analyser | |
| { | |
| private String input; | |
| private char element; | |
| private int pos; | |
| private boolean error, negation, negBrackets; | |
| private String code; | |
| private String message; | |
| private ArrayList<String> postfix; | |
| public Analyser(String s) | |
| { | |
| postfix = new ArrayList<String>(); | |
| input = s.replace(" ",""); | |
| element = '\u0000'; | |
| pos = 0; | |
| error = false; | |
| negation = false; | |
| code = ""; | |
| } | |
| public String analyse(/*out*/ ArrayList<String>[] pfix, /*out*/ int[] errorPosition) | |
| { | |
| nextChar(); | |
| expression(); | |
| if (element != '\u0000') | |
| { | |
| errorCode("Expected operator"); | |
| error = true; | |
| } | |
| if (!error) | |
| { | |
| message = msString.plusEqOperator(message, "Analysis successful, expression valid."); | |
| pos = -1; | |
| } | |
| else | |
| { | |
| message = msString.plusEqOperator(message, com.codeporting.csharp2java.System.msString.concat("Expression invalid.\n", code)); | |
| } | |
| errorPosition[0] = pos; | |
| pfix[0] = postfix; | |
| return message; | |
| } | |
| private void expression() | |
| { | |
| if (!error) | |
| { | |
| subexpression(); | |
| do | |
| { | |
| switch (element) | |
| { | |
| case '+': | |
| { | |
| while (element == '+') | |
| { | |
| nextChar(); | |
| subexpression(); | |
| postfix.add("+"); | |
| } | |
| } | |
| break; | |
| case '-': | |
| { | |
| while (element == '-') | |
| { | |
| nextChar(); | |
| subexpression(); | |
| postfix.add("-"); | |
| } | |
| } | |
| break; | |
| } | |
| } while (element == '+' || element == '-'); | |
| if (element != '\u0000' && getElementType() == ElementType.UNDEFINED) | |
| { | |
| errorCode("Operator invalid"); | |
| error = true; | |
| } | |
| } | |
| } | |
| private void subexpression() | |
| { | |
| if (!error) | |
| { | |
| factor(); | |
| do | |
| { | |
| switch (element) | |
| { | |
| case '*': | |
| { | |
| while (element == '*') | |
| { | |
| nextChar(); | |
| factor(); | |
| postfix.add("*"); | |
| } | |
| } | |
| break; | |
| case '/': | |
| { | |
| while (element == '/') | |
| { | |
| nextChar(); | |
| factor(); | |
| postfix.add("/"); | |
| } | |
| } | |
| break; | |
| } | |
| } while (element == '*' || element == '/'); | |
| } | |
| } | |
| private void errorCode(String s) | |
| { | |
| if (!error) | |
| { | |
| message = msString.plusEqOperator(message, com.codeporting.csharp2java.System.msString.concat(s, " at ", pos, ".\n")); | |
| } | |
| } | |
| private void factor() | |
| { | |
| if (!error) | |
| { | |
| switch (getElementType()) | |
| { | |
| case ElementType.NEGATION: | |
| { | |
| nextChar(); | |
| negation = true; | |
| negBrackets =(getElementType() == ElementType.OPEN); | |
| expression(); | |
| } | |
| break; | |
| case ElementType.OPEN: | |
| { | |
| nextChar(); | |
| expression(); | |
| if (negation & negBrackets) | |
| { | |
| addNegationToPostfix(); | |
| } | |
| if (!error) | |
| { | |
| if (getElementType() != ElementType.CLOSE) | |
| { | |
| errorCode("Parenthesis missing"); | |
| error = true; | |
| } | |
| else | |
| { | |
| nextChar(); | |
| } | |
| } | |
| } | |
| break; | |
| case ElementType.VARIABLE: | |
| { | |
| variableProcessing(); | |
| } | |
| break; | |
| case ElementType.FLOATING_POINT: | |
| { | |
| constantProcessing(); | |
| } | |
| break; | |
| case ElementType.ALLOWED: | |
| { | |
| variableProcessing(); | |
| } | |
| break; | |
| case ElementType.CONSTANT: | |
| { | |
| constantProcessing(); | |
| } | |
| break; | |
| default: | |
| { | |
| errorCode("Character invalid, expected variable or constant"); | |
| error = true; | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| private void variableProcessing() | |
| { | |
| { | |
| String variable = ""; | |
| /*ElementType*/int t; | |
| do | |
| { | |
| variable = msString.plusEqOperator(variable, element); | |
| nextChar(); | |
| t = getElementType(); | |
| } while (t == ElementType.VARIABLE || t == ElementType.CONSTANT || t == ElementType.ALLOWED || t == ElementType.FLOATING_POINT); | |
| postfix.add(variable.toString()); | |
| if (negation & !negBrackets) | |
| { | |
| addNegationToPostfix(); | |
| } | |
| } | |
| } | |
| private void constantProcessing() | |
| { | |
| String number = ""; | |
| boolean floatingPoint = false; | |
| boolean exp = false; | |
| /*ElementType*/int t; | |
| do | |
| { | |
| if (element == '.') | |
| { | |
| if (floatingPoint) | |
| { | |
| break; | |
| } | |
| else | |
| { | |
| floatingPoint = true; | |
| } | |
| } | |
| if (element == 'e') | |
| { | |
| if (exp) | |
| { | |
| break; | |
| } | |
| else | |
| { | |
| exp = true; | |
| nextChar(); | |
| if (element == '-' || getElementType() == ElementType.CONSTANT) | |
| { | |
| number = msString.plusEqOperator(number, 'e'); | |
| } | |
| else | |
| { | |
| break; | |
| } | |
| } | |
| } | |
| number = msString.plusEqOperator(number, element); | |
| nextChar(); | |
| t = getElementType(); | |
| } while (t == ElementType.CONSTANT || element == '.' || element == 'e'); | |
| postfix.add(number.toString()); | |
| if (negation & !negBrackets) | |
| { | |
| addNegationToPostfix(); | |
| } | |
| } | |
| private void addNegationToPostfix() | |
| { | |
| postfix.add("-1"); | |
| postfix.add("*"); | |
| negation = false; | |
| negBrackets = false; | |
| } | |
| private void nextChar() | |
| { | |
| if (input.length() > pos) | |
| { | |
| element = input.charAt(pos); | |
| pos++; | |
| } | |
| else | |
| { | |
| element = '\u0000'; | |
| } | |
| } | |
| private /*ElementType*/int getElementType() | |
| { | |
| /*ElementType*/int s; | |
| if (Character.isLetter(element)) | |
| { | |
| s = ElementType.VARIABLE; | |
| } | |
| else | |
| { | |
| if (Character.isDigit(element)) | |
| { | |
| s = ElementType.CONSTANT; | |
| } | |
| else | |
| { | |
| if (element == '(') | |
| { | |
| s = ElementType.OPEN; | |
| } | |
| else | |
| { | |
| if (element == ')') | |
| { | |
| s = ElementType.CLOSE; | |
| } | |
| else | |
| { | |
| if (element == '-') | |
| { | |
| s = ElementType.NEGATION; | |
| } | |
| else | |
| { | |
| if (element == '_') | |
| { | |
| s = ElementType.ALLOWED; | |
| } | |
| else | |
| { | |
| if (element == '.') | |
| { | |
| s = ElementType.FLOATING_POINT; | |
| } | |
| else | |
| { | |
| s = ElementType.UNDEFINED; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return s; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment