ANTLR expr sample
/*
1. Visit http://www.antlr.org/wiki/display/ANTLR3/Antlr3CSharpReleases and download antlr-dotnet-tool-xxx.7z
2. unzip the 7z file.
3. Create solution in Visual Studio and add the following reference for the project: antlr3.runtime.dll.
4. Copy this file as program.cs
5. In unziped files, you can find antlr3.exe. Use it to generate necessary cs files: antlr3.exe expr.g3.
6. Add generated cs files to project.
7. Create folder 'sample' in the project and add a text file named Expr1.txt
8. Place the follow content in Expr1.txt, then change the property 'Copy to Output Directory' to "Copy always".
1+1
x=2
y=3
x+y
9. Build and run!
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Antlr.Runtime;
namespace expr
{
class Program
{
static int calc(string input)
{
ANTLRStringStream stream = new ANTLRStringStream(input);
var lexer = new exprLexer( stream );
var tokens = new CommonTokenStream(lexer);
var parser = new exprParser( tokens );
var result = parser.expr().value;
return result;
}
static void standardTest()
{
ANTLRFileStream input = new ANTLRFileStream("sample\\Expr1.txt");
var lexer = new exprLexer( input );
var tokens = new CommonTokenStream(lexer);
var parser = new exprParser( tokens );
parser.prog();
var result = parser.expr().value;
Console.WriteLine("Result = {0}", result);
}
static void Main(string[] args)
{
standardTest();
Console.WriteLine("calc( '1+2' )={0}", calc("1+2"));
Console.WriteLine("calc( '100+200' )={0}", calc("100+200"));
Console.WriteLine("calc( '1000+2000' )={0}", calc("1000+2000"));
Console.WriteLine("calc( '10000+20000' )={0}", calc("10000+20000"));
Console.WriteLine("Parse done.");
Console.ReadLine();
}
}
}