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 NumericValueToken, OperatorToken, BeginBracketToken, EndBracketToken. each contains a way method parse when given input, checks validity and keeps value in some way.
I Created a ArethmaticParser 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
4 1629 JosAH 11,448
Recognized Expert MVP
So basically your visitor pattern is broken because each token should simply report
back to the visitor which token it actually is and the visitor should handle it from there.
A Java example: -
interface Visitor {
-
void leftBracketToken(LeftBracketToken visitee);
-
void rightBracketToken(RightBractketToken visitee);
-
void plusToken(PlusToken visitee);
-
void minusToken(MinusToken visitee);
-
void numberToken(NumberToken visitee);
-
// etc. etc.
-
}
-
interface visitee {
-
void visit(Visitor visitor);
-
}
-
An implementation of the Visitor visits a Visitee that'll reveil what it actually is. All
the different tokens should implement this Visitee interface. This pattern is also
called the 'double dispatch' pattern because two 'hops' are needed to get to the
point where something appropriate can be done 1: visitor calls visitee, 2: visitee
calls back on the visitor.
kind regards,
Jos
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)
JosAH 11,448
Recognized Expert MVP
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
Thanks, your ports were very helpful.
I'll do just that.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Boris Boutillier |
last post by:
Hi all,
I'm looking for parsing a Verilog file in my python module,
is there already such a tool in python (a module in progress) to
help instead of doing a duplicate job.
And do you know of some generic parsing module in python, in which you
give some kind of grammar and callbacks ?
Thanks for the help
|
by: bartek |
last post by:
Hello,
I've been pondering with this for quite some time now, and finally
decided to ask here for suggestions. I'm kind of confused, actually...
Maybe I'm thinking too much...
Brain dump follows...
I need a class to represent a variable, with an associated data type
and/or value. Though, I don't want it to be a variant type, and not a
|
by: James Saker |
last post by:
I've been working on a Python class object for APRS (Automatic Position
Reporting System - a digital mode used by amateur radio operators) and have
been going through David Mertz's Text Processing in Python book to help in
the construction of the parser component of the class.
I was curious if there was a good reference on the most pythonic...
|
by: John Doe |
last post by:
I've been doing some reading/research on parsing simple configuration
files through C, and have heard various opinions on the matter. I'd like
to solicit some opinions and design criteria (as well as "gotchas") for
doing this.
I'm implementing a program that needs to read a standard configuration
file, in key=value pairs for starters...
|
by: Matthius |
last post by:
Greetings,
I am a database guys trying to get into C# and design patterns. I really
like the "Head first design patterns" book but don't like that it has
samples in java. Is anyone interested in re-writing all of the java
examples in c# so that the book is usable for C# stundents? I have only a
limited knowledge of both languages but...
| |
by: Andy |
last post by:
Hi all,
I'm building various tiers to support multiple applications. I will
have the data layer, business layer, and serveral UI layers.
Currently I have a Data class when represents a single row in a
specific table. I have a business object which works solely on that
row.
In my business object, I have a method that will retreive the...
|
by: stéphane bard |
last post by:
hello
i would like to parse java files an detect
class name's, attributes name's type's and visibility (and or list of
methods).
is there any module who can parse easily a java file without using
(jython)?
|
by: neelsmail |
last post by:
Hi,
I have been working on C++ for some time now, and I think I have a
flair for design (which just might be only my imagination over-
stretched.. :) ). So, I tried to find a design certification, possibly
that involves C++, but, if not, C++ and UML. All I could find was Java
+ UML design certifications (one such is detailed on...
|
by: =?Utf-8?B?eWRibg==?= |
last post by:
I need to write a program validate a text file in CSV format. So I will have a
class DataType
and a lot of of derived class for various type, e.g. IntType, StringType,
FloatType, MoneyType, ... etc.
For each column of a type, it may accept null/empty value. or not. It may
have various max length for StringType, IntType,... etc.
|
by: pbd22 |
last post by:
Hi.
Anybody know of any good code examples out there on
how to take a telnet command and parse it?
Thanks!
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |