[antlr-interest] ANTLR C# Example Demanded

Jim Idle jimi at temporal-wave.com
Thu Aug 16 09:40:26 PDT 2007


Well,

 

Assuming that this grammar is correct then all you need do is look at
the example calling code in the C# wiki, but:

using System;

using System.Text;

using Antlr.Runtime;

...

            try

            {

                string stringThatIsYourInput;

 

                // First create a string stream with which to parse the
expression

                // 

                Antlr.Runtime.ANTLRStringStream input = new
Antlr.Runtime.ANTLRStringStream(stringThatIsYourInput);

 

                // We can now create the lexer for the expression

                //

                HelloLexer lex = new HelloLexer(input);

 

                // And now the token stream

                //

                CommonTokenStream tokens = new CommonTokenStream(lex);

 

                // And finally, the parser

                //

                HelloParser parser = new HelloParser(tokens);

 

                try

                {

                    string retval;  

 

                    retval = parser.stat();

 

                    return retval;

                }

                catch (RecognitionException recEx)

                {

                    Console.Out.WriteLine(recEx.StackTrace);

 

                    return "Invalid expression";

                }

            }

            catch(Exception ex)

            {

                Console.Out.WriteLine(ex.Message);

                return "Unable to create parser";

            }

 

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Mohammed Ibrahim
Sent: Thursday, August 16, 2007 9:21 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] ANTLR C# Example Demanded

 

Please Everybody in this group , i would like to have a good working C#
code written for This simple grammar File 

==========================================

grammar hello;
options
{
   language = CSharp;
}
prog : stat+;
stat returns [string value] : GREETING{$value = $GREETING.text;}
(LETTER{$value +=" "+$LETTER.text; })+;
GREETING: 'welcome'|'greeting'|'hello';
LETTER : ('a'..'z')+;
WS : ('"'|' '|'\r'|'\n'|'\t')+ {skip();};

 

======================================

 

Please Suppose that the Output Lexer and Parser files are named
HelloLexer and HelloParser respectively .

 

Can anybody Show me a working C# Code for this simple Grammar File.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070816/956ddd6d/attachment-0001.html 


More information about the antlr-interest mailing list