Beispiel #1
0
 private static void ReInitAll()
 {
     Expansion.reInit();
     CSharpCCErrors.ReInit();
     CSharpCCGlobals.ReInit();
     Options.init();
     CSharpCCParserInternals.reInit();
     RStringLiteral.reInit();
     // CSharpFiles.reInit();
     LexGen.reInit();
     NfaState.reInit();
     MatchInfo.reInit();
     LookaheadWalk.reInit();
     Semanticize.reInit();
     ParseGen.reInit();
     OtherFilesGen.reInit();
     ParseEngine.reInit();
 }
Beispiel #2
0
        public void GenerateNoErrors()
        {
            var input = MakeUpGrammar();

            SetupOptions();

            using (var reader = new StringReader(input)) {
                var parser = new CSharpCCParser(reader);

                CSharpCCGlobals.FileName = CSharpCCGlobals.OriginalFileName = "SimpleParser.cc";
                parser.csharpcc_input();
                CSharpCCGlobals.CreateOutputDir(Options.getOutputDirectory().FullName);

                Semanticize.start();
                ParseGen.start();
                LexGen.start();
                OtherFilesGen.start();
            }

            Assert.AreEqual(0, CSharpCCErrors.ErrorCount);
        }
Beispiel #3
0
 public void Action(Expansion e)
 {
     if (e is OneOrMore)
     {
         if (Semanticize.EmptyExpansionExists(((OneOrMore)e).Expansion))
         {
             CSharpCCErrors.SemanticError(e, "Expansion within \"(...)+\" can be matched by empty string.");
         }
     }
     else if (e is ZeroOrMore)
     {
         if (Semanticize.EmptyExpansionExists(((ZeroOrMore)e).Expansion))
         {
             CSharpCCErrors.SemanticError(e, "Expansion within \"(...)*\" can be matched by empty string.");
         }
     }
     else if (e is ZeroOrOne)
     {
         if (Semanticize.EmptyExpansionExists(((ZeroOrOne)e).Expansion))
         {
             CSharpCCErrors.SemanticError(e, "Expansion within \"(...)?\" can be matched by empty string.");
         }
     }
 }
Beispiel #4
0
        public static void choiceCalc(Choice choice)
        {
            int first = firstChoice(choice);

            // dbl[i] and dbr[i] are lists of size limited matches for choice i
            // of choice.  dbl ignores matches with semantic lookaheads (when force_la_check
            // is false), while dbr ignores semantic lookahead.
            IList <MatchInfo>[] dbl       = new IList <MatchInfo> [choice.Choices.Count];
            IList <MatchInfo>[] dbr       = new IList <MatchInfo> [choice.Choices.Count];
            int[]             minLA       = new int[choice.Choices.Count - 1];
            MatchInfo[]       overlapInfo = new MatchInfo[choice.Choices.Count - 1];
            int[]             other       = new int[choice.Choices.Count - 1];
            MatchInfo         m;
            IList <MatchInfo> v;
            bool overlapDetected;

            for (int la = 1; la <= Options.getChoiceAmbiguityCheck(); la++)
            {
                MatchInfo.laLimit = la;
                LookaheadWalk.considerSemanticLA = !Options.getForceLaCheck();
                for (int i = first; i < choice.Choices.Count - 1; i++)
                {
                    LookaheadWalk.sizeLimitedMatches = new List <MatchInfo>();
                    m = new MatchInfo();
                    m.firstFreeLoc = 0;
                    v = new List <MatchInfo>();
                    v.Add(m);
                    LookaheadWalk.genFirstSet(v, (Expansion)choice.Choices[i]);
                    dbl[i] = LookaheadWalk.sizeLimitedMatches;
                }
                LookaheadWalk.considerSemanticLA = false;
                for (int i = first + 1; i < choice.Choices.Count; i++)
                {
                    LookaheadWalk.sizeLimitedMatches = new List <MatchInfo>();
                    m = new MatchInfo();
                    m.firstFreeLoc = 0;
                    v = new List <MatchInfo>();
                    v.Add(m);
                    LookaheadWalk.genFirstSet(v, (Expansion)choice.Choices[i]);
                    dbr[i] = LookaheadWalk.sizeLimitedMatches;
                }
                if (la == 1)
                {
                    for (int i = first; i < choice.Choices.Count - 1; i++)
                    {
                        Expansion exp = (Expansion)choice.Choices[i];
                        if (Semanticize.EmptyExpansionExists(exp))
                        {
                            CSharpCCErrors.Warning(exp,
                                                   "This choice can expand to the empty token sequence " +
                                                   "and will therefore always be taken in favor of the choices appearing later.");
                            break;
                        }
                        else if (CodeCheck(dbl[i]))
                        {
                            CSharpCCErrors.Warning(exp,
                                                   "CSHARPCODE non-terminal will force this choice to be taken " +
                                                   "in favor of the choices appearing later.");
                            break;
                        }
                    }
                }
                overlapDetected = false;
                for (int i = first; i < choice.Choices.Count - 1; i++)
                {
                    for (int j = i + 1; j < choice.Choices.Count; j++)
                    {
                        if ((m = overlap(dbl[i], dbr[j])) != null)
                        {
                            minLA[i]        = la + 1;
                            overlapInfo[i]  = m;
                            other[i]        = j;
                            overlapDetected = true;
                            break;
                        }
                    }
                }
                if (!overlapDetected)
                {
                    break;
                }
            }
            for (int i = first; i < choice.Choices.Count - 1; i++)
            {
                if (explicitLA((Expansion)choice.Choices[i]) && !Options.getForceLaCheck())
                {
                    continue;
                }
                if (minLA[i] > Options.getChoiceAmbiguityCheck())
                {
                    CSharpCCErrors.Warning("Choice conflict involving two expansions at");
                    Console.Error.Write("         line " + ((Expansion)choice.Choices[i]).Line);
                    Console.Error.Write(", column " + ((Expansion)choice.Choices[i]).Column);
                    Console.Error.Write(" and line " + ((Expansion)choice.Choices[other[i]]).Line);
                    Console.Error.Write(", column " + ((Expansion)choice.Choices[other[i]]).Column);
                    Console.Error.WriteLine(" respectively.");
                    Console.Error.WriteLine("         A common prefix is: " + image(overlapInfo[i]));
                    Console.Error.WriteLine("         Consider using a lookahead of " + minLA[i] + " or more for earlier expansion.");
                }
                else if (minLA[i] > 1)
                {
                    CSharpCCErrors.Warning("Choice conflict involving two expansions at");
                    Console.Error.Write("         line " + ((Expansion)choice.Choices[i]).Line);
                    Console.Error.Write(", column " + ((Expansion)choice.Choices[i]).Column);
                    Console.Error.Write(" and line " + ((Expansion)choice.Choices[other[i]]).Line);
                    Console.Error.Write(", column " + ((Expansion)choice.Choices[other[i]]).Column);
                    Console.Error.WriteLine(" respectively.");
                    Console.Error.WriteLine("         A common prefix is: " + image(overlapInfo[i]));
                    Console.Error.WriteLine("         Consider using a lookahead of " + minLA[i] + " for earlier expansion.");
                }
            }
        }
Beispiel #5
0
        public static int MainProgram(String[] args)
        {
            // Initialize all static state
            ReInitAll();

            CSharpCCGlobals.BannerLine("Parser Generator", "");

            CSharpCCParser parser = null;

            if (args.Length == 0)
            {
                Console.Out.WriteLine("");
                help_message();
                return(1);
            }
            else
            {
                Console.Out.WriteLine("(type \"csharpcc\" with no arguments for help)");
            }

            if (Options.IsOption(args[args.Length - 1]))
            {
                Console.Out.WriteLine("Last argument \"" + args[args.Length - 1] + "\" is not a filename.");
                return(1);
            }
            for (int arg = 0; arg < args.Length - 1; arg++)
            {
                if (!Options.IsOption(args[arg]))
                {
                    Console.Out.WriteLine("Argument \"" + args[arg] + "\" must be an option setting.");
                    return(1);
                }
                Options.SetCmdLineOption(args[arg]);
            }

            try {
                FileInfo fp = new FileInfo(args[args.Length - 1]);
                if (!fp.Exists)
                {
                    Console.Out.WriteLine("File " + args[args.Length - 1] + " not found.");
                    return(1);
                }
                parser =
                    new CSharpCCParser(
                        new StreamReader(new FileStream(args[args.Length - 1], FileMode.Open, FileAccess.Read, FileShare.Read),
                                         Encoding.GetEncoding(Options.getGrammarEncoding())));
            } catch (SecurityException) {
                Console.Out.WriteLine("Security violation while trying to open " + args[args.Length - 1]);
                return(1);
            } catch (FileNotFoundException) {
                Console.Out.WriteLine("File " + args[args.Length - 1] + " not found.");
                return(1);
            }

            try {
                Console.Out.WriteLine("Reading from file " + args[args.Length - 1] + " . . .");
                CSharpCCGlobals.FileName      = CSharpCCGlobals.OriginalFileName = args[args.Length - 1];
                CSharpCCGlobals.TreeGenerated = CSharpCCGlobals.IsGeneratedBy("CSTree", args[args.Length - 1]);
                CSharpCCGlobals.ToolNames     = CSharpCCGlobals.GetToolNames(args[args.Length - 1]);
                parser.csharpcc_input();
                CSharpCCGlobals.CreateOutputDir(Options.getOutputDirectory().FullName);

                if (Options.getUnicodeInput())
                {
                    NfaState.unicodeWarningGiven = true;
                    Console.Out.WriteLine("Note: UNICODE_INPUT option is specified. " +
                                          "Please make sure you create the parser/lexer using a Reader with the correct character encoding.");
                }

                Semanticize.start();
                ParseGen.start();
                LexGen.start();
                OtherFilesGen.start();

                if ((CSharpCCErrors.ErrorCount == 0) && (Options.getBuildParser() || Options.getBuildTokenManager()))
                {
                    if (CSharpCCErrors.WarningCount == 0)
                    {
                        Console.Out.WriteLine("Parser generated successfully.");
                    }
                    else
                    {
                        Console.Out.WriteLine("Parser generated with 0 errors and "
                                              + CSharpCCErrors.WarningCount + " warnings.");
                    }
                    return(0);
                }
                else
                {
                    Console.Out.WriteLine("Detected " + CSharpCCErrors.ErrorCount + " errors and "
                                          + CSharpCCErrors.WarningCount + " warnings.");
                    return((CSharpCCErrors.ErrorCount == 0) ? 0 : 1);
                }
            } catch (MetaParseException e) {
                Console.Out.WriteLine("Detected " + CSharpCCErrors.ErrorCount + " errors and "
                                      + CSharpCCErrors.WarningCount + " warnings.");
                return(1);
            } catch (ParseException e) {
                Console.Out.WriteLine(e.ToString());
                Console.Out.WriteLine("Detected " + (CSharpCCErrors.ErrorCount + 1) + " errors and "
                                      + CSharpCCErrors.WarningCount + " warnings.");
                return(1);
            }
        }