Explore Courses Blog Tutorials Interview Questions
0 votes
in AI and Deep Learning by (50.2k points)

I'm writing a Lisp to C translator and I have a problem with handling strings. This is a code that transforms a unary Lisp function to a C equivalent:

define(F) --> fun_unary(F), 

!. fun_unary(F) --> "(define (", label(Fun), spaces, label(Arg1), ")", spaces, expr(Body), ")", {swritef(F, "data *%t(data *%t) { return(%t); }", [Fun, Arg1, Body])}, !. funs([F]) --> define(F), !. funs([F|Fs]) --> define(F), spaces, funs(Fs), !.

Now I want to read any number of functions and return them as a single string. The above funs is the best I could come up with, but it works like this:

?- funs(F, "(define (carzero l) (= (car l) 0)) (define (zero n) (= 0 n))", []). F = ["data *carzero(data *l) { return(eq(car(l), make_atom_int(0))); }", "data *zero(data *n) { return(eq(make_atom_int(0), n)); }"].

While I want something like this:

F = "data *carzero(data *l) { return(eq(car(l), make_atom_int(0))); }\n\ndata *zero(data *n) { return(eq(make_atom_int(0), n)); }".

so that I can nicely swritef is into a complete program, between #includes and main(). An alternative solution is to modify the highest level translator to handle the list. It currently looks like this:

program(P) --> define(F), {swritef(P, "#include \"lisp2c.h\" \n\n%t \nint main() { return 0; }", [F])}, !.

How would I do any of these two? I'm using SWI Prolog.

1 Answer

0 votes
by (108k points)

Code for Prolog program to read two strings and then output the third-string which is a concatenation of both excluding repeating characters in Artificial Intelligence.










        write("Enter first string = "),


        write("Enter second string = "),




        Str2 <> "",






        write("concat string = ",Str1).



        TempStr1 <> "",


        Char1 <> Char2,



        TempStr1 <> "",

        Str3 = Str1.




 For a better understanding of the concatenation of the two lists refer to the following link:

Browse Categories