PDA

View Full Version : reg expression



mickey
4th July 2007, 13:06
Hi,
I have a regular expression. I had to write the .h and .cpp to rappresent it! THen I have to write a parser for it. Any suggests? Do anyone know any "good" link for same code examples in C++?

thanks

wysota
4th July 2007, 17:10
Search the forum (and/or Google) for "finite state machine".

jacek
4th July 2007, 17:23
When it comes to parsing the Dragon Book (http://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools) is one of the best books. Introduction to Automata Theory, Languages, and Computation (http://infolab.stanford.edu/~ullman/ialc.html) by J. Hopcroft and J. Ullman covers the theoretical side of regular expressions and Mastering Regular Expressions (http://regex.info/) by J. Friedl a presents practical approach (but after reading this one you will only know how to use them, not how to code your own regexp engine).

Check QRexExp sources to see an example implementation.

mickey
4th July 2007, 18:25
I read dragon book but at the moment my problem is the simple class to rappresent a SIMPLE reg. expression. So QregExpr is very difficulty for me. I'm reading it but I think I need a simpler example to start. I can't write the class because I don't understand what "rappresent regular expression with class" means....thanks

jacek
4th July 2007, 20:20
I can't write the class because I don't understand what "rappresent regular expression with class" means
Probably you have to write a class similar to QRegExp.

mickey
5th July 2007, 20:36
hi, I can't understand that class so well because it uses many unknows qt methods. Leave out the problem of code a Parser. now, to write .h and .cpp to rappresent a reg expression (that include: characters, list symbol [c1-c2], and '|' symbol). How can I represent it? (Maybe should I code an lexycal analizer or a symbol table?. )

jacek
5th July 2007, 20:51
now, to write .h and .cpp to rappresent a reg expression (that include: characters, list symbol [c1-c2], and '|' symbol). How can I represent it?
You should start from .h file and define the interface of your class.

Here's a template:

#include <QString>

class RegExp
{
public:
RegExp( const QString & regExp );
RegExp( const RegExp & regExp );
RegExp & operator=( const RegExp & regExp );
...
};
So far it allows you to create and copy regular expression objects. Now you have to add one or more methods that will allow you to use it.

mickey
5th July 2007, 21:10
excuse me: then then problem of to treat symbols '|' and '[list] and '*' (kleene star) is here, in this stage or only in the parser stage? I thought I need treat those symbols in definition of reg expression. And this is what I can't understand (how to treat only | for example)
thanks

jacek
5th July 2007, 21:21
I thought I need treat those symbols in definition of reg expression. And this is what I can't understand (how to treat only | for example)
IMO this should be hidden in the implementation. If you want to use an object that represents a regular expression you are not interested in the structure of that expression (for example whether it contains | and where) , but whether given string matches it or not. Therefore the interface of your class should contain methods that will allow you to use regular expressions and the parser, automaton and other stuff should be hidden in .cpp file.

PS. Kleene's star in Polish is called "operator tranzytywnego domknięcia konkatenacji". Great name, isn't it? ;)