示例#1
0
        static void Main(string[] args)
        {
            Console.Title = "MyCompiler - Lex";

            var argManager = new ArgManager(args);

            bool debug = argManager.Contains("-d");
            //debug = true;

            string sourceFilename = argManager.First();
            if (string.IsNullOrEmpty(sourceFilename))
                sourceFilename = argManager.Find("-s");

            //if (debug && string.IsNullOrEmpty(sourceFilename)) // IF DEBUG
            //    sourceFilename = "main.c";

            string outputFilename = argManager.Find("-o");
            if (string.IsNullOrEmpty(outputFilename))
                outputFilename = "lexoutput.txt";
            string tokenDefinitionFilename = argManager.Find("-t");
            if (string.IsNullOrEmpty(tokenDefinitionFilename))
                tokenDefinitionFilename = "lextoken.txt";

            // Error
            if (!File.Exists(sourceFilename))
            {
                Console.WriteLine(String.Format("Error, source file: \"{0}\" not found!", sourceFilename));
                return;
            }
            if (!File.Exists(tokenDefinitionFilename))
            {
                Console.WriteLine(String.Format("Error, token definition file: \"{0}\" not found!", tokenDefinitionFilename));
                return;
            }

            var scanner = new Scanner();

            // Load token definitions
            foreach (var tokenDefinition in (new TokenDefinitionParser(tokenDefinitionFilename)).Parse())
                scanner.AddDefinition(tokenDefinition);

            var tokens = scanner.Tokenize(File.ReadAllText(sourceFilename));

            if (tokens == null)
                return;

            StreamWriter wr = new StreamWriter(outputFilename);

            foreach (var token in tokens)
            {
                Console.WriteLine(token);
                wr.WriteLine(token);
            }

            wr.Close();
            wr.Dispose();

            if (debug)
                Console.ReadKey();
        }
示例#2
0
        public BNFParser(string filename)
        {
            if (File.Exists(filename))
                Filename = filename;

            scanner = new Scanner();
            scanner.AddDefinition(new TokenDefinition("TOKEN_EQUALS", new Regex(@"\:=")));
            scanner.AddDefinition(new TokenDefinition("TOKEN_OR", new Regex(@"\|")));
            scanner.AddDefinition(new TokenDefinition("TOKEN_SEMICOLON", new Regex(@"\;")));
            scanner.AddDefinition(new TokenDefinition("TOKEN_SYMBOL", new Regex(@"[a-zA-Z_][a-zA-Z0-9_]+")));

            scanner.AddDefinition(new TokenDefinition("TOKEN_EOF", new Regex(@"\(end\)")));
            scanner.AddDefinition(new TokenDefinition("TOKEN_EMPTY", new Regex(@"\(empty\)")));
            scanner.AddDefinition(new TokenDefinition("TOKEN_WHITESPACE", new Regex(@"\s"), true));
        }
 protected void SetUp()
 {
     _scanner = new Scanner();
     _parser = new Parser();
 }
 protected void SetUp()
 {
     _scanner = new Scanner();
 }