Reputation: 171
I have a lexer that I am trying to build. I just want to know why it doesn't create the second list.
%TokenList=[int, add, '(', int, a, ',', int, b, ')'].
lexer([H|TokenList],Output,[Output|X2]):-
lexer_test(H,Output),lexer(TokenList,Output,X2).
lexer_test(H,Output):-
(
H = '\'int\'' -> Output = 'TYPE_INT'
;(H = '\'bool\'' -> Output='TYPE_BOOL'
;(H = '\',\'' -> Output='COMMA'
;(H = '\'=\'' -> Output='ASSIGN'
;(H = '\'let\'' -> Output='LET'
;(H = '\'in\'' -> Output='LET_IN'
;(H = '\'if\'' -> Output='COND_IF'
;(H = '\'then\'' -> Output='COND_THEN'
;(H = '\'else\'' -> Output='COND_ELSE'
;(H = '\'==\'' -> Output='LOGIC_EQ'
;(H = '\'!=\'' -> Output='LOGIC_NOT_EQ'
;(H = '\'>\'' -> Output='LOGIC_GT'
;(H = '\'>=\'' -> Output='LOGIC_GTEQ'
;(H = '\'+\'' -> Output='ARITH_ADD'
;(H = '\'-\'' -> Output='ARITH_SUB'
;(H = '\'(\'' -> Output='OPEN_P'
;(H = '\')\'' -> Output='CLOSE_P'
;(H = '\'0\'' -> Output='INTEGER'
;(H = '\'1\'' -> Output='INTEGER'
;(H = '\'2\'' -> Output='INTEGER'
;(H = '\'3\'' -> Output='INTEGER'
;(H = '\'4\'' -> Output='INTEGER'
;(H = '\'5\'' -> Output='INTEGER'
;(H = '\'6\'' -> Output='INTEGER'
;(H = '\'7\'' -> Output='INTEGER'
;(H = '\'8\'' -> Output='INTEGER'
;(H = '\'9\'' -> Output='INTEGER'
;(Output='IDENTIFIER'
)))))))))))))))))))))))))))).
Don't worry about the second function lexer_test. I can fix that. But I really don't know why I don't get an output list. And if I can make lexer/2 instead of lexer/3 that would be great. And if you could suggest some good reads to learn prolog please do let me know. Thanks.
Upvotes: 0
Views: 57
Reputation: 1557
At the moment, you do not specify a termination condition:
lexer([H|TokenList],Output,[Output|X2]):-
lexer_test(H,Output),lexer(TokenList,Output,X2).
Lexer keeps calling itself (recursion), but you have no other clause specified, and thus Prolog will end up in an endless loop. The condition you are looking for is:
lexer([],_,[]).
as stop condition. This states the condition in which Tokenlist becomes empty.
A good place to start learning prolog (imho) would be: http://www.learnprolognow.org/
Upvotes: 1