Quote:
|
Originally Posted by koren99 Thanks for the input. its probably what i'll do.
But can you / someone else advise if there is a better approch / design that would fit ?
a design that would not force me to detect which token is being parsed.
this is a problem because the parser contains a tree of tokens created according to the user's input and i need to run the visitor on all of these and i don't want to add logic to detect what type of token i send to the visitor.
(they are stored as base tokens in a list) |
If your token set is fixed a visitor pattern is sort of ideal: let the visitor do with
those tokens whatever it wants. You can write a new visitor whenever it's needed.
OTOH if the token set isn't fixed (built-in functions come to mind and identifiers
as in 'sin(x+y)') a visitor pattern for that is hopeless because you have to rewrite
(add to) every visitor when a new token type sees the light.
Scanning and parsing each token up front remains ideal because you don't want
to determine the type of a token over and over again. Building a syntax tree out
of the token stream is fine too because you don't want to determine the syntax
of a stream of tokens over and over again.
It's the translation of one AST (Abstract Syntax Tree) to another representation
that's bothering you but an AST is an ideal 'blue print' representation of the original
input stream. It's just the set of acceptable tokens you have to deal with, but you
already stated that you're dealing with a fixed input language. Simply define that
one as general as possible and fix it.
kind regards,
Jos