views:

426

answers:

2

I am making an application where I need to verify the syntax of each line which contains a command involving a keyword as the first word.
Also, if the syntax is correct I need to check the type of the variables used in the keywords.

Like if there's a print command:

  print "string" < variable < "some another string" //some comments

  print\s".*"((\s?<\s?".*")*\s?<\s?(?'string1'\w+))?(\s*//.*)?

So i made the following Regex:

  \s*[<>]\s*((?'variant'\w+)(\[\d+\])*)

This is to access all words in variant group to extract the variables used and verify their type.
Like this my tool has many keywords and currently I am crudely writing regex for each keyword. And if there's a change tomorrow I would be replacing the respective change everytime everywhere in every keyword.
I am storing a Regex for each keyword in an XML file. However I was interested in making it extensible, where say the specification changes tomorrow so I need to change it only once and it would reflect in all the places something like I transform the print regex to:

print %string% (%<% %string%|%variable%)* %comments%

Now like this, I write a specification for each keyword and write the definition of string, variable, comments in another file which stores their regex. Then I write a parser which parses this string and create a regex string for me.

Is this possible?

Is there any better way of doing this or is there any way I can do this in XML?

+1  A: 

Last time I asked a question like this, someone pointed me to http://www.antlr.org/. Enjoy. :-)

SarekOfVulcan
A: 

I got an idea and made my own replacer. I used %myname% kind of tags to define my regular expression, and i wrote the definition of %myname% tags seperately using regex. Then i scanned the string recursively and converted the occurance of %myname% tags to the specification they had. It did my work.Thanks any ways

Anirudh Goel