LJMU FET 5129COMP: Infix Language Interpreter Project Report
VerifiedAdded on  2023/06/11
|88
|9426
|328
Project
AI Summary
This project presents a solution to the Infix Language Interpreter assignment for the 5129COMP module. The solution includes Java code for converting infix expressions to postfix, interpreting expressions, and performing lexical analysis and parsing. The code encompasses programs for infix to postfix conversion, interpreter demonstration, postfix to infix conversion, and supporting classes for token management and input handling. It also implements an interpreter to execute source code and print the output. The assignment likely involves expanding a given lexical analyzer/parser and writing a context-free grammar for the language specification, accompanied by an individual report detailing the development process. Desklib provides this complete solution along with other resources for students.

1. Infix java Program:
import java.util.Stack;
class infix
{
//the given operator precedence returned by the function
//if it is returned means it has higher precedence
static int Precious(char choice)
{
switch (choice)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
import java.util.Stack;
class infix
{
//the given operator precedence returned by the function
//if it is returned means it has higher precedence
static int Precious(char choice)
{
switch (choice)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

}
//converting to infix expression in main method
// for expressing post_fix
static String infixToPost_fix(String expect)
{
// empty string initialization
String result = new String("the expression for infix is : ");
// empty stack initialization
Stack<Character> stack = new Stack<>();
for (int j = 0; j<expect.length(); ++j)
{
char ch = expect.charAt(j);
//adding to the output.
if (Character.isLetterOrDigit(ch))
result += ch;
// pushing to the stack.
else if (ch == '(')
stack.push(ch);
//converting to infix expression in main method
// for expressing post_fix
static String infixToPost_fix(String expect)
{
// empty string initialization
String result = new String("the expression for infix is : ");
// empty stack initialization
Stack<Character> stack = new Stack<>();
for (int j = 0; j<expect.length(); ++j)
{
char ch = expect.charAt(j);
//adding to the output.
if (Character.isLetterOrDigit(ch))
result += ch;
// pushing to the stack.
else if (ch == '(')
stack.push(ch);

//checking the condition
else if (ch == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "the Expression is invalid"; // expression checking
else
stack.pop();
}
else //
{
while (!stack.isEmpty() && Precious(ch) <=
Precious(stack.peek()))
result += stack.pop();
stack.push(ch);
}
}
else if (ch == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
result += stack.pop();
if (!stack.isEmpty() && stack.peek() != '(')
return "the Expression is invalid"; // expression checking
else
stack.pop();
}
else //
{
while (!stack.isEmpty() && Precious(ch) <=
Precious(stack.peek()))
result += stack.pop();
stack.push(ch);
}
}
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

// from the stack pope the operators
while (!stack.isEmpty())
result += stack.pop();
return result;
}
// driver method
public static void main(String[] args)
{
String exp = "(x+y-z*x)";
//String exp1= "(x+y+z%x)";
System.out.println(infixToPost_fix(exp));
//System.out.println(infixToPost_fix(exp1));
}
}
2. InterpreterDemo java program:
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class InterpreterDemo {
while (!stack.isEmpty())
result += stack.pop();
return result;
}
// driver method
public static void main(String[] args)
{
String exp = "(x+y-z*x)";
//String exp1= "(x+y+z%x)";
System.out.println(infixToPost_fix(exp));
//System.out.println(infixToPost_fix(exp1));
}
}
2. InterpreterDemo java program:
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class InterpreterDemo {
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

public static boolean precedence(char x, char y) {
String higher = "*/", lower = "+-";
if (x == '(') {
return false;
}
if (x == ')' && y == '(') {
System.out.println(")-(");
return false;
}
if (y == '(') {
return false;
}
if (y == ')') {
return true;
}
if (higher.indexOf(x) > - 1 && lower.indexOf(y) > - 1) {
return true;
}
if (higher.indexOf(x) > - 1 && higher.indexOf(y) > - 1) {
return true;
}
if (lower.indexOf(x) > - 1 && lower.indexOf(y) > - 1) {
return true;
String higher = "*/", lower = "+-";
if (x == '(') {
return false;
}
if (x == ')' && y == '(') {
System.out.println(")-(");
return false;
}
if (y == '(') {
return false;
}
if (y == ')') {
return true;
}
if (higher.indexOf(x) > - 1 && lower.indexOf(y) > - 1) {
return true;
}
if (higher.indexOf(x) > - 1 && higher.indexOf(y) > - 1) {
return true;
}
if (lower.indexOf(x) > - 1 && lower.indexOf(y) > - 1) {
return true;

}
return false;
}
public static String convertToPostfix(String expr) {
Stack<Character> stackOperations = new Stack<>();
StringBuilder out = new StringBuilder();
String operations = "+-*/()";
char topSymbol = '+';
boolean empty;
String[] tokens = expr.split(" ");
for (String token : tokens) {
if (operations.indexOf(token.charAt(0)) == -1) {
out.append(token);
out.append(' ');
} else {
while (!(empty = stackOperations.isEmpty()) &&
precedence(topSymbol = stackOperations.pop(), token.charAt(0))) {
out.append(topSymbol);
out.append(' ');
}
if (!empty) {
stackOperations.push(topSymbol);
return false;
}
public static String convertToPostfix(String expr) {
Stack<Character> stackOperations = new Stack<>();
StringBuilder out = new StringBuilder();
String operations = "+-*/()";
char topSymbol = '+';
boolean empty;
String[] tokens = expr.split(" ");
for (String token : tokens) {
if (operations.indexOf(token.charAt(0)) == -1) {
out.append(token);
out.append(' ');
} else {
while (!(empty = stackOperations.isEmpty()) &&
precedence(topSymbol = stackOperations.pop(), token.charAt(0))) {
out.append(topSymbol);
out.append(' ');
}
if (!empty) {
stackOperations.push(topSymbol);
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

}
if (empty || token.charAt(0) != ')') {
stackOperations.push(token.charAt(0));
} else {
topSymbol = stackOperations.pop();
}
}
}
while (!stackOperations.isEmpty()) {
out.append(stackOperations.pop());
out.append(' ');
}
return out.toString();
}
public static double processPostfix(String postfix, Map <String, Integer> map) {
Stack<Double> stack = new Stack<>();
String operations = "+-*/";
String[] tokens = postfix.split(" ");
for (String token : tokens) {
if (operations.indexOf(token.charAt(0)) == -1) {
double term;
if (empty || token.charAt(0) != ')') {
stackOperations.push(token.charAt(0));
} else {
topSymbol = stackOperations.pop();
}
}
}
while (!stackOperations.isEmpty()) {
out.append(stackOperations.pop());
out.append(' ');
}
return out.toString();
}
public static double processPostfix(String postfix, Map <String, Integer> map) {
Stack<Double> stack = new Stack<>();
String operations = "+-*/";
String[] tokens = postfix.split(" ");
for (String token : tokens) {
if (operations.indexOf(token.charAt(0)) == -1) {
double term;
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

try {
term = Double.parseDouble(token);
} catch (NumberFormatException ex) {
term = map.get(token);
}
stack.push(term);
} else {
double x = stack.pop(), y = stack.pop();
if (token.charAt(0) == '+') {
x = x + y;
}
if (token.charAt(0) == '-') {
x = x - y;
}
if (token.charAt(0) == '*') {
x = x * y;
}
if (token.charAt(0) == '/') {
x =x / y;
}
stack.push(x);
term = Double.parseDouble(token);
} catch (NumberFormatException ex) {
term = map.get(token);
}
stack.push(term);
} else {
double x = stack.pop(), y = stack.pop();
if (token.charAt(0) == '+') {
x = x + y;
}
if (token.charAt(0) == '-') {
x = x - y;
}
if (token.charAt(0) == '*') {
x = x * y;
}
if (token.charAt(0) == '/') {
x =x / y;
}
stack.push(x);

}
}
return stack.pop();
}
public static void main(String[] args) {
String interinfix = "v * 9 / 5 + 32";
String interpostfix = convertToPostfix(interinfix);
System.out.println("Infix: " + interinfix);
System.out.println("Postfix: " + interpostfix);
Map < String, Integer > map = new HashMap<>();
for (int i = 0; i <= 100; i += 10) {
map.put("v", i);
System.out.println("v is " + i + ", G is " + processPostfix(interpostfix, map));
}
}
}
3. Post_fixToInfix java program:
import java.util.Scanner;
import java.util.Stack;
public class Post_fixToInfix
}
return stack.pop();
}
public static void main(String[] args) {
String interinfix = "v * 9 / 5 + 32";
String interpostfix = convertToPostfix(interinfix);
System.out.println("Infix: " + interinfix);
System.out.println("Postfix: " + interpostfix);
Map < String, Integer > map = new HashMap<>();
for (int i = 0; i <= 100; i += 10) {
map.put("v", i);
System.out.println("v is " + i + ", G is " + processPostfix(interpostfix, map));
}
}
}
3. Post_fixToInfix java program:
import java.util.Scanner;
import java.util.Stack;
public class Post_fixToInfix
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

{
private static Scanner SC;
// checking the operators
private boolean isOperator(char c){
if(c == '+' || c == '-' || c == '*' || c =='/' || c == '^')
return true;
return false;
}
// logic for converting infix to post-fix
public String convert(String fixpost){
Stack<String> s = new Stack<>();
for(int j = 0; j < fixpost.length(); j++){
char c = fixpost.charAt(j);
if(isOperator(c)){
String b = s.pop();
String a = s.pop();
s.push("("+a+c+b+")");
private static Scanner SC;
// checking the operators
private boolean isOperator(char c){
if(c == '+' || c == '-' || c == '*' || c =='/' || c == '^')
return true;
return false;
}
// logic for converting infix to post-fix
public String convert(String fixpost){
Stack<String> s = new Stack<>();
for(int j = 0; j < fixpost.length(); j++){
char c = fixpost.charAt(j);
if(isOperator(c)){
String b = s.pop();
String a = s.pop();
s.push("("+a+c+b+")");
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

}
else
s.push(""+c);
}
return s.pop();
}
public static void main(String[] args) {
Post_fixToInfix obj = new Post_fixToInfix();
SC = new Scanner(System.in);
System.out.print("enter the Postfix notation : ");
String fixpost = SC.next();
System.out.println("Infix notation for entered notation is : "+obj.convert(fixpost));
}
}
4. Euclid java program:
package infix;
import java.io.IOException;
import java.util.Hashtable;
else
s.push(""+c);
}
return s.pop();
}
public static void main(String[] args) {
Post_fixToInfix obj = new Post_fixToInfix();
SC = new Scanner(System.in);
System.out.print("enter the Postfix notation : ");
String fixpost = SC.next();
System.out.println("Infix notation for entered notation is : "+obj.convert(fixpost));
}
}
4. Euclid java program:
package infix;
import java.io.IOException;
import java.util.Hashtable;

public class Euclid {
public static final int MAX_NUM_TOKENS = 3000; // You may assume there will be no
more than 3000 tokens
public static Token[] tokenSequence = new Token[MAX_NUM_TOKENS];
public static int currentToken=0;
public static Hashtable<String, IdentToken> idents;
public static boolean lexAnalysisSuccessful = true;
public static void main(String[] args) {
System.out.println("Enter source file path:");
InputData.filePath = System.console().readLine("");
LexAnalyser lex = new LexAnalyser();
Token nextToken = new Token(TokenType.END_OF_LINE);
do {
public static final int MAX_NUM_TOKENS = 3000; // You may assume there will be no
more than 3000 tokens
public static Token[] tokenSequence = new Token[MAX_NUM_TOKENS];
public static int currentToken=0;
public static Hashtable<String, IdentToken> idents;
public static boolean lexAnalysisSuccessful = true;
public static void main(String[] args) {
System.out.println("Enter source file path:");
InputData.filePath = System.console().readLine("");
LexAnalyser lex = new LexAnalyser();
Token nextToken = new Token(TokenType.END_OF_LINE);
do {
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 88
Related Documents

Your All-in-One AI-Powered Toolkit for Academic Success.
 +13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.