public static void Main(string[] args) { try { // open a simple stream to the input CharBuffer input = new CharBuffer(Console.In); // attach java lexer to the input stream, mainLexer = new PLexer(input); // notify selector about starting lexer; name for convenience selector.addInputStream(mainLexer, "main"); selector.select("main"); // start with main P lexer // Create parser attached to selector parser = new PParser(selector); // Parse the input language: P parser.setFilename("<stdin>"); parser.startRule(); } catch (Exception e) { Console.Error.WriteLine("exception: " + e); Console.Error.WriteLine(e.StackTrace); // so we can get stack trace } }
private static PParser.ProgramContext Parse(ICompilationJob job, FileInfo inputFile) { string fileText = File.ReadAllText(inputFile.FullName); AntlrInputStream fileStream = new AntlrInputStream(fileText); PLexer lexer = new PLexer(fileStream); CommonTokenStream tokens = new CommonTokenStream(lexer); PParser parser = new PParser(tokens); parser.RemoveErrorListeners(); // As currently implemented, P can be parsed by SLL. However, if extensions to the // language are later added, this will remain robust. There is a performance penalty // when a file doesn't parse (it is parsed twice), but most of the time we expect // programs to compile and for code generation to take about as long as parsing. try { // Stage 1: use fast SLL parsing strategy parser.Interpreter.PredictionMode = PredictionMode.Sll; parser.ErrorHandler = new BailErrorStrategy(); return(parser.program()); } catch (Exception e) when(e is RecognitionException || e is OperationCanceledException) { // Stage 2: use slower LL(*) parsing strategy job.Output.WriteMessage("Reverting to LL(*) parsing strategy.", SeverityKind.Warning); tokens.Reset(); parser.AddErrorListener(new PParserErrorListener(inputFile, job.Handler)); parser.Interpreter.PredictionMode = PredictionMode.Ll; parser.ErrorHandler = new DefaultErrorStrategy(); return(parser.program()); } }
private static PParser.ProgramContext Parse(ITranslationErrorHandler handler, FileInfo inputFile) { var fileStream = new AntlrFileStream(inputFile.FullName); var lexer = new PLexer(fileStream); var tokens = new CommonTokenStream(lexer); var parser = new PParser(tokens); parser.RemoveErrorListeners(); try { // Stage 1: use fast SLL parsing strategy parser.Interpreter.PredictionMode = PredictionMode.Sll; parser.ErrorHandler = new BailErrorStrategy(); return(parser.program()); } catch (Exception e) when(e is RecognitionException || e is OperationCanceledException) { // Stage 2: use slower LL(*) parsing strategy tokens.Reset(); parser.AddErrorListener(new PParserErrorListener(inputFile, handler)); parser.Interpreter.PredictionMode = PredictionMode.Ll; parser.ErrorHandler = new DefaultErrorStrategy(); return(parser.program()); } }
/// <summary> /// Parses a P syntax tree to C#. /// </summary> /// <param name="tree">SyntaxTree</param> private void ParsePSyntaxTree(SyntaxTree tree) { var root = (CompilationUnitSyntax)tree.GetRoot(); var tokens = new PLexer().Tokenize(root.ToFullString()); var program = new PParser(this, tree).ParseTokens(tokens); this.PPrograms.Add(program as PProgram); this.ProgramMap.Add(program, tree); }
public static int[,] GetMatrixFromCode(string code) { var graph = new Graph <SStatement>(); var tokens = PLexer.ParseTokens(code); PParser.GetFunctionContents(tokens).First().BuildGraphNodes(graph); RemoveUnvisible(graph); return(graph.GetMatrix()); }
private void DisplayProgram(string program) { try { var tokens = PLexer.ParseTokens(program); foreach (var matchedFunction in PParser.GetFunctionContents(tokens)) { var graph = new Graph <SStatement>(); matchedFunction.BuildGraphNodes(graph); RemoveUnvisible(graph); var matrix = graph.GetMatrix(); dataGridView1.ColumnCount = matrix.GetLength(0); dataGridView1.RowCount = matrix.GetLength(1); dataGridView1.ColumnHeadersHeight = 4; dataGridView1.RowHeadersWidth = 4; for (var i0 = 0; i0 < matrix.GetLength(0); i0++) { dataGridView1.Columns[i0].HeaderText = (i0 + 1).ToString(); dataGridView1.Columns[i0].Width = 17; for (var i1 = 0; i1 < matrix.GetLength(1); i1++) { dataGridView1[i1, i0].Value = matrix[i0, i1]; } } } } catch (ArgumentException exc) { MessageBox.Show(@"There is an error in source code: " + exc.Message); } catch (InvalidOperationException exc) { MessageBox.Show(@"There is an error in source code: " + exc.Message); } catch { MessageBox.Show(@"An unexpected error occured!"); } }
public static void Main(string[] args) { try { // open a simple stream to the input CharBuffer input = new CharBuffer(Console.In); // attach java lexer to the input stream, mainLexer = new PLexer(input); // notify selector about starting lexer; name for convenience selector.addInputStream(mainLexer, "main"); selector.select("main"); // start with main P lexer // Create parser attached to selector parser = new PParser(selector); // Parse the input language: P parser.setFilename("<stdin>"); parser.startRule(); } catch(Exception e) { Console.Error.WriteLine("exception: "+e); Console.Error.WriteLine(e.StackTrace); // so we can get stack trace } }