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()); } }
static void HandleFileCs(TestVisitor visitor, string filePath) { FileInfo info = new FileInfo(filePath); if (info.Extension == ".cs") { AntlrFileStream stream = new AntlrFileStream(filePath); CSharpLexer lexer = new CSharpLexer(stream); CommonTokenStream tokens = new CommonTokenStream(lexer); CSharpParser parser = new CSharpParser(tokens); CSharpParser.Compilation_unitContext startContext = parser.compilation_unit(); TestListener listener = new TestListener(parser); IParseTree tree = parser.compilation_unit(); ParseTreeWalker walker = new ParseTreeWalker(); walker.Walk(listener, startContext); StringBuilder streamwritter = new StringBuilder(stream.ToString()); foreach (Tuple <int, string> tup in listener.GetTuples()) { streamwritter.Remove(tup.Item1, tup.Item2.Length).Insert(tup.Item1, tup.Item2); } //visitor.Visit(startContext); StreamWriter writer = new StreamWriter(filePath); writer.Write(streamwritter); writer.Dispose(); } }
private static TypescriptParser.SourceFileContext LoadFile(string filename, ref List <string> errors) { if (!File.Exists(filename)) { errors.Add($"File '{filename}' was not found"); return(null); } var antlrStream = new AntlrFileStream(filename); var lexer = new TypescriptLexer(antlrStream); var tokens = new CommonTokenStream(lexer); var parser = new TypescriptParser(tokens); var errorListener = new ErrorListener(filename); parser.RemoveErrorListeners(); parser.RemoveParseListeners(); parser.AddErrorListener(errorListener); parser.AddParseListener(new ParserValidation(errorListener)); var sourceFile = parser.sourceFile(); errors.AddRange(errorListener.Errors); if (sourceFile == null) { return(null); } sourceFile.FilePath = filename; return(sourceFile); }
private static ASTNode ParseInput(string inputPath) { var input = new AntlrFileStream(inputPath); var lexer = new CoolLexer(input); var errors = new List <string>(); lexer.RemoveErrorListeners(); //lexer.AddErrorListener(new LexerErrorListener(errors)); var tokens = new CommonTokenStream(lexer); var parser = new CoolParser(tokens); //parser.RemoveErrorListeners(); //parser.AddErrorListener(new ParserErrorListener(errors)); IParseTree tree = parser.program(); if (errors.Any()) { Console.WriteLine(); foreach (var item in errors) { Console.WriteLine(item); } return(null); } var astBuilder = new ASTGenerator(); ASTNode ast = astBuilder.Visit(tree); return(ast); }
public void load_from_file(string file_name) { this.can_run = true; AntlrFileStream fileStream = new AntlrFileStream(file_name); this.run(fileStream); }
static void Try(string file) { var str = new AntlrFileStream(file); var lexer = new aLexer(str); var tokens = new CommonTokenStream(lexer); var parser = new aParser(tokens); var listener = new ErrorListener <IToken>(parser, lexer, tokens); parser.AddErrorListener(listener); DateTime start = DateTime.Now; var tree = parser.file(); if (listener.had_error) { System.Console.WriteLine("error in parse."); } // System.Console.WriteLine(tokens.OutputTokens()); // System.Console.WriteLine(tree.OutputTree(tokens)); System.Console.WriteLine("Times = " + lexer.times); DateTime end = DateTime.Now; var diff = end - start; System.Console.WriteLine("Runtime " + diff); System.Console.WriteLine("Tokens " + tokens.GetTokens().Count); }
static void Main(string[] args) { var input = new AntlrFileStream("C:/Users/Leandro/Desktop/Compiler/CoolCompiler/input.cl"); var lexer = new CoolLexer(input); /*var errors = new List<string>(); * lexer.RemoveErrorListeners();*/ var tokens = new CommonTokenStream(lexer); var parser = new CoolParser(tokens); var tree = parser.program(); ASTBuilder astBuilder = new ASTBuilder(); ASTNode ast = astBuilder.Visit(tree); Console.WriteLine( ); /* var a = new Dictionary<int,int>(); * a.Add(1,2); * a.Add(3, 4); * a.Add(5, 6); * * foreach (var x in ast.Attributes) * { * Console.WriteLine( "{"+ * x.Key.ToString() + ":" + x.Value.ToString() + "}" ); * } */ Console.WriteLine(tree.ToStringTree(parser)); }
/// <summary> /// The method is used to load the vbf file from the given path. /// </summary> /// <param name="path"></param> /// <returns></returns> public static Database Load(string path) { // Create antlr input stream. AntlrInputStream stream = new AntlrFileStream(path); // Create the lexer. VbfLexer lexer = new VbfLexer(stream); // Start syntax analysing. CommonTokenStream tokens = new CommonTokenStream(lexer); // Create the parser to start sematic analysing. VbfParser parser = new VbfParser(tokens); //parser.RemoveErrorListeners(); //parser.AddErrorListener(new VbfErrorListener()); // Get the parse tree. IParseTree tree = parser.database(); // For test purpose, print out the parse tree. //Console.Write(tree.ToStringTree()); VbfVisitor loader = new VbfVisitor(); loader.Visit(tree); Database d = loader.Database; d.Valid = d.checkDatabaseIsValid(); return(d); }
public double InterpretProgramCodeFromFilePath(string filePath) { var inputStream = new AntlrFileStream(filePath); var result = InterpretProgram(inputStream); return(result); }
static void Main() { try { // В качестве входного потока символов устанавливаем консольный ввод или содержимое файла // AntlrInputStream input = new AntlrInputStream(Console.In); AntlrFileStream input = new AntlrFileStream("Source.txt"); // Настраиваем лексер на этот поток CompilerLexer lexer = new CompilerLexer(input); // Создаем поток токенов на основе лексера tokens = new CommonTokenStream(lexer); // Создаем парсер CompilerParser parser = new CompilerParser(tokens); IParseTree tree = parser.compiler(); // Запускаем первое правило грамматики MyVisitor visitor = new MyVisitor(); // Запускаем обход дерева visitor.Visit(tree); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadKey(); }
public static CommonTokenStream Tokens(string inputPath) { var code = new AntlrFileStream(inputPath); var lexer = new CoolLexer(code); var errors = new List <string>(); lexer.RemoveErrorListeners(); lexer.AddErrorListener(new LexerErrorListener(errors)); if (errors.Any()) { Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; foreach (var item in errors) { Console.WriteLine(item); } Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(); return(null); } var tokens = new CommonTokenStream(lexer); return(tokens); }
public static void OpenFile(string[] args) { if (args.Length == 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR: No arguments given "); Console.ForegroundColor = ConsoleColor.Gray; Environment.Exit(1); return; } Console.WriteLine("Program: {0,40}\t", args[0]); AntlrFileStream input_file; try { input_file = new AntlrFileStream(args[0]); } catch (Exception) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("ERROR: Opening file "); Console.ForegroundColor = ConsoleColor.Gray; Environment.Exit(1); return; } }
public Experiment Read(string filename) { try { var charStream = new AntlrFileStream(filename); var lexer = new KpExperimentLexer(charStream); var tokenStream = new CommonTokenStream(lexer); var parser = new KpExperimentParser(tokenStream); var modelBuilder = new KpExperimentModelBuilder(); parser.BuildParseTree = true; var tree = parser.kPExpriment(); var experiment = modelBuilder.GetExperiment(tree); return(experiment); } catch (KpExperimentSemanticException semanticException) { Console.WriteLine(string.Format("Error: {0}; Line: {1}; Column: {2}", semanticException.Message, semanticException.Line, semanticException.Column)); return(null); } }
public KPsystem Read(string filename) { try { var charStream = new AntlrFileStream(filename); var lexer = new KpLinguaLexer(charStream); var tokenStream = new CommonTokenStream(lexer); var parser = new KpLinguaParser(tokenStream); var modelBuilder = new KpLinguaModelBuilder(); parser.BuildParseTree = true; var tree = parser.kPsystem(); var kPsystem = modelBuilder.GetKpSystem(tree); return(kPsystem); } catch (KpLinguaSemanticException semanticException) { Console.WriteLine(string.Format("Error: {0}; Line: {1}; Column: {2}", semanticException.Message, semanticException.Line, semanticException.Column)); return(null); } }
private void Form1_Shown(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); DialogResult result; do { result = ofd.ShowDialog(); } while (result != DialogResult.OK || ofd.FileName == null); string fileName = ofd.FileName; FileInfo snakewareLogo = new FileInfo(fileName); MessageBox.Show("Bytes before: " + snakewareLogo.Length); ImageOptimizer optimizer = new ImageOptimizer(); optimizer.LosslessCompress(snakewareLogo); snakewareLogo.Refresh(); MessageBox.Show("Bytes after: " + snakewareLogo.Length); //Compressor return; AntlrFileStream inputStream = new AntlrFileStream(fileName); IParseTree tree = null; IVocabulary names = null; string typeS = fileName.Substring(fileName.LastIndexOf('.') + 1); switch (typeS.ToUpper()) { case "CSS": tree = new CssParser(new CommonTokenStream(new CssLexer(inputStream))).stylesheet(); names = CssParser.DefaultVocabulary; break; case "HTML": tree = new HtmlParser(new CommonTokenStream(new HtmlLexer(inputStream))).htmlDocument(); names = HtmlParser.DefaultVocabulary; break; case "JS": tree = new JsParser(new CommonTokenStream(new JsLexer(inputStream))).program(); names = JsParser.DefaultVocabulary; break; } var node = treeView.Nodes.Add(ToText(tree, names)); RecursFn(tree, node, (i) => ToText(i, names)); /*var d = new CommonTokenStream(new HtmlLexer(inputStream)); * names = HtmlParser.DefaultVocabulary; * d.Fill(); * foreach(var item in d.GetTokens()) { * treeView.Nodes.Add(names.GetDisplayName(item.Type) + " --- " + item.ToString()); * }*/ }
public static Motif readFromFile(string filepath) { AntlrFileStream stream = new AntlrFileStream(filepath); AbcNotationLexer lexer = new AbcNotationLexer(stream); CommonTokenStream tokens = new CommonTokenStream(lexer); AbcNotationParser parser = new AbcNotationParser(tokens); return(parser.file().m); }
static void Main(string[] args) { string in_filename = @"..\..\NOD.java"; string out_filename = @"..\..\NOD.class"; AntlrInputStream inputStream = new AntlrFileStream(in_filename); var javaLexer = new JavaLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(javaLexer); var javaParser = new JavaParser(commonTokenStream); var visitor = new JavaVisitor(new StackFrame()); visitor.Visit(javaParser.compileUnit()); var stackFrame = visitor.StackFrame; var initInd = stackFrame.GetOrAddMethodref("<init>", "()V", "java/lang/Object"); var methods = new List <MethodInfo>(); var initMethod = new MethodInfo(MethodAccessFlags.ACC_PUBLIC, stackFrame.GetOrAddString("<init>"), stackFrame.GetOrAddString("()V")); var initCodeInstructions = new List <byte>(); initCodeInstructions.Add(0x2A); initCodeInstructions.Add(0xB7); initCodeInstructions.AddRange(Utils.WriteUShort(initInd)); initCodeInstructions.Add(0xB1); var initCodeAttribute = new CodeAttributeParser(1, 1, initCodeInstructions); var initData = initCodeAttribute.BuildData(); initMethod.AttributesCount = 1; initMethod.Attributes = new AttributeDescription[1] { new AttributeDescription(stackFrame.GetOrAddString("Code"), (uint)initData.Length, initData) }; methods.Add(initMethod); var mainMethod = new MethodInfo(MethodAccessFlags.ACC_PUBLIC | MethodAccessFlags.ACC_STATIC, stackFrame.GetOrAddString("main"), stackFrame.GetOrAddString("([Ljava/lang/String;)V")); var mainCodeAttribute = new CodeAttributeParser(stackFrame.maxStackSize, stackFrame.maxLocalsSize, stackFrame.BuildInstructions()); var mainData = mainCodeAttribute.BuildData(); mainMethod.AttributesCount = 1; mainMethod.Attributes = new AttributeDescription[1] { new AttributeDescription(stackFrame.GetOrAddString("Code"), (uint)mainData.Length, mainData) }; methods.Add(mainMethod); var classFile = ClassFile.CreateClassFile(0, 52, ClassAccessFlags.ACC_PUBLIC | ClassAccessFlags.ACC_SUPER, stackFrame.thisClass, stackFrame.constantPool, methods); classFile.SuperClass = stackFrame.GetOrAddClass("java/lang/Object"); File.WriteAllBytes(out_filename, classFile.BuildClassFile()); }
public static string GetOwnText(string filename) { var afs = new AntlrFileStream(filename); var lexer = new gramLexer(afs); var ts = new UnbufferedTokenStream(lexer); var parser = new gramParser(ts); var tree = parser.compileUnit(); var source = tree.GetText(); return(source); }
public static void Main() { var stream = new AntlrFileStream(Path); var lexer = new LeafLexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new LeafParser(tokens); var fragment = new FragmentNode(parser.entry_point()); Console.WriteLine(fragment.ToString()); }
public Copybook Parse(string file) { var inputStream = new AntlrFileStream(file); var lexer = new Cobol85Lexer(inputStream); var commonTokenStream = new CommonTokenStream(lexer); var parser = new Cobol85Parser(commonTokenStream); var visitor = new CopybookVisitor(); return(visitor.Visit(parser.dataDescriptionEntry())); }
private static void Lint(ref List <string> errorList, string fileName) { var inputStream = new AntlrFileStream(fileName); var lexer = new CPPLINTLexer(inputStream); var tokenStream = new CommonTokenStream(lexer); var parser = new CPPLINTParser(tokenStream); var tree = parser.translationunit(); var walker = new ParseTreeWalker(); walker.Walk(new Listener(ref errorList), tree); }
private static sim6502Parser.SuitesContext GetContext(string test) { var afs = new AntlrFileStream(test); var lexer = new sim6502Lexer(afs); var tokens = new CommonTokenStream(lexer); var parser = new sim6502Parser(tokens); parser.RemoveErrorListeners(); parser.AddErrorListener(new SimErrorListener()); parser.BuildParseTree = true; return(parser.suites()); }
static void Main(string[] args) { AntlrFileStream fileStream = new AntlrFileStream("a.txt"); CaculatorLexer lexer = new CaculatorLexer(fileStream); CommonTokenStream tokens = new CommonTokenStream(lexer); CaculatorParser parser = new CaculatorParser(tokens); CaculatorParser.ProgContext progContext = parser.prog(); EvalVisitor visitor = new EvalVisitor(); visitor.Visit(progContext); }
static void Run(string file) { var stream = new AntlrFileStream(file); var lexer = new SuperReadableLexer(stream); var tokens = new CommonTokenStream(lexer); var parser = new SuperReadableParser(tokens); var visitor = new StatementVisitor(); var program = visitor.Visit(parser.file()); program.Run(); }
static void Main(string[] args) { var input = new AntlrFileStream("Modl.Vm\\test.modl"); var lexer = new AsmLexer(input); var tokens = new CommonTokenStream(lexer); var parser = new AsmParser(tokens); var tree = parser.program(); var visit = new AsmAstBuilder(); visit.VisitProgram(tree); var vm = new VirtualMachine(visit.Program, visit.Functions); vm.Execute(true); }
public static List <Shader> Read(string filepath, BaseErrorListener errorListener = null) { Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US"); ICharStream charStream = new AntlrFileStream(filepath); ShaderLexer lexer = new ShaderLexer(charStream); ShaderParser parser = new ShaderParser(new CommonTokenStream(lexer)); parser.RemoveErrorListeners(); if (errorListener != null) { parser.AddErrorListener(errorListener); } return(parser.shaderList().value); }
public static ParseTreeData ReadFileToPt(string path, SideeffectHelper helper) { ICharStream charStream = new AntlrFileStream(path, Encoding.UTF8); CrawlLexer tokenSource = new CrawlLexer(charStream); ITokenStream tokenStream = new CommonTokenStream(tokenSource); CompilationMessageErrorListner cm = new CompilationMessageErrorListner(helper.CompilationMessages, path); CrawlParser parser = new CrawlParser(tokenStream); parser.RemoveErrorListeners(); parser.AddErrorListener(cm); CrawlParser.Translation_unitContext translationUnit = parser.translation_unit(); return(new ParseTreeData(tokenStream, translationUnit, path)); }
static void HandleFileCs(TestListener listener, string filePath) { FileInfo info = new FileInfo(filePath); if (info.Extension == ".cs") { AntlrFileStream stream = new AntlrFileStream(filePath); CSharpLexer lexer = new CSharpLexer(stream); CommonTokenStream tokens = new CommonTokenStream(lexer); CSharpParser parser = new CSharpParser(tokens); parser.RemoveErrorListeners(); parser.AddErrorListener(new CustomError()); CSharpParser.Compilation_unitContext startContext = parser.compilation_unit(); ParseTreeWalker walker = new ParseTreeWalker(); walker.Walk(listener, startContext); } }
private static int RunTests(Options opts) { var afs = new AntlrFileStream(opts.SuiteFile); var lexer = new sim6502Lexer(afs); var tokens = new CommonTokenStream(lexer); var parser = new sim6502Parser(tokens) { BuildParseTree = true }; var tree = parser.suites(); var walker = new ParseTreeWalker(); var sbl = new SimBaseListener(); walker.Walk(sbl, tree); return(sbl.TotalSuitesFailed == 0 ? 0 : 1); }
/// <summary> /// Reloads the definition of the program /// </summary> private void ReloadDefinition() { lock (runCounterLock) { executionWait.Reset(); while (currentRuns > 0) { Monitor.Wait(runCounterLock); } } try { AntlrInputStream astr = null; if (!isStatic) { astr = new AntlrFileStream(fileName, Encoding.Default); } else { using (var r = new StreamReader(file, Encoding.Default)) { astr = new AntlrInputStream(r); } } //SingletonPredictionContext. Lexer lex = new ITVScriptingLexer(astr); ITVScriptingParser parser = new ITVScriptingParser(new CommonTokenStream(lex)); ErrorListener listener = new ErrorListener(); parser.RemoveErrorListeners(); parser.AddErrorListener(listener); //parser.AddParseListener(new ScriptErrorHandler()); program = parser.program(); runnable = parser.NumberOfSyntaxErrors == 0; suspectLine = listener.SuspectLine; errors = listener.GetAllErrors(); lastCompilation = DateTime.Now; } finally { executionWait.Set(); } }