コード例 #1
0
        public void Parser_MiniPL_LL1ParseTableGenerationTest()
        {
            CFG        grammar    = MiniPL.GetInstance().Grammar;
            ParseTable parseTable = grammar.CreateLL1ParseTable();

            Assert.AreNotEqual(null, parseTable);
        }
コード例 #2
0
ファイル: LRParser.cs プロジェクト: KallynGowdy/Parser.Net
 /// <summary>
 ///     Sets the parse table that the parser is to use. The given parse table is not checked for conflicts so a
 ///     MultipleParseActions error might be returned during parse time.
 /// </summary>
 /// <param name="table"></param>
 public virtual void SetParseTable(ParseTable <T> table)
 {
     if (table == null)
     {
         throw new ArgumentNullException("table");
     }
     parseTable = table;
     SetEndOfInputFromTable();
 }
コード例 #3
0
 public ShiftAction(ParseTable <T> table, int nextState)
     : base(table)
 {
     if (nextState < 0)
     {
         throw new ArgumentOutOfRangeException("nextState", "Must be greater than or equal to 0");
     }
     NextState = nextState;
 }
コード例 #4
0
 public ReduceAction(ParseTable <T> table, LRItem <T> reduceItem)
     : base(table)
 {
     if (reduceItem != null)
     {
         ReduceItem = reduceItem;
     }
     else
     {
         throw new ArgumentNullException("reduceItem", "Must be non-null");
     }
 }
コード例 #5
0
 public AcceptAction(ParseTable <T> table, LRItem <T> acceptItem)
     : base(table)
 {
     if (acceptItem != null)
     {
         AcceptItem = acceptItem;
     }
     else
     {
         throw new ArgumentNullException("acceptItem", "Must be non-null");
     }
 }
コード例 #6
0
        static void Main(string[] args)
        {
            // todo read from file
            var grammar = new Grammar("S -> a S b S\nS -> a");
            var states  = new StateGenerator(grammar);

            states.PrintStates();

            var table = new ParseTable(grammar, states.States);

            Console.WriteLine();
            table.Print();
        }
コード例 #7
0
		/// <summary>
		/// Prints the parse table to an specified AST.
		/// </summary>
		/// <param name="parseTable">The <see cref="ParseTable"/>.</param>
		/// <returns>The resulting AST.</returns>
		private ITerm Print(ParseTable parseTable)
		{
			#region Contract
			Contract.Requires<ArgumentNullException>(parseTable != null);
			Contract.Ensures(Contract.Result<ITerm>() != null);
			#endregion

			// TODO: Injections?

			return factory.Cons("parse-table",
				factory.Int(6),
				factory.Int(parseTable.InitialState.Index),
				PrintLabels(parseTable.Labels),
				factory.Cons("states", PrintStates(parseTable.States, parseTable.Labels)),
				factory.Cons("priorities", PrintPriorities(parseTable.Priorities)));
		}
コード例 #8
0
		public static String WriteToString(this IParseTableFormat format, ParseTable parseTable)
		{
			#region Contract
			Contract.Requires<ArgumentNullException>(format != null);
			Contract.Requires<ArgumentNullException>(parseTable != null);
			#endregion
			using (MemoryStream stream = new MemoryStream())
			{
				format.Write(parseTable, stream);
				stream.Flush();
				stream.Position = 0;
				using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, true, 0x400, true))
				{
					return reader.ReadToEnd();
				}
			}
		}
コード例 #9
0
            /// <exception cref="NotComputedException"></exception>
            public static Parse Compute(LL1Algo algo)
            {
                _ = algo.Nullable ?? throw new NotComputedException(nameof(algo.Nullable));
                _ = algo.First ?? throw new NotComputedException(nameof(algo.First));
                _ = algo.Follow ?? throw new NotComputedException(nameof(algo.Follow));

                // init 2d dict
                var parset = new ParseTable();

                foreach (var A in algo.Thecfg.Nonterminals)
                {
                    parset.Add(A, new ParseTableRow());
                    foreach (var a in algo.Thecfg.Terminals)
                    {
                        parset[A].Add(a, new ParseTableResult());
                    }
                }

                // do algo
                foreach (var prod in algo.Thecfg.Productions)
                {
                    foreach (var a in algo.First.Sequence(prod.Rhs))
                    {
                        parset[prod.Lhs][a].Add(prod);
                    }
                    if (algo.Nullable.Sequence(prod.Rhs))
                    {
                        foreach (var a in algo.Follow[prod.Lhs])
                        {
                            parset[prod.Lhs][a].Add(prod);
                        }
                    }
                }

                return(new Parse(algo, parset));
            }
コード例 #10
0
 private Parse(LL1Algo algo, ParseTable result)
     : base(algo)
 {
     _Result  = result;
     Parsable = result.All(dict => dict.Value.All(e => e.Value.Count <= 1));
 }
コード例 #11
0
ファイル: LRParser.cs プロジェクト: KallynGowdy/Parser.Net
        /// <summary>
        ///     Sets the parse table that the parser uses with the given state graph used to help resolve parse table conflicts.
        /// </summary>
        /// <exception cref="Parser.Parsers.InvalidParseTableException">
        ///     Thrown when the given parse table contains either a 'Shift
        ///     Reduce' conflict or a 'Reduce Reduce' conflict.
        /// </exception>
        /// <param name="value">The parse table to use for parsing</param>
        /// <param name="graph">The graph to use to return informative exceptions regarding conflicts.</param>
        public virtual void SetParseTable(ParseTable <T> value, StateGraph <GrammarElement <T>, LRItem <T>[]> graph)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (graph == null)
            {
                throw new ArgumentNullException("graph");
            }

            foreach (var colRow in value.ActionTable.Select(a =>
            {
                foreach (Terminal <T> b in a.Value.Keys)
                {
                    return(new
                    {
                        Row = a,
                        Column = b,
                        Value = a.Value[b]
                    });
                }
                return(null);
            }))
            {
                //if we have a conflict
                if (colRow.Value.Count > 1)
                {
                    var conflicts = new List <ParseTableConflict <T> >();

                    StateNode <GrammarElement <T>, LRItem <T>[]> node = graph.GetBreadthFirstTraversal().ElementAt(colRow.Row.Key);


                    //ParseTableExceptionType exType;
                    ////if we have a shift-reduce conflict
                    if (colRow.Value.Any(a => a is ShiftAction <T>) &&
                        colRow.Value.Any(a => a is ReduceAction <T>))
                    {
                        LRItem <T>[] items = node.Value.Where(a =>
                        {
                            GrammarElement <T> e = a.GetNextElement();
                            if (e != null)
                            {
                                return(e.Equals(colRow.Column));
                            }
                            return(false);
                        }).Concat(colRow.Value.OfType <ReduceAction <T> >().Select(a => a.ReduceItem)).ToArray();

                        conflicts.Add(new ParseTableConflict <T>(ParseTableExceptionType.SHIFT_REDUCE, new ColumnRowPair <int, Terminal <T> >(colRow.Row.Key, colRow.Column), node, items));

                        //then check for a reduce-reduce conflict
                        if (colRow.Value.Count(a => a is ReduceAction <T>) > 1)
                        {
                            items = colRow.Value.OfType <ReduceAction <T> >().Select(a => a.ReduceItem).ToArray();
                            conflicts.Add(new ParseTableConflict <T>(ParseTableExceptionType.REDUCE_REDUCE, new ColumnRowPair <int, Terminal <T> >(colRow.Row.Key, colRow.Column), node, items));
                        }
                    }
                    //otherwise we have a reduce-reduce conflict
                    else
                    {
                        //get all of the reduce items
                        LRItem <T>[] items = colRow.Value.OfType <ReduceAction <T> >().Select(a => a.ReduceItem).ToArray();

                        conflicts.Add(new ParseTableConflict <T>(ParseTableExceptionType.REDUCE_REDUCE, new ColumnRowPair <int, Terminal <T> >(colRow.Row.Key, colRow.Column), node, items));
                    }

                    //throw invalid parse table exception
                    throw new InvalidParseTableException <T>(value, node, conflicts.ToArray());
                }
            }
            parseTable = value;
            SetEndOfInputFromTable();
        }
コード例 #12
0
ファイル: Program.cs プロジェクト: Jeffersah/GrammarParser
        static void Main(string[] args)
        {
            string input = @"3+(2*4+5*(-2+4))";

            //var tokenizer = new BasicTokenizer();
            //var splitter = new BasicSplitter();

            //splitter.CharacterCategories["operator"] = new HashSet<char>(new char[]
            //{
            //    '+', '-', '*', '/', '(', ')', '=', '.', '{', '}', '<', '>'
            //});

            //splitter.CharacterCategories["eos"] = new HashSet<char>(new char[]
            //{
            //    ';'
            //});

            //splitter.CharacterCategories["number"] = new HashSet<char>(Enumerable.Range(0, 10).Select(x => (char)('0' + x)));
            //splitter.CharacterCategories["name"] = new HashSet<char>(Enumerable.Range(0, ('z'-'a')).Select(x => (char)('a' + x)));

            //var tokens = tokenizer.Tokenize(input).ToArray();
            //tokens = splitter.Split(tokens).ToArray();

            int linenum = 0;
            int col     = 0;
            var tokens  = input.Select(value =>
            {
                var res = ToToken(value, linenum, col);
                if (value == '\n')
                {
                    linenum++;
                    col = 0;
                }
                else
                {
                    col++;
                }
                return(res);
            });

            Console.WriteLine("Reading Grammar");

            var productions = new List <Production>();
            var lines       = File.ReadAllLines("MathGrammar.txt");

            foreach (var line in lines)
            {
                if (line.Trim().Length == 0 || line.Trim().StartsWith("#"))
                {
                    continue;
                }

                var caretPosition  = line.IndexOf("->");
                var productionName = line.Substring(0, caretPosition).Trim();
                int priority       = 0;
                if (productionName.StartsWith("["))
                {
                    priority       = int.Parse(productionName.Substring(1, productionName.IndexOf(']') - 1));
                    productionName = productionName.Substring(productionName.IndexOf(']') + 1).Trim();
                }

                var spl      = line.Substring(caretPosition + 2).Split(' ').Select(x => x.Trim()).Where(x => !string.IsNullOrEmpty(x)).ToArray();
                var patterns = new StackItemPattern[spl.Length];
                for (var i = 0; i < spl.Length; i++)
                {
                    var          value = spl[i];
                    EPatternType pType = EPatternType.Literal;
                    if (value.StartsWith("{") && value.EndsWith("}"))
                    {
                        pType = EPatternType.Production;
                        value = value.Substring(1, value.Length - 2);
                    }
                    else if (value.StartsWith("<") && value.EndsWith(">"))
                    {
                        pType = EPatternType.TokenType;
                        value = value.Substring(1, value.Length - 2);
                    }
                    else if (value == "$$")
                    {
                        pType = EPatternType.EndOfInput;
                        value = "";
                    }
                    patterns[i] = new StackItemPattern(pType, value);
                }
                productions.Add(new Production(productionName, patterns)
                {
                    Precedence = priority
                });
            }

            foreach (var production in productions)
            {
                Console.WriteLine(production);
            }


            Console.WriteLine("Next: Build Parse Table [press enter]");
            Console.ReadLine();

            ParseTable parseTable = new ParseTable(productions);

            Console.WriteLine(parseTable);
            File.WriteAllText("output.txt", parseTable.ToString());
            Console.ReadLine();


            Console.WriteLine("Next: Parse");
            Console.WriteLine(input);
            Console.ReadLine();

            Parser p = new Parser(parseTable, tokens);

            var  result     = p.Step();
            bool shouldSkip = false;

            while (result == null)
            {
                if (!shouldSkip)
                {
                    PrintState(p.State);
                    Console.WriteLine("(Type SKIP to skip parsing)");
                    shouldSkip = Console.ReadLine().ToUpper() == "SKIP";
                }
                result = p.Step();
            }

            PrintRecursive(result);
            Console.ReadLine();
        }
コード例 #13
0
ファイル: Parser.cs プロジェクト: scnerd/CSLY
 internal LRParser(ParseRule[] Rules)
     : base(Rules)
 {
     Table = GenerateParseTable(Rules);
 }
コード例 #14
0
ファイル: SglrEngine.cs プロジェクト: Virtlink/noofax
		/// <summary>
		/// Initializes a new instance of the <see cref="SglrEngine"/> class.
		/// </summary>
		/// <param name="reader">The text reader to read from.</param>
		/// <param name="parseTable">The parse table.</param>
		/// <param name="parseNodeFactory">The parse node factory.</param>
		public SglrEngine(TextReader reader, ParseTable parseTable, IParseNodeFactory parseNodeFactory)
		{
			#region Contract
			Contract.Requires<ArgumentNullException>(reader != null);
			Contract.Requires<ArgumentNullException>(parseTable != null);
			Contract.Requires<ArgumentNullException>(parseNodeFactory != null);
			#endregion

			this.reader = reader as PeekingReader ?? new PeekingReader(reader);
			this.parseTable = parseTable;
			this.parseNodeFactory = parseNodeFactory;

			// C0 = (s0 | a1 ... an)
			this.AcceptingStack = null;
			this.StartFrame = new Frame(this.parseTable.InitialState);
			this.activeStacks = new Deque<Frame>() { this.StartFrame };
		}
コード例 #15
0
		/// <inheritdoc />
		public void Write(ParseTable parseTable, Stream output)
		{
			SpoofaxParseTableFormat.atermFormat.CreateWriter().Write(Print(parseTable), output);
		}
コード例 #16
0
 public ParserAction(ParseTable <T> table)
 {
     ParseTable = table;
 }
コード例 #17
0
 /// <summary>
 ///     Sets the parse table of this GLRParse based on the given grammar.
 /// </summary>
 /// <param name="grammar"></param>
 public override void SetParseTable(ContextFreeGrammar <T> grammar)
 {
     ParseTable = new ParseTable <T>(grammar);
     SetEndOfInputFromTable();
 }
コード例 #18
0
 /// <summary>
 ///     Sets the parse table of this GLRParser. The graph is ignored.
 /// </summary>
 /// <param name="table"></param>
 /// <param name="graph"></param>
 public override void SetParseTable(ParseTable <T> table, StateGraph <GrammarElement <T>, LRItem <T>[]> graph)
 {
     base.ParseTable = table;
     SetEndOfInputFromTable();
 }
コード例 #19
0
 /// <summary>
 ///     Sets the parse table of this GLRParser.
 /// </summary>
 /// <param name="table"></param>
 public override void SetParseTable(ParseTable <T> table)
 {
     base.ParseTable = table;
     SetEndOfInputFromTable();
 }
コード例 #20
0
 public InvalidParseTableException(ParseTable <T> invalidTable, StateNode <GrammarElement <T>, LRItem <T>[]> node, params ParseTableConflict <T>[] conflicts)
 {
     this.conflicts = conflicts;
     ParseTable     = invalidTable;
     InvalidNode    = node;
 }
コード例 #21
0
ファイル: ParseTest.cs プロジェクト: ph3rin/sacc
 public void SetUp()
 {
     mCfg        = CfgBuilderGenerator.Generate().Build();
     mParseTable = new ParseTableBuilder().BuildTableForCfg(mCfg);
 }
コード例 #22
0
        /// <summary>
        /// constructor, construct parser table
        /// </summary>
        public Parser(Grammar grammar)
        {
            _lrGotos = new List<int[]>();
            _gotoPrecedence = new List<int[]>();
            _lr0Items = new List<LR0Item>();
            _lr1Items = new List<LR1Item>();
            _lr0States = new List<HashSet<int>>();
            _lr0Kernels = new List<HashSet<int>>();
            _lalrStates = new List<HashSet<int>>();
            _terminals = new HashSet<int>();

            _nonterminals = new HashSet<int>();
            _lalrPropogations = new List<IDictionary<int, IList<LALRPropogation>>>();
            _grammar = grammar;
            _productions = new List<Production>();
            _productionDerivation = new List<Derivation>();
            _productionPrecedence = new List<int>();
            var nTokens = _grammar.TokenCategories.Length;
            _firstSets = new HashSet<int>[nTokens];

            PopulateProductions();
            InitSymbols();
            GenerateLR0Items();
            ComputeFirstSets();
            ConvertLR0ItemsToKernels();
            var nLalrStates = InitLALRTables();
            CalculateLookAheads();

            _parseTable = new ParseTable(nLalrStates, nTokens);
            GenerateParseTable();
        }
コード例 #23
0
 /// <summary>
 ///     Sets the parse table of this GLRParser based on the given graph and end of input element.
 /// </summary>
 /// <param name="graph"></param>
 /// <param name="endOfInputElement"></param>
 public override void SetParseTable(StateGraph <GrammarElement <T>, LRItem <T>[]> graph, Terminal <T> endOfInputElement)
 {
     ParseTable = new ParseTable <T>(graph);
     SetEndOfInputFromTable();
 }
コード例 #24
0
 /// <summary>
 ///     Creates a new GLRParse with the given parse table.
 /// </summary>
 /// <param name="table"></param>
 public GLRParser(ParseTable <T> table)
 {
     base.ParseTable = table;
     SetEndOfInputFromTable();
 }
コード例 #25
0
ファイル: IParseTableFormat.cs プロジェクト: Virtlink/noofax
			public void Write(ParseTable parseTable, Stream output)
			{
				Contract.Requires<ArgumentNullException>(parseTable != null);
				Contract.Requires<ArgumentNullException>(output != null);
				Contract.Requires<ArgumentException>(output.CanWrite);
			}