Lexical analysis is a complex subject.
When handling a grammar like this there are several ways to do it, some of which involve a lot of work.
I recommend you use a Recursive Descent Parser. It will make your life much happier.
BTW, that grammar is a little confusing to me the way it is written — it does not clearly delineate terminals and rules. In particular, I am unsure what is meant by “S(S)S”.
Also BTW, that grammar reduces to a single semicolon being a valid program.
Thanks for recursive parser!
I went to read about it. :)
I see what Recursive Descent Parser is not suitable for me, because program must work with any LL(k) grammar.
Ok, um, LOL, RD is for parsing LL(k) grammars...