Exemplo n.º 1
0
Arquivo: Form1.cs Projeto: dfyz/stuff
 private void ParseButton_Click(object sender, System.EventArgs e)
 {
     Parser P = new Parser(new LexicalAnalyser(ParseInput.Text));
     Expression E = P.Parse();
     expTree.BeginUpdate();
     expTree.Nodes.Clear();
     TreeNode tr = expTree.Nodes.Add(FormName(E));
     ParseTree(tr.Nodes, E);
     expTree.EndUpdate();
 }
Exemplo n.º 2
0
Arquivo: Class1.cs Projeto: dfyz/stuff
 public static new void Parse(Parser parser,int basePriority)
 {
     switch (parser.Analyser.Peek().Type)
     {
         case LexemeType.add: case LexemeType.sub: case LexemeType.mul: case LexemeType.div:
             break;
         default:
             throw new InvalidOperationException("Ожидается арифметическое действие!");
     }
     Lexeme lexeme = parser.Analyser.Peek();
     LexemeType operation = parser.Analyser.Read().Type;
     Expression.Parse(parser, lexeme.GetPriority());
     switch (operation)
     {
         case LexemeType.add:
             parser.ParseStack.Push(new Addition((Expression)parser.ParseStack.Pop(),(Expression)parser.ParseStack.Pop()));
             break;
         case LexemeType.sub:
             Expression op1 = (Expression)parser.ParseStack.Pop();
             parser.ParseStack.Push(new Substract((Expression)parser.ParseStack.Pop(),op1));
             break;
         case LexemeType.mul:
             parser.ParseStack.Push(new Multiplication((Expression)parser.ParseStack.Pop(),(Expression)parser.ParseStack.Pop()));
             break;
         case LexemeType.div:
             Expression op2 = (Expression)parser.ParseStack.Pop();
             parser.ParseStack.Push(new Division((Expression)parser.ParseStack.Pop(),op2));
             break;
     }
 }
Exemplo n.º 3
0
Arquivo: Class1.cs Projeto: dfyz/stuff
 public static void Parse(Parser parser, int basePriority)
 {
     int context = 0;
     while ( true )
     {
         switch ( context )
         {
             case 0:
                 context = 1;
                 switch ( parser.Analyser.Peek().Type )
                 {
                     case LexemeType.num:
                         Number.Parse(parser, 0);
                         continue;
                     case LexemeType.bracketOpen:
                         CompoundExpression.Parse(parser, 0);
                         continue;
                     case LexemeType.bracketClose:
                         return;
                     case LexemeType.end:
                         return;
                     default:
                         throw new Exception("На входе инвалидное выражение!");
                 }
             case 1:
                 bool lPriority = ( parser.Analyser.Peek().GetPriority() <= basePriority );
      						switch ( parser.Analyser.Peek().Type )
                 {
                     case LexemeType.add: case LexemeType.sub: case LexemeType.mul: case LexemeType.div:
                         if ( lPriority ) return;
                         BinaryOperation.Parse(parser, parser.Analyser.Peek().GetPriority());
                         continue;
                     case LexemeType.bracketClose: case LexemeType.end:
                         context = 0;
                         return;
                     default:
                         throw new Exception("На входе невалидное выражение!");
                 }
                 break;
         }
     }
 }
Exemplo n.º 4
0
Arquivo: Class1.cs Projeto: dfyz/stuff
 public static new void Parse(Parser parser,int basePriority)
 {
     if ( parser.Analyser.Peek().Type != LexemeType.num )
         throw new Exception("Ожидается число!");
      			Lexeme lex = parser.Analyser.Read();
     parser.ParseStack.Push(new Number((decimal)lex.Value));
 }
Exemplo n.º 5
0
Arquivo: Class1.cs Projeto: dfyz/stuff
 public static new void Parse(Parser parser,int basePriority)
 {
     if ( parser.Analyser.Peek().Type != LexemeType.bracketOpen )
         throw new Exception("Ожидается ( !");
     Lexeme lexeme = parser.Analyser.Peek();
     parser.Analyser.Read();
     Expression.Parse(parser, lexeme.GetPriority());
     if ( parser.Analyser.Peek().Type != LexemeType.bracketClose )
         throw new Exception("Ожидается ) !");
     parser.Analyser.Read();
     parser.ParseStack.Push(new CompoundExpression((Expression)parser.ParseStack.Pop()));
 }