LJMU FET 5129COMP: Infix Language Interpreter Project Report

Verified

Added 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.
Document Page
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;
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
}
//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);
Document Page
//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);
}
}
Document Page
// 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 {
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
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;
Document Page
}
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);
Document Page
}
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;
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
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);
Document Page
}
}
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
Document Page
{
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+")");
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
}
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;
Document Page
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 {
chevron_up_icon
1 out of 88
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]