Skip to content
Snippets Groups Projects
Commit d890e865 authored by Dennis Wobbe's avatar Dennis Wobbe :speech_balloon:
Browse files

Adding necessary ANTLR classes

parent 2344ea34
No related branches found
No related tags found
1 merge request!21Near final implementation of Convert package + documentation
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
namespace Transform.Convert
{
internal class EListener : IAntlrErrorListener<IToken>
{
public void SyntaxError([NotNull] IRecognizer recognizer, [Nullable] IToken offendingSymbol, int line, int charPositionInLine, [NotNull] string msg, [Nullable] RecognitionException e)
{
throw new Exception(msg);
}
}
}
grammar MMFHeader;
header : PERCENT PERCENT MATRIXMARKET MATRIX format;
format : choice1 | choice2 | choice3;
choice1 : ( COORDINATE | ARRAY) (REAL | INTEGER | COMPLEX) (GENERAL | SYMMETRIC | SKEW);
choice2 : ( COORDINATE | ARRAY) COMPLEX HERMITIAN;
choice3 : COORDINATE PATTERN (GENERAL | SYMMETRIC);
PERCENT : '%';
MATRIXMARKET : 'MatrixMarket';
MATRIX : 'matrix';
COORDINATE : 'coordinate';
ARRAY : 'array';
REAL : 'real';
INTEGER : 'integer';
COMPLEX : 'complex';
GENERAL : 'general';
SYMMETRIC : 'symmetric';
SKEW : 'skew-symmetric';
HERMITIAN : 'Hermitian';
PATTERN : 'pattern';
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;
using Antlr4.Runtime;
using DataStructures;
using Transform;
using Transform.Convert;
namespace SparseTransform.Convert
{
internal class MatrixMarketReader : IReader
{
{
public IGraph Read(string matrix)
{
throw new NotImplementedException();
string[] lines = matrix.Split(
new string[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);
checkComments(lines);
if (checkHeader(lines[0]))
{
BipartiteGraph graph = new BipartiteGraph();
return ReadGraph(lines, graph);
}
else
{
AdjacencyGraph graph = new AdjacencyGraph();
return ReadGraph(lines, graph);
}
}
public AdjacencyGraph ReadAdjacency(string matrix)
private bool checkHeader(string header)
{
throw new NotImplementedException();
AntlrInputStream stream = new AntlrInputStream(header);
MMFHeaderLexer lexer = new MMFHeaderLexer(stream);
CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
MMFHeaderParser speakParser = new MMFHeaderParser(commonTokenStream);
EListener errorHandler = new EListener();
speakParser.AddErrorListener(errorHandler);
VisitorHeader visitor = new VisitorHeader();
return visitor.Visit(speakParser.header());
}
private void checkComments(string[] lines)
{
bool leftComments = false;
foreach (string line in lines)
{
if (line != "")
{
if (leftComments && line.StartsWith('%'))
{
throw new Exception("comment section mismatch on input");
}
else
{
if (!line.StartsWith('%'))
{
leftComments = true;
}
}
}
else
{
leftComments = true;
}
}
}
public IGraph ReadGraph(string[] matrix, IGraph graph)
{
string[] values;
int lineCount = getFirstDataLine(matrix);
matrix[lineCount] = "% " + matrix[lineCount];
try
{
foreach (String line in matrix)
{
if (line != "")
{
if (!line.StartsWith('%'))
{
values = line.Split(" ");
if (values.Length != 3)
{
throw new Exception();
}
int xCoordinate = Int32.Parse(values[0]);
int yCoordinate = Int32.Parse(values[1]);
String value = values[2];
addEdge(xCoordinate, yCoordinate, graph);
}
}
}
return graph;
}
catch (Exception e)
{
throw new Exception("data section mismatch on input");
}
}
private void addEdge(int xCoordinate, int yCoordinate, IGraph g)
{
if (xCoordinate != yCoordinate)
{
g.AddEdge(xCoordinate, yCoordinate);
}
}
private int getFirstDataLine(String[] lines)
{
int firstDataLine = 0;
foreach (string line in lines)
{
if (line != "")
{
if (line[0] != '%')
{
break;
}
else
{
firstDataLine++;
}
}
else
{
firstDataLine++;
}
}
return firstDataLine;
}
public BipartiteGraph ReadBipartite(string matrix)
{
throw new NotImplementedException();
}
public AdjacencyGraph ReadAdjacency(string matrix)
{
throw new NotImplementedException();
}
}
}
}
\ No newline at end of file
......@@ -41,7 +41,7 @@ namespace SparseTransform.Convert
private void CalculateCompressedMatrix()
{
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using DataStructures;
using SparseTransform.Convert;
namespace Transform.Convert
{
internal class VisitorHeader : MMFHeaderBaseVisitor<bool>
{
public override bool VisitHeader([NotNull] MMFHeaderParser.HeaderContext context)
{
if ((context.format().choice1() is null || context.format().choice1().GENERAL() is null)
&& (context.format().choice3() is null || context.format().choice3().GENERAL() is null))
{
return false;
}
else
{
return true;
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment