Gopinath M

Hi All,

First of all thanks you very much for spending your time to open this post. We are trying to implement a small parser which evaluates expression given by users. We have idenfied a set of operators, functions, keywords, conditional staments which are required for the language. Can you please tell us how shall we implement a parser which checks syntax of the expression and evalutes them If you have any reference to data structure concepts, please let me know.

Many thanks in advance.

Cheers,

Gopi



Re: Visual C# General Implementing an expression parser

carlop

You should convert expressions into the well know 'Polish Notation', from that you can easily evaluate it:<br>http://www.google.it/url sa=t&amp;amp;ct=res&amp;amp;cd=1&amp; amp;url=http%3A%2F%2F www.codeproject.com%2Fstring%2FSParser.asp%3Fdf%3D100%26forumid%3D37630%26exp%3D0%26select%3D791980&amp; amp;ei=wwDHRYeOLYKQ2ALCrqjGDw&amp;amp;usg=__wDsd4qg84hizJhyZozcbjEnJUeI=&amp;amp; sig2=TM2P1KWp-NIdMulkv80Q4g<br><br><br><br>

<Moderator: Broke up ulr for easier reading>




Re: Visual C# General Implementing an expression parser

micvos

Sounds like you're trying to build an interpreter. If you're new to the subject, read some articles on languages and grammars. It's not an easy subject. First you'll have to build a tokenizer. Then you'll have to build a parser based on you're language syntax, most probably it uses lots of recursion. Depending on what you're trying to do, you'll have to build a symbol table to store your variables en function names. There's no straight forward answer and it can be a lot of work depending of the size of your language you have defined. Maybe I can help you get started if you show me the language.




Re: Visual C# General Implementing an expression parser

Gopinath M

Hi carlop, can you the link which you provided in post again. I'm not able to access the link.



Re: Visual C# General Implementing an expression parser

Gopinath M

micvos,

My expression language looks similar to this..

( Input.Surname is null AND lenght(Input.forname) < 10 true : false)

If an expression of the above kind is provided by the user, then I've to validate the syntax, make sure that the expression is going to return either ture or false value only. After validating the expression, I've convert the expression in to an XML format which is understood by another component. The component which understands the xml format outputs either SQL Queries or C# as requested by the user.

Cheers,

Gopi





Re: Visual C# General Implementing an expression parser

micvos

I'm sorry, but I can't help you there because this is not really a language defenition. To build a parser you need something like a syntax defined in BNF or some other equivalent form.




Re: Visual C# General Implementing an expression parser

carlop

Sorry for the mistake, here the complete link:
http://www.codeproject.com/string/SParser.asp df=100&forumid=37630&exp=0&select=791980



Carlo Pagliei . sw developer . www.carlop.com




Re: Visual C# General Implementing an expression parser

Adam Slosarski

Hi, Look for great tool "Visual BNF" at www.intralogic.eu



Re: Visual C# General Implementing an expression parser

TilakGopi

Hi,

U can use Regular expressions also to validate the input strings read from file.

FYI: http://msdn2.microsoft.com/en-us/library/ms228595(VS.80).aspx , going through this post will help u much.

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# General Implementing an expression parser

OmegaMan

If lexical analysis is needed check out this post in the Regular Expression Forums where a user has identified a lexical parser and given an example in this thread Lexical Analysis using GPLEX .





Re: Visual C# General Implementing an expression parser

CalvinR

I would suggest that you start by modelling your language in BNF notation, you can build regular expressions from that, and then use those as input to a Scanner Generator/Lexical Analyser, which will create the code for your pareser. It's not terribly hard to do but it's not so easy that someone can just whip one up and put here for you. But I really suggest you create BNF notation first, from there everything will follow.


http://en.wikipedia.org/wiki/Backus-Naur_form
http://www.google.ca/search q=C%23+Scanner+Generator