I’m writing a lexer in Prolog which will be used as a part of functional language interpreter. Language spec allows expressions like for example let \x = x + 2; to occur. What I want lexer to do for such input is to “return”:
[tokLet, tokLambda, tokVar(x), tokEq, tokVar(x), tokPlus, tokNumber(2), tokSColon]
and the problem is, that Prolog seems to ignore the \ character and “returns” the line written above except for tokLambda.
One approach to solve this would be to somehow add second backslash before/after every occurrence of one in the program code (because everything works fine if I change the original input to let \\x = x + 2;) but I don’t really like it.
Any ideas?
EDIT:
If anyone should have similar problems, that’s how I solved it:
main(File) :-
open(File,read,Stream),
read_stream_to_codes(Stream, Codes),
lexer(X,Codes,[]),
... invoke other methods
Where did you get the string
let \x = x + 2;from?I got inspired by that problem and wrote a bit of code, which should be portable to all Prolog implementations:
I tried it, and it worked for me.
Of course, as explained in question 1846199, you can also use
read_line_to_codes/2.