Skip to content
Permalink
Browse files

Updated ANTLR Grammar, Parser, Visitor and ErrorListener

  • Loading branch information...
pascalpfeil committed Apr 23, 2018
1 parent 88cdb72 commit 4084a7f2eea7a0685b57b7953fff9c13a678aa20
@@ -0,0 +1,131 @@
grammar Oc5;

// Parser
ocfile: VERSION module;

module:
MODULE IDENTIFIER constants? signals? variables? actions? automaton ENDMODULE;

constants: CONSTANTS NUMBER (constant)* ENDTABLE;
constant: LIST_INDEX IDENTIFIER index;

signals: SIGNALS NUMBER (signal)* ENDTABLE;
signal: LIST_INDEX nature channel bool?;
nature: input | output;
input: INPUT IDENTIFIER presAction;
presAction: index | HYPHEN;
output: OUTPUT IDENTIFIER outAction;
outAction: index | HYPHEN;
channel: pure | single | multiple;
pure: PURE;
single: SINGLE index;
multiple: MULTIPLE index index;
bool: BOOL index;

variables: VARIABLES NUMBER (variable)* ENDTABLE;
variable: LIST_INDEX index;

actions: ACTIONS NUMBER (action)* ENDTABLE;
action: LIST_INDEX (testAction | linearAction);
testAction: presentAction | ifAction | dszAction;
presentAction: PRESENT index;
ifAction: IF expression;
dszAction: DSZ index;
linearAction: callAction | outputAction;
callAction:
CALL index OPEN_PARENTHESIS index CLOSE_PARENTHESIS OPEN_PARENTHESIS expression
CLOSE_PARENTHESIS;
outputAction: OUTPUT index;

automaton: states startpoint calls (state)* ENDTABLE;
states: STATES NUMBER;
startpoint: STARTPOINT index;
calls: CALLS NUMBER;
state: LIST_INDEX actionTree;
actionTree: closedDag | openTestList closedDag;
closedDag: newState | closedTest /*| newClosedDag*/;
newState:
linearActionList LESS_THAN_SIGN NUMBER GREATER_THAN_SIGN
/* pragmaList */;
closedTest:
linearActionList index /* what index? */ OPEN_PARENTHESIS actionTree CLOSE_PARENTHESIS
OPEN_PARENTHESIS actionTree CLOSE_PARENTHESIS;
//newClosedDag: linearActionList '[' NUMBER ']' pragmaList;
openTestList: (openTest)+;
openTest:
/* linearActionList '{' NUMBER '}' pragmaList |*/ linearActionList index
OPEN_PARENTHESIS openDag CLOSE_PARENTHESIS OPEN_PARENTHESIS actionTree CLOSE_PARENTHESIS
| linearActionList index OPEN_PARENTHESIS actionTree CLOSE_PARENTHESIS
OPEN_PARENTHESIS openDag CLOSE_PARENTHESIS
| linearActionList index OPEN_PARENTHESIS openDag CLOSE_PARENTHESIS
OPEN_PARENTHESIS openDag CLOSE_PARENTHESIS;
openDag: openTestList? linearActionList;
linearActionList: (NUMBER)*;
// pragmaList: (pragmaList pragma)?;

expression:
atomExpression
| constantExpression
| variableExpression
| functionCallExpression;
atomExpression: HASHTAG atomValue;
atomValue: NUMBER | DECIMAL | STRING;
constantExpression: AT_SIGN index;
variableExpression: index;
functionCallExpression:
index OPEN_PARENTHESIS expressionList CLOSE_PARENTHESIS;
expressionList: expression (COMMA expression)*;

index:
NUMBER
| DOLLAR_SIGN NUMBER;

// Lexer
VERSION: 'oc5:';
MODULE: 'module:';
ENDMODULE: 'endmodule:';
ENDTABLE: 'end:';
CONSTANTS: 'constants:';
SIGNALS: 'signals:';
INPUT: 'input:';
OUTPUT: 'output:';
PURE: 'pure:';
SINGLE: 'single:';
MULTIPLE: 'multiple:';
BOOL: 'bool:';
VARIABLES: 'variables:';
ACTIONS: 'actions:';
PRESENT: 'present:';
IF: 'if:';
DSZ: 'dsz';
CALL: 'call:';
STATES: 'states:';
STARTPOINT: 'startpoint:';
CALLS: 'calls:';

IDENTIFIER: LETTER (LETTER | UNDERSCORE | NUMBER)*;
LIST_INDEX: NUMBER COLON;
POINT: '.';
UNDERSCORE: '_';
COLON: ':';
HYPHEN: '-';
OPEN_PARENTHESIS: '(';
CLOSE_PARENTHESIS: ')';
LESS_THAN_SIGN: '<';
GREATER_THAN_SIGN: '>';
DOLLAR_SIGN: '$';
HASHTAG: '#';
AT_SIGN: '@';
COMMA: ',';

NUMBER: [0-9]+;
DECIMAL: NUMBER* POINT NUMBER+;
STRING: (LETTER | NUMBER)+; //TODO

WS: [ \t\r\n] -> skip;

// Lexer fragments
fragment LOWERCASE: [a-z];
fragment UPPERCASE: [A-Z];
fragment LETTER: (UPPERCASE | LOWERCASE);
fragment DIGIT: [0-9];
@@ -0,0 +1,30 @@
using Antlr4.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SafetyLustre.ANTLR
{
public class Oc5ErrorListener : BaseErrorListener
{
public String OffendingSymbol { get; private set; }
public StringWriter Writer { get; private set; }

public Oc5ErrorListener(StringWriter writer)
{
Writer = writer;
}

public override void SyntaxError(IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
{
//base.SyntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e);

Writer.WriteLine(msg);

OffendingSymbol = offendingSymbol.Text;
}
}
}
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SafetyLustre.ANTLR")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SafetyLustre.ANTLR")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("cfaafd6c-76b7-400a-a715-2eb0b8ee0899")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\Dependencies\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.props" Condition="Exists('..\..\Dependencies\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>{CFAAFD6C-76B7-400A-A715-2EB0B8EE0899}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SafetyLustre.ANTLR</RootNamespace>
<AssemblyName>SafetyLustre.ANTLR</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<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' ">
<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>..\..\Dependencies\Antlr4.Runtime.4.6.4\lib\net45\Antlr4.Runtime.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="obj\Debug\Oc5BaseVisitor.cs" />
<Compile Include="obj\Debug\Oc5Lexer.cs" />
<Compile Include="obj\Debug\Oc5Parser.cs" />
<Compile Include="obj\Debug\Oc5Visitor.cs" />
<Compile Include="Oc5ErrorListener.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ToStringVisitor.cs" />
</ItemGroup>
<ItemGroup>
<Antlr4 Include="Oc5.g4">
<Generator>MSBuild:Compile</Generator>
<CustomToolNamespace>SafetyLustre.ANTLR</CustomToolNamespace>
<Listener>False</Listener>
<Visitor>True</Visitor>
</Antlr4>
</ItemGroup>
<ItemGroup>
<None Include="obj\Debug\Oc5.tokens" />
<None Include="obj\Debug\Oc5Lexer.tokens" />
<None Include="packages.config" />
</ItemGroup>
<!-- Use this if you don't want to depend on the Java tool -->
<PropertyGroup>
<Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator>
</PropertyGroup>
<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('..\..\Dependencies\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\Dependencies\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.props'))" />
<Error Condition="!Exists('..\..\Dependencies\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\Dependencies\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.targets'))" />
</Target>
<Import Project="..\..\Dependencies\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.targets" Condition="Exists('..\..\Dependencies\Antlr4.CodeGenerator.4.6.4\build\Antlr4.CodeGenerator.targets')" />
</Project>

0 comments on commit 4084a7f

Please sign in to comment.
You can’t perform that action at this time.