public void TestLeftRecursionAssociation()
 {
     var parser = new LRParser();
     var match = parser.GetMatch("1+1+1+1", parser.Exp);
     Assert.IsTrue(match.Success);
     Assert.AreEqual("(((1 + 1) + 1) + 1)", match.Result);
 }
 public void TestLeftRecursionParseTree()
 {
     var parser = new LRParser();
     var match = parser.GetMatch("aaaa", parser.A);
     Assert.IsTrue(match.Success);
     var res = match.Result;
 }
Exemple #3
0
        static void Main(string[] args)
        {
            var grammar   = new LRGrammar();
            var tokenizer = new Tokenizer();
            var lexer     = new LexerMC(grammar, tokenizer);
            var parser    = new LRParser(lexer, grammar);
            var Compiler  = new Compiler(lexer, parser);

            Compiler.Compile();
        }
Exemple #4
0
        public static string ParseBetweenStrings(BotData data, string input,
                                                 string leftDelim, string rightDelim, bool caseSensitive = true, string prefix = "", string suffix = "")
        {
            var parsed = LRParser.ParseBetween(input, leftDelim, rightDelim, caseSensitive).FirstOrDefault() ?? string.Empty;

            parsed = prefix + parsed + suffix;

            data.Logger.LogHeader();
            data.Logger.Log($"Parsed value: {parsed}", LogColors.Yellow);
            return(parsed);
        }
Exemple #5
0
        public static List <string> ParseBetweenStringsRecursive(BotData data, string input,
                                                                 string leftDelim, string rightDelim, bool caseSensitive = true, string prefix = "", string suffix = "")
        {
            var parsed = LRParser.ParseBetween(input, leftDelim, rightDelim, caseSensitive)
                         .Select(p => prefix + p + suffix).ToList();

            data.Logger.LogHeader();
            data.Logger.Log($"Parsed {parsed.Count} values:", LogColors.Yellow);
            data.Logger.Log(parsed, LogColors.Yellow);
            return(parsed);
        }
Exemple #6
0
        public TablesForm(Syntactic syntactic, Semantic semantic)
        {
            _semantic   = semantic;
            _syntactic  = syntactic;
            _llanalyser = new LLParser(_semantic, _syntactic);
            _lrParser   = new LRParser(_semantic, _syntactic);
            _slrParser  = new SLRParser(_semantic, _syntactic);
            _lalrParser = new LALRParser(_semantic, _syntactic);

            InitializeComponent();
        }
Exemple #7
0
    private static void Main(string[] args)
    {
        var sw     = Stopwatch.StartNew();
        var parser = new LRParser(
            MakePair("start", new string[] { "exps" }),
            MakePair("exps", new string[] { "exps", "exp" }),
            MakePair("exps", new string[] { }),
            MakePair("exp", new string[] { "INTEGER" })
            );

        Console.WriteLine(sw.ElapsedMilliseconds);
    }
        public void TestLeftRecursionNonLR()
        {
            var parser = new LRParser(false);
            var match = parser.GetMatch("ab", parser.NonLR);
            Assert.IsTrue(match.Success);

            match = parser.GetMatch("ac", parser.NonLR);
            Assert.IsTrue(match.Success);

            match = parser.GetMatch("ad", parser.NonLR);
            Assert.IsFalse(match.Success);
        }
        private static List <string> ParseLR(string original, string left, string right, bool useRegex)
        {
            List <string> list;

            if (!useRegex)
            {
                list = LRParser.ParseBetween(original, left, right).ToList();
            }
            else
            {
                list = new();
                var pattern = BuildLRPattern(left, right);

                foreach (Match match in Regex.Matches(original, pattern))
                {
                    list.Add(match.Value);
                }
            }

            return(list);
        }
        public void ParseBetweenStrings_OneLineStringCaseSensitive_MatchNothing()
        {
            string match = LRParser.ParseBetween(oneLineString, "THE ", " IS").FirstOrDefault();

            Assert.Null(match);
        }
 public void TestLeftRecursionHexEscape()
 {
     var parser = new LRParser();
     var match = parser.GetMatch("#\\x000", parser.HexEscapeCharacter);
     Assert.IsTrue(match.Success);
 }
        public void ParseBetweenStrings_OneLineStringManyMatches_GetText()
        {
            var matches = LRParser.ParseBetween(oneLineTwoMatchesString, "the ", " is", false).ToArray();

            Assert.Equal(new string[] { "cat", "dog" }, matches);
        }
        public void ParseBetweenStrings_OneLineStringCaseInsensitive_GetText()
        {
            string match = LRParser.ParseBetween(oneLineString, "THE ", " IS", false).FirstOrDefault();

            Assert.Equal("cat", match);
        }
        public void ParseBetweenStrings_RightDelimEmpty_ParseUntilEnd()
        {
            string match = LRParser.ParseBetween(oneLineString, "is ", string.Empty).FirstOrDefault();

            Assert.Equal("on the table", match);
        }
        public void ParseBetweenStrings_LeftDelimEmpty_ParseFromBeginning()
        {
            string match = LRParser.ParseBetween(oneLineString, string.Empty, " is").FirstOrDefault();

            Assert.Equal("The cat", match);
        }
        public ExampleLangTest(string program, [CallerMemberNameAttribute] string testName = "unknown")
        {
            string cwd = Directory.GetCurrentDirectory();
            string localDirectory;

            using (var md5 = System.Security.Cryptography.MD5.Create()) {
                var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(program));
                localDirectory = Path.Combine(cwd, $"{testName}_{BitConverter.ToString(hash).Replace("-", "")}");
            }
            if (Directory.Exists(localDirectory))
            {
                Directory.Delete(localDirectory, true);
            }
            Directory.CreateDirectory(localDirectory);
            try {
                SymbolTable.Reset();
                // Lexical analysis
                var tokenConfigurationFile    = new LexicalConfigurationFile(TokenPath);
                var tokenParserTableGenerator = new TokenParserTableGenerator(tokenConfigurationFile);
                var tokenParser = new TokenParser(tokenParserTableGenerator.NFATable);
                var tokenStream = tokenParser.ParseString(program);

                // Syntactic analysis
                var syntaxConfigurationFile = new SyntacticConfigurationFile(SyntaxPath);
                var productionTable         = new ProductionTable(syntaxConfigurationFile);
                var clrStates    = new CLRStateGenerator(productionTable, syntaxConfigurationFile);
                var lrTable      = LRParsingTable.From(clrStates, productionTable);
                var syntaxParser = new LRParser(syntaxConfigurationFile, tokenStream);
                var ast          = syntaxParser.Parse(lrTable, tokenStream);

                foreach (var file in Directory.GetFiles(SemanticVisitorsPath, "*.py"))
                {
                    new SemanticVisitor(file).Traverse(ast);
                }

                var instructionStream = new InstructionStream();
                foreach (var file in Directory.GetFiles(CodeGeneratorVisitorsPath, "*.py"))
                {
                    new CodeGeneratorVisiter(file, instructionStream).Traverse(ast);
                }
                File.WriteAllText(Path.Combine(localDirectory, InstructionStreamFileName), instructionStream.ToString());

                File.Copy(PostBuildScript, Path.Combine(localDirectory, new FileInfo(PostBuildScript).Name));
                File.Copy(STDIO_D, Path.Combine(localDirectory, new FileInfo(STDIO_D).Name));
                var process = new System.Diagnostics.Process();
                process.StartInfo = new System.Diagnostics.ProcessStartInfo()
                {
                    FileName               = "powershell",
                    Arguments              = PostBuildScript,
                    WorkingDirectory       = localDirectory,
                    RedirectStandardOutput = true
                };
                process.Start();
                process.WaitForExit();
                Log.WriteLineVerbose(process.StandardOutput.ReadToEnd());

                string exePath = Path.Combine(localDirectory, "program.exe");

                if (!File.Exists(exePath))
                {
                    throw new BuildFailureException("Program.exe did not build. Likely a problem in the IR when building using GCC");
                }

                this.OutputPath   = Path.Combine(localDirectory, "debug_output.out");
                process           = new System.Diagnostics.Process();
                process.StartInfo = new System.Diagnostics.ProcessStartInfo()
                {
                    FileName         = exePath,
                    WorkingDirectory = localDirectory
                };
                process.Start();
                process.WaitForExit();
            } catch (Exception e) {
                throw e;
            } finally {
                Log.Dump(Path.Combine(localDirectory, "dmp.log"));
            }
        }
        public void ParseBetweenStrings_OneLineStringCaseSensitive_GetText()
        {
            string match = LRParser.ParseBetween(oneLineString, "The ", " is").FirstOrDefault();

            Assert.Equal("cat", match);
        }
        public void ParseBetweenStrings_BothDelimsNotFound_MatchNothing()
        {
            string match = LRParser.ParseBetween(oneLineString, "John", "Mary").FirstOrDefault();

            Assert.Null(match);
        }
        public void ParseBetweenStrings_RightDelimNotFound_MatchNothing()
        {
            string match = LRParser.ParseBetween(oneLineString, "cat", "John").FirstOrDefault();

            Assert.Null(match);
        }
        public void ParseBetweenStrings_BothDelimsEmpty_ParseEntireInput()
        {
            string match = LRParser.ParseBetween(oneLineString, string.Empty, string.Empty).FirstOrDefault();

            Assert.Equal(oneLineString, match);
        }