// tally.java // Implementes a simple LL(1) Recursive Descent Parser and calculator for simple expressions // Uses 1. lexical analysis (simple calss) using java.io.StreamTokenizer // 2. eunmeration for tokentypes // 3. an LL(1) recursive descent parsing algorithm // 4. a simpel stack to convert to prefixed (cambriagde) notation // 5. binary tree (simple node and tree classes) to build a ascii art tree // // Uses an attribute grammar to calculate the value of the given expression. // Synthesized value attributes are returned as integer values from the methods // that correspond to nonterminals. Inherited subtotal attributes are passed to // the methods as arguments // // // Compile: // javac tally.java // // Execute: // java tally [-parse] [-tree] [<filename>] // import java.io.*; import java.util.*; //************************************************* // Recursive Descent Parser and calculator for simple expressions // Uses an attribute grammar to calculate the value of the given expression. // Synthesized value attributes are returned as integer values from the methods // that correspond to nonterminals. Inherited subtotal attributes are passed to // the methods as arguments // // grammar: // // <expr> -> <term> { <add_op> <expr> } // // <term> -> <factor> { mult_op> <term> } // // <factor> -> '(' <expr> ')' // | identifier // | number // // <add_op> -> '+' | '-' // <mult_op> -> '*' | '/' // // **************************************************************************************** // TALLY CLASS // **************************************************************************************** public class tally { private static int token; // enum: Identifer, Number, Operator private static Lexical lex; // scanner - gets next token private static aStack s; // used to convert to prefixed (cambriagde) notation private static boolean print = false; // show parsing (debug) inof private static boolean art = false; // show ascii art parsing tree (abstract) // ************************************************ // MAIN // ************************************************ public static void main(String argv[]) { // deal with any commandline arguments if (argv.length > 0) { if ( argv[0].equals("-parse") ) { // "-parse" is there show the parsing for (int i=1; i<argv.length; ++i) argv[i-1] = argv[i]; print=true; } if ( argv[0].equals("-tree") ) { // "-tree" is there show the ascii art parsing tree for (int i=1; i<argv.length; ++i) argv[i-1] = argv[i]; art=true; } } // create the stack - for converting to postfix s = new aStack(); // create the lexigraphical analysier lex = new Lexical(); if(print) lex.print=true; // advance to the first token on the input: token = lex.getToken(); // parse expression and get calculated value: int value = expr(); // check if expression ends with ';' and print value if (token == (int) ';') { System.out.println("\nValue = " + value); String cambridge = s.printStack(); if(art) { // printout ascii art parser tree cambridge = cambridge.replaceAll("\\]",""); cambridge = cambridge.replaceAll("\\[",""); cambridge = cambridge.replaceAll("\\)",""); cambridge = cambridge.replaceAll("\\(",""); cambridge = cambridge.replaceAll(" "," "); cambridge = cambridge.replaceAll(" "," "); //System.out.println(cambridge); String[] splitted = cambridge.split(" "); pTree t = new pTree(); Node root = t.constructTree(splitted); System.out.println("abstract parse tree: "); t.print("", t.root, false); } }else System.out.println("Syntax error"); } // ************************************************ // EXPR // <expr> -> <term> { <add_op> <expr> } //************************************************ private static int expr() { s.indentPrint(1, "expr", print); int subtotal = term(); while ( token == (int) '+' || token == (int) '-' ) { int saveOp = token; token = lex.getToken(); if( (int) saveOp == '+' ) subtotal += expr(); else if( (int) saveOp == '-' ) subtotal += expr(); else {System.out.println("error: "+ (char) saveOp +" expected '+' or '-' ");} s.push_op(saveOp); } s.indentPrint(-1, "expr", print); return subtotal; } // ************************************************ // TERM // <term> -> <factor> { mult_op> <term> } //************************************************ private static int term() { s.indentPrint(1, "term", print); int subtotal = factor(); while ( token == (int) '*' || token == (int) '/' ) { int saveOp = token; token = lex.getToken(); if( (int) saveOp == '*' ) subtotal *= term(); else if( (int) saveOp == '/' ) subtotal = subtotal / term(); else {System.out.println("error: "+ (char) saveOp +" expected '*' or '/' ");} s.push_op(saveOp); } s.indentPrint(-1, "term", print); return subtotal; } // ************************************************ // FACTOR // <factor> -> '(' <expr> ')' | '-' <factor> // | identifier | number //************************************************ private static int factor() { s.indentPrint(1, "factor", print); int subtotal = 0; if (token == (int) '(') { token = lex.getToken(); //if(print) System.out.println("Lexeem: "+lex.type()+":"+lex.tokenStr()); subtotal = expr(); if (token == (int) ')') { token = lex.getToken(); //if(print) System.out.println("Lexeem: "+lex.type()+":"+lex.tokenStr()); }else { System.out.println("closing ')' expected"); } } else if (lex.type() == TokenType.Identifer) { token = lex.getToken(); //if(print) System.out.println("Lexeem: "+lex.type()+":"+lex.tokenStr()); // ignore variable names for now // t.stack.push(lex.word()); } else if (lex.type() == TokenType.Number) { token = lex.getToken(); //if(print) System.out.println("Lexeem: "+lex.type()+":"+ lex.tokenStr()); subtotal = (int) lex.value(); s.stack.push(" "+ (int)lex.value()); } else { System.out.println("factor expected"); } s.indentPrint(-1, "factor", print); return subtotal; } } //*************************************************************************** // ENUMERATIONS // eunmeration for types of tokens //*************************************************************************** enum TokenType { Identifer, Number, Operator } //*************************************************************************** // LEXIGRAPHICAL ANALYSIS // Lexigraphical Analysiser // reads input and determines what are the tokens // and pass then to the parse //*************************************************************************** class Lexical { private static StreamTokenizer tokenstream; private static int token; private static TokenType type; private static double num; private static String word; public static boolean print = false; //************************************************ // constructors //************************************************ public Lexical() { this(new String[] {}); } //************************************************ // constructors //************************************************ public Lexical(String argv[]) { try { InputStreamReader reader; if (argv.length > 0) reader = new InputStreamReader(new FileInputStream(argv[0])); else reader = new InputStreamReader(System.in); // create the tokenizer: tokenstream = new StreamTokenizer(reader); tokenstream.ordinaryChar('.'); tokenstream.ordinaryChar('-'); tokenstream.ordinaryChar('/'); } catch (IOException e) { System.err.println(e.getMessage()); e.printStackTrace(); } } //************************************************ // getToken - advance to the next token on the input //************************************************ public static int getToken() { try { token = tokenstream.nextToken(); switch (token) { case StreamTokenizer.TT_NUMBER: type = TokenType.Number; num = tokenstream.nval; if(print)aStack.indentPrint(0, ("Number found: " + num), true); break; case StreamTokenizer.TT_WORD: type = TokenType.Identifer; word = tokenstream.sval; if(print)aStack.indentPrint(0, ("Word found: " + word), true); break; case '+': case '-': case '/': case '*': case '(': case ')': case ';': type = TokenType.Operator; if(print)aStack.indentPrint(0, ("Operator found: " + (char)token), true); break; default: System.out.println("Scanner found niether Identifer, Number, nor Operator"); } } catch (IOException e) { System.err.println(e.getMessage()); e.printStackTrace(); } return token; } //******************************************** // getter //******************************************** public static TokenType type(){ return type; } public static double value(){ return num; } public static String word(){ return word; } //******************************************** // getter //******************************************** public static String tokenStr(){ switch (type) { case Number: return ""+(int)value(); case Identifer: return word(); case Operator: return word(); default: return "unknown"; } } } //*************************************************************************** // BINARY TREE // binary tree for building simple binary tree // so we can do a nice ASCII art printout // only implemented the few needed methods //*************************************************************************** class Node { static int count = 0; String value; Node left, right; Node(){this(null);} Node(String item) { value = item; left = right = null; } @Override public String toString() { return String.format("Data: "+ value + " left: "+ left +" right: " + right+ " count: "+ count); } // function to insert element in binary tree // expression elemenmts are assumed to be in a String array in prefix order Node insert( String prefix[]) { Node temp = this; if ( count >= prefix.length ) { System.out.println("walked off array "+count); System.exit(1); } value = prefix[count]; left=right=null; count++; if ( isOperator(prefix[count-1]) ) { left = new Node(); temp = left.insert( prefix ); right = new Node(); temp = right.insert( prefix ); } return temp; } // is this element an operator ? static boolean isOperator(String s){ if(s.contains("+")) return true; if(s.contains("-")) return true; if(s.contains("*")) return true; if(s.contains("/")) return true; if(s.contains("^")) return true; return false; } } //*************************************************************************** // BINARY TREE // only implemented the few needed methods //*************************************************************************** class pTree { Node root; pTree(){ root = null; } pTree(String data){ root = new Node(data); } // Returns root of constructed tree for given postfix expression Node constructTree(String postfix[]) { // Traverse through every character of input expression root = new Node(postfix[0]); Node temp = root; while (root.count < postfix.length){ temp = temp.insert( postfix ); } return root; } public void print(String prefix, Node n, boolean isLeft) { if (n != null) { print(prefix + " ", n.right, false); System.out.println (prefix + ("|-- ") + n.value); print(prefix + " ", n.left, true); } } } //*************************************************************************** // STACK // Java code for building cambriagde notation // show how operation are grouped //*************************************************************************** class aStack { static int level = 0; // indentation level public static Stack<String> stack = new Stack <String> (); public static String printStack(){ String str = new String(Arrays.toString(stack.toArray())); System.out.println(str); return str; } // tokens get pushed onto the stack in <FACTOR> (later could be <Primary>) // here we pop them off and then // push the OPERATION and its 2 operands (in correct order) public static void push_op(int tok){ String rside = stack.pop(); String lside = stack.pop(); String tmp = ""+(char)tok; String line = " ( "+ tmp + lside + rside + " )"; // build stack.push(new String(line)); // save sub on stack - it will be needed later } // printout user info but kepp track of indentation level // 'amount' could be -1, 0 , or 1 -1 decrements indetation level // 1 increamants indetation // 'str' is string to print public static void indentPrint(int amount, String str, Boolean print){ if(amount>0) level += amount; if(print) for(int i=0; i<level; i++){ System.out.print("\t"); } if(print) { // show parsing info in detail System.out.print(""+level+": "); System.out.println(str); } if(amount<0) level += amount; } }
Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running the Java LTS version 17. Getting started with the OneCompiler's Java editor is easy and fast. The editor shows sample boilerplate code when you choose language as Java and start coding.
OneCompiler's Java online editor supports stdin and users can give inputs to the programs using the STDIN textbox under the I/O tab. Using Scanner class in Java program, you can read the inputs. Following is a sample program that shows reading STDIN ( A string in this case ).
import java.util.Scanner;
class Input {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String inp = input.next();
System.out.println("Hello, " + inp);
}
}
OneCompiler supports Gradle for dependency management. Users can add dependencies in the build.gradle
file and use them in their programs. When you add the dependencies for the first time, the first run might be a little slow as we download the dependencies, but the subsequent runs will be faster. Following sample Gradle configuration shows how to add dependencies
apply plugin:'application'
mainClassName = 'HelloWorld'
run { standardInput = System.in }
sourceSets { main { java { srcDir './' } } }
repositories {
jcenter()
}
dependencies {
// add dependencies here as below
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
}
Java is a very popular general-purpose programming language, it is class-based and object-oriented. Java was developed by James Gosling at Sun Microsystems ( later acquired by Oracle) the initial release of Java was in 1995. Java 17 is the latest long-term supported version (LTS). As of today, Java is the world's number one server programming language with a 12 million developer community, 5 million students studying worldwide and it's #1 choice for the cloud development.
short x = 999; // -32768 to 32767
int x = 99999; // -2147483648 to 2147483647
long x = 99999999999L; // -9223372036854775808 to 9223372036854775807
float x = 1.2;
double x = 99.99d;
byte x = 99; // -128 to 127
char x = 'A';
boolean x = true;
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
// code
} else {
// code
}
Example:
int i = 10;
if(i % 2 == 0) {
System.out.println("i is even number");
} else {
System.out.println("i is odd number");
}
Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.
switch(<conditional-expression>) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
//code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations is known in advance.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(<condition>){
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (<condition>);
Class is the blueprint of an object, which is also referred as user-defined data type with variables and functions. Object is a basic unit in OOP, and is an instance of the class.
class
keyword is required to create a class.
class Mobile {
public: // access specifier which specifies that accessibility of class members
string name; // string variable (attribute)
int price; // int variable (attribute)
};
Mobile m1 = new Mobile();
public class Greeting {
static void hello() {
System.out.println("Hello.. Happy learning!");
}
public static void main(String[] args) {
hello();
}
}
Collection is a group of objects which can be represented as a single unit. Collections are introduced to bring a unified common interface to all the objects.
Collection Framework was introduced since JDK 1.2 which is used to represent and manage Collections and it contains:
This framework also defines map interfaces and several classes in addition to Collections.
Collection | Description |
---|---|
Set | Set is a collection of elements which can not contain duplicate values. Set is implemented in HashSets, LinkedHashSets, TreeSet etc |
List | List is a ordered collection of elements which can have duplicates. Lists are classified into ArrayList, LinkedList, Vectors |
Queue | FIFO approach, while instantiating Queue interface you can either choose LinkedList or PriorityQueue. |
Deque | Deque(Double Ended Queue) is used to add or remove elements from both the ends of the Queue(both head and tail) |
Map | Map contains key-values pairs which don't have any duplicates. Map is implemented in HashMap, TreeMap etc. |