Skip to content

Instantly share code, notes, and snippets.

@yutopio
Created March 6, 2018 13:40
Show Gist options
  • Save yutopio/3c127ac047011ef05fadded2385c34a7 to your computer and use it in GitHub Desktop.
Save yutopio/3c127ac047011ef05fadded2385c34a7 to your computer and use it in GitHub Desktop.
Simple math calculator
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="packages\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.props" Condition="Exists('packages\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{EE3E54A6-D9F2-47D9-A017-0E3FCCB12700}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Calc</RootNamespace>
<AssemblyName>Calc</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Antlr4.Runtime, Version=4.6.0.0, Culture=neutral, PublicKeyToken=09abb75b9ed49849, processorArchitecture=MSIL">
<HintPath>packages\Antlr4.Runtime.4.6.4\lib\net45\Antlr4.Runtime.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<Antlr4 Include="Calc.g4">
<Generator>MSBuild:Compile</Generator>
<CustomToolNamespace>Calc</CustomToolNamespace>
</Antlr4>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.props'))" />
<Error Condition="!Exists('packages\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.targets'))" />
</Target>
<Import Project="packages\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.targets" Condition="Exists('packages\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.targets')" />
</Project>
grammar Calc;
/*
* Parser Rules
*/
expr
: expr PLUS expr #addExpr
| expr ASTERISK expr #mulExpr
| LPR expr RPR #parExpr
| NUMBER #numExpr
;
/*
* Lexer Rules
*/
WS: ' ' -> channel(HIDDEN);
fragment DIGIT: [0-9];
NUMBER: DIGIT+;
LPR: '(';
RPR: ')';
PLUS: '+';
ASTERISK: '*';
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Antlr4" version="4.6.4" targetFramework="net461" developmentDependency="true" />
<package id="Antlr4.CodeGenerator" version="4.6.4" targetFramework="net461" developmentDependency="true" />
<package id="Antlr4.Runtime" version="4.6.4" targetFramework="net461" />
</packages>
using System;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
namespace Calc
{
class Program
{
static void Main(string[] args)
{
var inputStream = new AntlrInputStream(Console.ReadLine());
var lexer = new CalcLexer(inputStream);
var token = new CommonTokenStream(lexer);
var parser = new CalcParser(token);
var ctx = parser.expr();
var visitor = new CalcVisitor();
Console.WriteLine(visitor.Visit(ctx));
Console.ReadLine();
}
}
class CalcVisitor : CalcBaseVisitor<int>
{
public override int VisitAddExpr([NotNull] CalcParser.AddExprContext context)
=> Visit(context.expr(0)) + Visit(context.expr(1));
public override int VisitMulExpr([NotNull] CalcParser.MulExprContext context)
=> Visit(context.expr(0)) * Visit(context.expr(1));
public override int VisitParExpr([NotNull] CalcParser.ParExprContext context)
=> Visit(context.expr());
public override int VisitNumExpr([NotNull] CalcParser.NumExprContext context)
=> int.Parse(context.NUMBER().GetText());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment