private static void Main(string[] args)
{
Stream inputFile = null;
Grammar grammar = null;
ErrorHandler handler = new ErrorHandler();
Lexers.Scanner scanner = null;
Parser.Parser parser = null;
Assembly assm = Assembly.GetExecutingAssembly();
object info = Attribute.GetCustomAttribute(assm, typeof(AssemblyFileVersionAttribute));
versionInfo = ((AssemblyFileVersionAttribute)info).Version;
try
{
string filename = ProcessOptions(args);
if (filename == null)
{
return;
}
try
{
inputFile = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch (IOException)
{
inputFile = null;
string message = String.Format(CultureInfo.InvariantCulture, "Source file <{0}> not found{1}", filename, Environment.NewLine);
handler.AddError(message, null); // aast.AtStart;
throw;
}
scanner = new Lexers.Scanner(inputFile);
scanner.SetHandler(handler);
parser = new Parser.Parser(filename, scanner, handler);
//
// If the parse is successful, then process the grammar.
// Otherwise just report the errors that have been listed.
//
if (parser.Parse())
{
grammar = parser.Grammar;
if (Terminal.Max > 255)
{
handler.ListError(null, 103, CharacterUtilities.Map(Terminal.Max), '\'');
}
LALRGenerator generator = new LALRGenerator(grammar);
List <AutomatonState> states = generator.BuildStates();
generator.ComputeLookAhead();
generator.BuildParseTable();
if (!grammar.CheckGrammar(handler))
{
throw new ArgumentException("Non-terminating grammar");
}
//
// If the grammar has non-terminating non-terms we cannot
// create a diagnostic report as the grammar is incomplete.
//
bool DoDiagnose = Diagnose && !grammar.HasNonTerminatingNonTerms;
if (Report || DoDiagnose)
{
string htmlName = System.IO.Path.ChangeExtension(filename, ".report.html");
try
{
System.IO.FileStream htmlFile = new System.IO.FileStream(htmlName, System.IO.FileMode.Create);
System.IO.StreamWriter htmlWriter = new System.IO.StreamWriter(htmlFile);
Grammar.HtmlHeader(htmlWriter, filename);
if (Report && DoDiagnose)
{
grammar.GenerateCompoundReport(htmlWriter, filename, states);
}
else if (Report)
{
grammar.GenerateReport(htmlWriter, filename, states);
}
Grammar.HtmlTrailer(htmlWriter);
if (htmlFile != null)
{
htmlWriter.Flush();
htmlFile.Close();
}
}
catch (System.IO.IOException)
{
Console.Error.WriteLine("Cannot create html output file {0}", htmlName);
}
}
else if (!handler.Errors)
{
CodeGenerator code = new CodeGenerator();
code.Generate(states, grammar);
}
}
}
catch (System.Exception e)
{
Console.Error.WriteLine("Unexpected Error {0}", e.Message);
throw; // Now rethrow the caught exception.
}
finally
{
if ((handler.Errors || handler.Warnings) && scanner != null)
{
handler.DumpAll(scanner.Buffer, Console.Error);
}
if (Listing || handler.Errors || handler.Warnings)
{
string listName = parser.ListfileName;
StreamWriter listStream = ListingFile(listName);
if (listStream != null)
{
handler.MakeListing(scanner.Buffer, listStream, parser.SourceFileName, versionInfo);
}
}
}
}