Hey there,

First off I'm really new to Java so please bear with me. I need to translate a line of text from one form to another. An example line segment has the following format:

" step(sent(1,A,B,vars(Na,Rv,ped(pk(B),cat(N
a,A)))))"

This means that at step 1, A sends to B that which is contained in vars() i.e. Na, Rv and encryption of Na and A (concatenated) using B's public key [ped(pk(B),cat(Na,A))]

The output format I need is the following:

"Knows_B,time_1(Received(B,time_1,(Na,Rv,enc({Na,A},pk_B))))" (ii)

Basically this is the same information that is in the input format but I just need to translate it to the output format above. From what I've read I think i need to use string tokenisers to separate out the input format and thus build up the output form. However I really am at a loss as to how to begin coding this translation. If anyone has the time could they perhaps give me an example of some code that might solve this problem.
If anyone needs more information regarding the input format and how it's related to the output format I will try and elaborate some more on their connection.

Thanks a million.

Dani AI

Generated

’s hint about tokenizing is a useful starting point, but nested, function-like terms are brittle with simple split/token approaches. A small recursive parser that builds a tiny AST (term + argument list) then walks that tree to produce the target string is more robust and easier to extend. This approach keeps the grammar explicit and avoids fragile string-manipulation rules.

The example below implements a compact recursive parser and a translator in Java. It handles identifiers, numbers and nested calls like step(sent(...)), and maps the common constructs used in the thread (for example: stepKnows_...,time_...(...), sentReceived(...), ped(pk(B),cat(...))enc({...},pk_B)). The parser skips whitespace and builds nodes; the translator pattern-matches node names and assembles the output.

import java.util.*;

public class SimpleTranslator {
    static class Node {
        String name; List<Node> args; boolean atom;
        Node(String v) { name = v; atom = true; }
        Node(String n, List<Node> a) { name = n; args = a; atom = false; }
    }

    static class Parser {
        final String s; int p = 0;
        Parser(String s) { this.s = s; }
        void skip() { while (p < s.length() && Character.isWhitespace(s.charAt(p))) p++; }
        String ident() { skip(); int a = p; while (p < s.length() && (Character.isLetterOrDigit(s.charAt(p)) || s.charAt(p)=='_')) p++; return s.substring(a,p); }
        Node parseNode() {
            skip(); String id = ident(); skip();
            if (p < s.length() && s.charAt(p) == '(') {
                p++; List<Node> args = new ArrayList<>(); skip();
                if (p < s.length() && s.charAt(p) == ')') { p++; return new Node(id, args); }
                while (true) {
                    args.add(parseNode()); skip();
                    if (p < s.length() && s.charAt(p) == ',') { p++; skip(); continue; }
                    if (p < s.length() && s.charAt(p) == ')') { p++; break; }
                    break;
                }
                return new Node(id, args);
            }
            return new Node(id);
        }
    }

    static class Trans {
        String trans(Node n) {
            if (n.atom) return n.name;
            switch (n.name) {
                case "step": return transStep(n);
                case "sent": return transSent(n);
                case "vars": return transVars(n);
                case "ped": return transPed(n);
                case "pk": return transPk(n);
                case "cat": return transCat(n);
                default:
                    List<String> r = new ArrayList<>();
                    for (Node a : n.args) r.add(trans(a));
                    return n.name + "(" + String.join(",", r) + ")";
            }
        }
        String transStep(Node n) {
            Node s = n.args.get(0);
            String recv = s.args.get(2).name;
            String t = s.args.get(0).name;
            return "Knows_" + recv + ",time_" + t + "(" + trans(s) + ")";
        }
        String transSent(Node n) {
            String t = trans(n.args.get(0));
            String to = trans(n.args.get(2));
            String payload = trans(n.args.get(3));
            return "Received(" + to + ",time_" + t + "," + payload + ")";
        }
        String transVars(Node n) {
            List<String> r = new ArrayList<>();
            for (Node a : n.args) r.add(trans(a));
            return "(" + String.join(",", r) + ")";
        }
        String transPed(Node n) {
            String key = trans(n.args.get(0)); // expects pk(...)
            String body = trans(n.args.get(1)); // expects cat(...)
            return "enc(" + body + "," + key + ")";
        }
        String transPk(Node n) {
            return "pk_" + n.args.get(0).name;
        }
        String transCat(Node n) {
            List<String> r = new ArrayList<>();
            for (Node a : n.args) r.add(trans(a));
            return "{" + String.join(",", r) + "}";
        }
    }

    public static void main(String[] args) {
        String in = "step(sent(1,A,B,vars(Na,Rv,ped(pk(B),cat(Na,A)))))";
        Parser p = new Parser(in);
        Node root = p.parseNode();
        System.out.println(new Trans().trans(root));
    }
}

Notes: test with variations (extra whitespace, different ordering) and add defensive checks for malformed input. To extend, add handlers for other term names or normalize name-to-output mappings in a lookup table. This keeps the solution maintainable compared with brittle delimiter-based tokenization.

A string tokenizer will separate that long string in an array of small strings.
I.e.
If you had the string "My@Name@Is@bobby@!" (that's not my name btw ;) ) and you set up a string tokenizer to deliminate by @ you'd end up with an array of 5 elements.
"My","Name","is","bobby","!"
Then you could loop through the array trying to match certain cases.

import java.util.*;
public class TokenizerTest{
	public static void main(String[] args){
		String input = "step(sent(1,A,B,vars(Na,Rv,ped(pk(B),cat(Na,A)))))" 
		String output = null;
		StringTokenizer st = new StringTokenizer(input,")");
		while(st.hasMoreTokens()){
			if(st.nextToken().equals("some string")){
				// do stuff here
			}
		}
		System.out.println("Output: "+ output);
	}
}

I'm not sure how the input relates to the output so you can figure that out. But atleast you should know how now. After you tokenize by ")" you'll be left with 8 tokens:
"step"
"sent"
"1,A,B,vars"
"Na,Rv,ped"
"Pk"
"B"
",cat"
"Na,A"

If you want you could deliminate even further by "," but that's up to you.

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.