In the Gist, I have explained my problem, and I have provided my tokenizer as well. If you can help me with the parser, that would be great. Also if my tokenizer is not very well-structured, please tell me.
btw the tokenizer is written in extremely portable Scheme. I think Chez is the best interpreter for Scheme.
https://github.com/mu-semtech/sparql-parser contains an EBNF parser for SPARQL, an LL(1) language. You might be able to borrow code, not sure how well it translates to scheme. GitHub asked me to log in to see the gist so I’d have to have a peek later.
sparql-ast folder contains the relevant bits regarding the parsing.