码迷,mamicode.com
首页 > 其他好文 > 详细

CocoSourcesCS 1

时间:2015-06-24 23:55:01      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

CocoSourcesCS 1

  1 /*-------------------------------------------------------------------------
  2 Compiler Generator Coco/R,
  3 Copyright (c) 1990, 2004 Hanspeter Moessenboeck, University of Linz
  4 extended by M. Loeberbauer & A. Woess, Univ. of Linz
  5 with improvements by Pat Terry, Rhodes University
  6 
  7 This program is free software; you can redistribute it and/or modify it 
  8 under the terms of the GNU General Public License as published by the 
  9 Free Software Foundation; either version 2, or (at your option) any 
 10 later version.
 11 
 12 This program is distributed in the hope that it will be useful, but 
 13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
 15 for more details.
 16 
 17 You should have received a copy of the GNU General Public License along 
 18 with this program; if not, write to the Free Software Foundation, Inc., 
 19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 20 
 21 As an exception, it is allowed to write an extension of Coco/R that is
 22 used as a plugin in non-free software.
 23 
 24 If not otherwise stated, any source code generated by Coco/R (other than 
 25 Coco/R itself) does not fall under the GNU General Public License.
 26 -------------------------------------------------------------------------*/
 27 /*-------------------------------------------------------------------------
 28   Trace output options
 29   0 | A: prints the states of the scanner automaton
 30   1 | F: prints the First and Follow sets of all nonterminals
 31   2 | G: prints the syntax graph of the productions
 32   3 | I: traces the computation of the First sets
 33   4 | J: prints the sets associated with ANYs and synchronisation sets
 34   6 | S: prints the symbol table (terminals, nonterminals, pragmas)
 35   7 | X: prints a cross reference list of all syntax symbols
 36   8 | P: prints statistics about the Coco run
 37   
 38   Trace output can be switched on by the pragma
 39     $ { digit | letter }
 40   in the attributed grammar or as a command-line option
 41   -------------------------------------------------------------------------*/
 42 
 43 using System;
 44 using System.IO;
 45 
 46 namespace at.jku.ssw.Coco {
 47 
 48 public class Coco {
 49         
 50     public static int Main (string[] arg) {
 51         Console.WriteLine("Coco/R (Apr 19, 2011)");
 52         string srcName = null, nsName = null, frameDir = null, ddtString = null,
 53         traceFileName = null, outDir = null;
 54         bool emitLines = false;
 55         int retVal = 1;
 56         for (int i = 0; i < arg.Length; i++) {
 57             if (arg[i] == "-namespace" && i < arg.Length - 1) nsName = arg[++i].Trim();
 58             else if (arg[i] == "-frames" && i < arg.Length - 1) frameDir = arg[++i].Trim();
 59             else if (arg[i] == "-trace" && i < arg.Length - 1) ddtString = arg[++i].Trim();
 60             else if (arg[i] == "-o" && i < arg.Length - 1) outDir = arg[++i].Trim();
 61             else if (arg[i] == "-lines") emitLines = true;
 62             else srcName = arg[i];
 63         }
 64         if (arg.Length > 0 && srcName != null) {
 65             try {
 66                 string srcDir = Path.GetDirectoryName(srcName);
 67                 
 68                 Scanner scanner = new Scanner(srcName);
 69                 Parser parser = new Parser(scanner);
 70 
 71                 traceFileName = Path.Combine(srcDir, "trace.txt");
 72                 parser.trace = new StreamWriter(new FileStream(traceFileName, FileMode.Create));
 73                 parser.tab = new Tab(parser);
 74                 parser.dfa = new DFA(parser);
 75                 parser.pgen = new ParserGen(parser);
 76 
 77                 parser.tab.srcName = srcName;
 78                 parser.tab.srcDir = srcDir;
 79                 parser.tab.nsName = nsName;
 80                 parser.tab.frameDir = frameDir;
 81                 parser.tab.outDir = (outDir != null) ? outDir : srcDir;
 82                 parser.tab.emitLines = emitLines;
 83                 if (ddtString != null) parser.tab.SetDDT(ddtString);
 84 
 85                 parser.Parse();
 86 
 87                 parser.trace.Close();
 88                 FileInfo f = new FileInfo(traceFileName);
 89                 if (f.Length == 0) f.Delete();
 90                 else Console.WriteLine("trace output is in " + traceFileName);
 91                 Console.WriteLine("{0} errors detected", parser.errors.count);
 92                 if (parser.errors.count == 0) { retVal = 0; }
 93             } catch (IOException) {
 94                 Console.WriteLine("-- could not open " + traceFileName);
 95             } catch (FatalError e) {
 96                 Console.WriteLine("-- " + e.Message);
 97             }
 98         } else {
 99             Console.WriteLine("Usage: Coco Grammar.ATG {{Option}}{0}"
100                               + "Options:{0}"
101                               + "  -namespace <namespaceName>{0}"
102                               + "  -frames    <frameFilesDirectory>{0}"
103                               + "  -trace     <traceString>{0}"
104                               + "  -o         <outputDirectory>{0}"
105                               + "  -lines{0}"
106                               + "Valid characters in the trace string:{0}"
107                               + "  A  trace automaton{0}"
108                               + "  F  list first/follow sets{0}"
109                               + "  G  print syntax graph{0}"
110                               + "  I  trace computation of first sets{0}"
111                               + "  J  list ANY and SYNC sets{0}"
112                               + "  P  print statistics{0}"
113                               + "  S  list symbol table{0}"
114                               + "  X  list cross reference table{0}"
115                               + "Scanner.frame and Parser.frame files needed in ATG directory{0}"
116                               + "or in a directory specified in the -frames option.",
117                               Environment.NewLine);
118         }
119         return retVal;
120     }
121         
122 } // end Coco
123 
124 } // end namespace

 

CocoSourcesCS 1

标签:

原文地址:http://www.cnblogs.com/endv/p/4598815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!