please, i need a java code that convert if statement to switch statement (Contrarily)
and convert for statement to while statement (Contrarily). ( deal with nested statement)
masijade 1,351 Industrious Poster Team Colleague Featured Poster
What are you talking about?
You want code to change code? Are you attempting to build some sort of parser or something?
Also, there is nothing "contrary" about the comparison between if/switch and for/while. Those statement groupings do the same things, not "contrary" things, which makes your post even less understandable.
sara_84 4 Light Poster
it is a project, i need a program using java that when i enter if statement covert it to switch and when i enterd switch statement convet it to if (deal with nested loop), do the same with for and while statement
masijade 1,351 Industrious Poster Team Colleague Featured Poster
Well, what do you have so far, and what problems are you having with it. No one is going to write it for you. We will help you fix your mistakes, but not do it for you.
sara_84 4 Light Poster
this is code,
package dex.compiler.parser;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import dex.compiler.model.base.Place;
import dex.compiler.model.expression.Call;
import dex.compiler.model.expression.DirectCall;
import dex.compiler.model.expression.Expression;
import dex.compiler.model.expression.IdentifierExpression;
import dex.compiler.model.expression.Infix;
import dex.compiler.model.statement.Assignment;
import dex.compiler.model.statement.Block;
import dex.compiler.model.statement.CallStatement;
import dex.compiler.model.statement.CatchBlock;
import dex.compiler.model.statement.Comment;
import dex.compiler.model.statement.Declaration;
import dex.compiler.model.statement.DecrementStatement;
import dex.compiler.model.statement.ForStatement;
import dex.compiler.model.statement.IfStatement;
import dex.compiler.model.statement.IncrementStatement;
import dex.compiler.model.statement.ReturnStatement;
import dex.compiler.model.statement.Statement;
import dex.compiler.model.statement.SwitchCase;
import dex.compiler.model.statement.SwitchStatement;
import dex.compiler.model.statement.ThrowStatement;
import dex.compiler.model.statement.TryStatement;
import dex.compiler.model.statement.WhileStatement;
import dex.compiler.model.type.BasicTypeNode;
import dex.compiler.model.type.TypeNode;
/**
* A parser for statement objects.
*/
class StatementParser extends ExpressionParser {
/**
* Character sequences that indicate a type tail.
*/
final private static List<String> TYPE_TAILS = Collections.unmodifiableList(Arrays.asList(
new String[] { "[]", "<(" }
));
/**
* The keywords that begin blocks in a switch statement.
*/
final private static List<String> SWITCHES = Collections.unmodifiableList(Arrays.asList(
new String[] { "case", "default" }
));
/**
* Constructs a new StatementParser.
*
* @param config the configuration for the new parser
*/
public StatementParser(ParserConfig config) {
super(config);
}
/**
* Parses a declaration. Expects a type followed by an identifier
* and optional initializer.
*
* @return the parsed declaration
*/
public Statement parseDeclaration() {
TypeNode t = parseType();
return parseDeclaration(t);
}
/**
* Parses a declaration whose type was already parsed.
*
* @param t the previously parsed type
* @return the newly parsed declaration statement
*/
public Statement parseDeclaration(TypeNode t) {
String id = parseIdentifier();
Expression initializer = null;
char ch = input.skipAndPeek();
if (ch == '=') {
input.expect('=');
initializer = parseExpression();
}
return new Declaration(input.getPlace(), t, id, initializer);
}
/**
* Parses a statement.
*
* @return the parsed statement
*/
public Statement parseStatement() {
// New local scope?
if (input.skipAndPeek() == '{') {
return parseBlock();
}
if (input.skipAndPeek() == '/') {
return parseComment();
}
// Check all the control flow reserved words...
if (testKeyword("while")) {
return parseWhile();
}
if (testKeyword("for")) {
return parseFor();
}
if (testKeyword("if")) {
return parseIf();
}
if (testKeyword("switch")) {
return parseSwitch();
}
if (testKeyword("try")) {
return parseTry();
}
if (testKeyword("return")) {
return parseReturn();
}
if (testKeyword("void")) {
return parseVoid();
}
if (testKeyword("throw")) {
return parseThrow();
}
// Okay, at this point we know it's not a reserved word.
// The next thing in the stream might be a type.
// We can quickly tell whether it's a local user type.
char ch = input.skipAndPeek();
if (ch == '#') {
return parseDeclaration();
}
// Okay, not an obvious type, anyway.
// But the next thing in the stream MUST be an identifier.
// The identifier can be a type (signifying a declaration),
// or a function call, or a variable.
String id = parseIdentifier();
// If [] follows the id, that indicates a dynamic array type.
// If <( follows the id, that indicates a function type.
// If a type is indicated, then this statement must be a declaration.
if (input.test(TYPE_TAILS, AnyBreaks.INSTANCE) != null) {
TypeNode t = new BasicTypeNode(input.getPlace(), false, getName(id));
return parseDeclaration(parseTypeTail(t));
}
// If ( follows the id, that indicates a function call.
if (input.skipAndPeek() == '(') {
DirectCall dc = new DirectCall(input.getPlace(), id, parseExpressionList());
return new CallStatement(input.getPlace(), dc);
}
ch = input.skipAndPeek();
// An identifier followed by another identifier is a declaration
// (identifier 1 is the type, followed by the local variable name)
if (Character.isJavaIdentifierStart(ch)) {
BasicTypeNode t = new BasicTypeNode(input.getPlace(), false, getName(id));
return parseDeclaration(t);
}
Expression lvalue;
// Not a block.
// Not a control statement.
// Not a declaration.
// It must be an assignment.
lvalue = new IdentifierExpression(input.getPlace(), id);
lvalue = parseOperandTail(lvalue);
// ...except I lied, the above parseOperandTail may have returned
// an expression chain that ends in a Call, eg:
//
// f.x.y[2].foo()
//
// Since you can't assign to the result of a function call,
// we want to convert that call expression to a statement
// and return that.
if (lvalue instanceof Call) {
Call call = (Call)lvalue;
return new CallStatement(input.getPlace(), call);
}
// Aren't you glad I did this by hand instead of using bison?
// It must be an assignment. We've got the lvalue; now we
// need the assignment operator.
// Test for special case of ++
if (input.test("++", SymbolBreaks.INSTANCE)) {
input.expect("++", SymbolBreaks.INSTANCE);
return new IncrementStatement(input.getPlace(), lvalue);
}
// Also --
if (input.test("--", SymbolBreaks.INSTANCE)) {
input.expect("--", SymbolBreaks.INSTANCE);
return new DecrementStatement(input.getPlace(), lvalue);
}
Infix assignmentOp;
ch = input.skipAndPeek();
if (ch == '=') {
assignmentOp = null; // null value represents straight assignment
} else {
String symbol = pickOperator();
if (symbol == null) {
raise("Expected assignment operator.");
}
assignmentOp = Infix.get(symbol);
}
// Otherwise, whatever operator we just parsed must be follwed
// by an = sign since it's an assignment.
// Eg, if we parsed + then we expect +=
input.expect('=');
Expression rvalue = parseExpression();
return new Assignment(input.getPlace(), assignmentOp, lvalue, rvalue);
}
/**
* Parses a return statement.
*
* @return the parsed return statement
*/
public ReturnStatement parseReturn() {
input.skipAndExpect("return", WordBreaks.INSTANCE);
return new ReturnStatement(input.getPlace(), parseExpression());
}
/**
* Parses a return statement that does not return a value.
*
* @return a return statement that does not return a value
*/
public ReturnStatement parseVoid() {
Place place = input.getPlace();
input.skipAndExpect("void", WordBreaks.INSTANCE);
return new ReturnStatement(place, null);
}
/**
* Parses a throw statement
*
* @return the parsed throw statement
*/
public ThrowStatement parseThrow() {
input.skipAndExpect("throw", WordBreaks.INSTANCE);
return new ThrowStatement(input.getPlace(), parseExpression());
}
/**
* Parses a block of statements.
*
* @return the parsed block
*/
public Block parseBlock() {
input.skipAndExpect('{');
List<Statement> statements = new ArrayList<Statement>();
char ch = input.skipAndPeek();
while ((ch != 0) && (ch != '}')) {
statements.add(parseStatement());
ch = input.skipAndPeek();
}
input.expect('}');
return new Block(input.getPlace(), statements);
}
/**
* Parses a while statement.
*
* @return the parsed while statement
*/
public WhileStatement parseWhile() {
input.skipAndExpect("while", WordBreaks.INSTANCE);
Expression test = parseExpression();
Statement body = parseStatement();
return new WhileStatement(input.getPlace(), test, body);
}
/**
* Parses a for statement.
*
* @return the parsed for statement
*/
public ForStatement parseFor() {
input.skipAndExpect("for", WordBreaks.INSTANCE);
Statement initializer = parseStatement();
input.skipAndExpect(';');
Expression test = parseExpression();
input.skipAndExpect(';');
Statement modifier = parseStatement();
Statement body = parseStatement();
return new ForStatement(input.getPlace(), initializer, test, modifier, body);
}
/**
* Parses an if statement.
*
* @return the parsed if statement
*/
public IfStatement parseIf() {
input.skipAndExpect("if", WordBreaks.INSTANCE);
Expression test = parseExpression();
Statement positive = parseStatement();
Statement negative;
if (testKeyword("else")) {
input.expect("else", WordBreaks.INSTANCE);
negative = parseStatement();
} else {
negative = null;
}
return new IfStatement(input.getPlace(), test, positive, negative);
}
/**
* Parses a switch statement.
*
* @return the parsed switch statement
*/
public SwitchStatement parseSwitch() {
input.skipAndExpect("switch", WordBreaks.INSTANCE);
Expression test = parseExpression();
List<SwitchCase> cases = new ArrayList<SwitchCase>();
Statement defaultStatement = null;
input.skipAndExpect('{');
while (input.skipAndTest(SWITCHES, WordBreaks.INSTANCE) != null) {
if (testKeyword("case")) {
cases.add(parseCase());
} else {
// must be default keyword
input.expect("default", WordBreaks.INSTANCE);
if (defaultStatement != null) {
raise("More than one default for switch.");
}
defaultStatement = parseStatement();
}
}
input.skipAndExpect('}');
return new SwitchStatement(input.getPlace(), test, cases, defaultStatement);
}
/**
* Parses a case of a switch statement.
*
* @return the parsed case
*/
public SwitchCase parseCase() {
input.skipAndExpect("case", WordBreaks.INSTANCE);
List<Expression> tests = new ArrayList<Expression>();
tests.add(parseExpression());
char ch = input.skipAndPeek();
while (ch == ',') {
input.expect(',');
tests.add(parseExpression());
ch = input.skipAndPeek();
}
Statement body = parseStatement();
return new SwitchCase(input.getPlace(), tests, body);
}
/**
* Parses a try/catch/finally statement.
*
* @return the parsed try/catch/finally statement
*/
public TryStatement parseTry() {
input.skipAndExpect("try", WordBreaks.INSTANCE);
Block trySection = parseBlock();
List<CatchBlock> catchSection = new ArrayList<CatchBlock>();
Block finallySection = null;
while (testKeyword("catch")) {
input.expect("catch", WordBreaks.INSTANCE);
TypeNode type = parseType();
String id = parseIdentifier();
Block block = parseBlock();
catchSection.add(new CatchBlock(input.getPlace(), type, id, block));
}
if (testKeyword("finally")) {
input.expect("finally", WordBreaks.INSTANCE);
finallySection = parseBlock();
}
return new TryStatement(input.getPlace(), trySection, catchSection, finallySection);
}
/**
* Parses a comment.
*
* @return the parsed comment
*/
public Comment parseComment() {
char ch = input.skipAndPeek();
if (ch != '/') {
raise("Expected comment.");
}
input.expect('/');
ch = input.peek();
String message;
if (ch == '*') {
input.expect('*');
message = input.readUntil("*/");
} else if (ch == '/') {
input.expect('/');
message = input.readUntil("\n");
} else {
raise("Expected comment.");
throw new AssertionError();
}
return new Comment(input.getPlace(), message);
}
}
rajatC -1 Junior Poster
oopss....such a long code....
is evey if statement convertible to switch...isn't it possible only in nested if's not mutually exclusive
Jishnu 160 Posting Pro
Code tags, please :D. Masijade told you to mention the problems you are getting. You've not done it yet.
is evey if statement convertible to switch...isn't it possible only in nested if's not mutually exclusive
True. switch-case statements can check only integer or character values.
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
switch-case statements can check only integer or character values.
To be more precise, switch statements can handle all Integral types except long. Character values and Integral values are not separate entities as explained below.
A primitive type is predefined by the Java programming language and named by
its reserved keyword (§3.9):
PrimitiveType:
NumericType
boolean
NumericType:
IntegralType
FloatingPointType
IntegralType: one of
byte short int long char
FloatingPointType: one of
float double
Edited by mike_2000_17 because: Fixed formatting
Jishnu commented: Right. I was wrong in considering char as a different type. +2
sara_84 4 Light Poster
Hello Friends,
am doing Comp. Science project
I need a Project description mosty in JAVA(comiler )
Something which conver if statement to switch and for to while
thanks for all:'(
darkagn 315 Veteran Poster Featured Poster
so what have you done so far?
peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster
asking for lot of help in other forums like c/c++, asembley, you know the usual sort of things...
masijade 1,351 Industrious Poster Team Colleague Featured Poster
Jishnu 160 Posting Pro
so what have you done so far?
sara_84 has posted the code, but hasn't bothered yet to mention what are the problems regarding it.
am doing Comp. Science project
I need a Project description mosty in JAVA(comiler )
Your keyboard is somewhat nasty ;). If you are doing a project, you are the one who is supposed to know about it. How can anyone else write its description for you?
darkagn 315 Veteran Poster Featured Poster
sara_84 has posted the code, but hasn't bothered yet to mention what are the problems regarding it.
Sorry about that Jishnu, sara_84 posted in two separate threads and a moderator has linked them. Now it looks like I asked her to post when she had already posted!
peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster
Do not worry so far sara_84 did not reply to anything originaly posted in first or second post. I would say it is pass deadline.
Anyone want to help me with coursework?. Joking:P
darkagn commented: lol +1
Jishnu 160 Posting Pro
Sorry about that Jishnu, sara_84 posted in two separate threads and a moderator has linked them. Now it looks like I asked her to post when she had already posted!
Understood.
Do not worry so far sara_84 did not reply to anything originaly posted in first or second post. I would say it is pass deadline.
Yes, I agree.
darkagn commented: You are always so nice to everyone! +1
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.