Hi,
I am having problems creating a good model for this.
and i'll appriciate any help.
Req.
1. parse a predefind language such as arethmatic: 4+5-7*9... with abilty to extend / add more implementations in the future
2. be able to translate the object tree created during the parse, into a string / object in a new language (for example, if i parse arethmatic string and have 2 calculator apps, that recieve input in 2 different ways, i want to be able to send the command to both.
What i did:
I Created a set of Objects such as NumericValueTok en, OperatorToken, BeginBracketTok en, EndBracketToken . each contains a way method parse when given input, checks validity and keeps value in some way.
I Created a ArethmaticParse r object that will contain a valid tree of the objects above after a successfull translation.
This is were problems stated for me:
I tried creating a Visitor pattern for this,
In order to translate the tree into a different language each time, i created a class hirarcy called LanguageContext which is the visitor, it contains a method called Translate that will get a Token Object and translate it.
each Token will have a Translate method that revicevs a LanguageContext
the parser will call the first translate of the first token and the string will be returned through the tree.
The Problem:
each Token req. a different translation so for each context i wanted to create a set of Translate Methods. one for OperatorToken, one for ValueToken, etc.
and each token will go to the appropriate method according to its type.
That will not work, because they all will go to the base classes method.
because they are kept in a generic baseToken tree.
another problem is that the context passed is also passed by the Abstract or base, so that the mothods used are from the base and not from the real object.
i know these things because i tried a little test.
Can anyone recommed a different approche? a fix to the problem.
Thanks,
Koren Shoval
I am having problems creating a good model for this.
and i'll appriciate any help.
Req.
1. parse a predefind language such as arethmatic: 4+5-7*9... with abilty to extend / add more implementations in the future
2. be able to translate the object tree created during the parse, into a string / object in a new language (for example, if i parse arethmatic string and have 2 calculator apps, that recieve input in 2 different ways, i want to be able to send the command to both.
What i did:
I Created a set of Objects such as NumericValueTok en, OperatorToken, BeginBracketTok en, EndBracketToken . each contains a way method parse when given input, checks validity and keeps value in some way.
I Created a ArethmaticParse r object that will contain a valid tree of the objects above after a successfull translation.
This is were problems stated for me:
I tried creating a Visitor pattern for this,
In order to translate the tree into a different language each time, i created a class hirarcy called LanguageContext which is the visitor, it contains a method called Translate that will get a Token Object and translate it.
each Token will have a Translate method that revicevs a LanguageContext
the parser will call the first translate of the first token and the string will be returned through the tree.
The Problem:
each Token req. a different translation so for each context i wanted to create a set of Translate Methods. one for OperatorToken, one for ValueToken, etc.
and each token will go to the appropriate method according to its type.
That will not work, because they all will go to the base classes method.
because they are kept in a generic baseToken tree.
another problem is that the context passed is also passed by the Abstract or base, so that the mothods used are from the base and not from the real object.
i know these things because i tried a little test.
Can anyone recommed a different approche? a fix to the problem.
Thanks,
Koren Shoval
Comment