Пример #1
0
        private void Compilar_Click(object sender, EventArgs e)
        {
            MyParser parser = new MyParser(pathArchivo + "\\DecafGP.cgt");

            TreeNode tree = parser.Parse(TextArea.Text);

            treeView1.Nodes.Clear();
            if (tree != null)
            {
                treeView1.Nodes.Add(tree);
                //statusLabel.Text = @"Success";
            }
            else
            {
                //statusLabel.Text = parser.ErrorMessage;
            }
        }
Пример #2
0
		static void Main(string[] args)
		{	
			if (args.Length==0)
			{
				args=new string[3];
                Console.WriteLine("args: grammar.grm template.tmpl result.cs");
                return;
				args[0]="pascalabc.grm";args[1]="PABCParserTemplate.cs";args[2]="PABCParser.cs";
			}

			parser=new MyParser(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName)+"\\grmCommentCompiler.cgt");
			StreamReader sr=File.OpenText(args[0]);
			Console.Write("Parsing file "+args[0]+"...");
			string s=sr.ReadToEnd();
			if (parser.Parse(s)==null) return;
			Console.WriteLine("OK");

			string TerminalTemplate=ExtractTemplateFromComment(parser.Comments,"[TERMINALTEMPLATE]");
			if (TerminalTemplate=="")
			{
				Console.WriteLine("Error: template [TERMINALTEMPLATE] not defined");
				return;
			}
			//Console.WriteLine(TerminalTemplate);
			
			string[] NonTerminalTemplate=new string[maxnontemtempl+1];
			for (int i=0;i<=maxnontemtempl;i++)
				NonTerminalTemplate[i]=ExtractTemplateFromComment(parser.Comments,"[NONTERMINALTEMPLATE"+i+"]");
			if (NonTerminalTemplate[0]=="")
			{
				Console.WriteLine("Error: template [NONTERMINALTEMPLATE0] not defined");
				return;
			}
			//Console.WriteLine(NonTerminalTemplate);
			
			string csterminaldef="//TERMINAL:";
			string csterminalend="//ENDTERMINAL";
			string csnonterminaldef="//NONTERMINAL:";
			string csnonterminalend="//ENDNONTERMINAL";
			StreamReader csfile=File.OpenText(args[1]);
			StreamWriter outfile=new StreamWriter(args[2]);
			string grmname,cstext;
			int bg,ed,tc=0,ntc=0;
			while(csfile.Peek()>=0)
			{
				s=csfile.ReadLine();
				if (s.IndexOf(csterminaldef)!=-1)
				{
					bg=s.IndexOf(':')+1;
					grmname=s.Substring(bg,s.Length-bg);
					cstext=parser.TerminalsDefs[grmname];
					if (cstext!=null)
					{
						while(s.IndexOf(csterminalend)==-1) s=csfile.ReadLine();
						s=CompileTemplateForTerminal(TerminalTemplate,cstext);
						tc++;
					}
				}
				else
				{
					if (s.IndexOf(csnonterminaldef)!=-1)
					{
						bg=s.IndexOf(':')+1;
						grmname=s.Substring(bg,s.Length-bg);
						grmname=grmname.Replace("'","");
						cstext=parser.TerminalsDefs[grmname];
						if (cstext!=null)
						{
							while(s.IndexOf(csnonterminalend)==-1) s=csfile.ReadLine();
							s=CompileTemplateForNonTerminal(NonTerminalTemplate,cstext);
							//debug
							//s="Console.WriteLine(\""+grmname+"\");"+s;

							ntc++;
							//Console.WriteLine(s);
						}
					}
				}

				outfile.WriteLine(s);
			}
			csfile.Close();
			outfile.Flush();
			outfile.Close();
			Console.WriteLine("Changed: "+tc+" terminals, "+ntc+" nonteminals");


		}