Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Irony - .NET Compiler Construction Kit

, 4 Jan 2008
Introduction to Irony - a new technology of parser/compiler construction for .NET.
irony_article.zip
Irony_article
irony_exprTree.jpg
irony_GrammarExplorer.jpg
Irony_src.zip
irony_src.zip
Irony_src
Irony.GrammarExplorer
.svn
all-wcprops
entries
format
prop-base
props
text-base
030.Irony.GrammarExplorer.csproj.svn-base
app.config.svn-base
fmGrammarExplorer.cs.svn-base
fmGrammarExplorer.Designer.cs.svn-base
fmGrammarExplorer.resx.svn-base
fmShowException.cs.svn-base
fmShowException.Designer.cs.svn-base
fmShowException.resx.svn-base
License.txt.svn-base
Program.cs.svn-base
tmp
prop-base
props
text-base
Properties
.svn
all-wcprops
entries
format
prop-base
props
text-base
AssemblyInfo.cs.svn-base
Resources.Designer.cs.svn-base
Resources.resx.svn-base
Settings.Designer.cs.svn-base
Settings.settings.svn-base
tmp
prop-base
props
text-base
Settings.settings
Irony.Samples
.svn
all-wcprops
entries
format
prop-base
props
text-base
020.Irony.Samples.csproj.svn-base
License.txt.svn-base
tmp
prop-base
props
text-base
OtherGrammars
.svn
all-wcprops
entries
format
prop-base
props
text-base
ExpressionGrammar.cs.svn-base
GrammarEx434.cs.svn-base
GrammarEx446.cs.svn-base
GrammarExL514.cs.svn-base
tmp
prop-base
props
text-base
Properties
.svn
all-wcprops
entries
format
prop-base
props
text-base
AssemblyInfo.cs.svn-base
tmp
prop-base
props
text-base
Python
.svn
all-wcprops
entries
format
prop-base
props
text-base
Python_auth_svn.txt.svn-base
PythonGrammar.cs.svn-base
tmp
prop-base
props
text-base
Ruby
.svn
all-wcprops
entries
format
prop-base
props
text-base
Ruby_auth.txt.svn-base
RubyGrammar.cs.svn-base
tmp
prop-base
props
text-base
Scheme
.svn
all-wcprops
entries
format
prop-base
props
text-base
SampleAstNodes.cs.svn-base
SchemeGrammar.cs.svn-base
tmp
prop-base
props
text-base
SourceSamples
.svn
all-wcprops
entries
format
prop-base
props
text-base
_about.txt.svn-base
99 bottles.py.svn-base
99 bottles.rb.svn-base
99 bottles.scm.svn-base
ExprSample.txt.svn-base
tmp
prop-base
props
text-base
99 bottles.rb
99 bottles.scm
Irony
.svn
all-wcprops
entries
format
prop-base
props
text-base
010.Irony.csproj.svn-base
Common.cs.svn-base
License.txt.svn-base
tmp
prop-base
props
text-base
Compiler
.svn
all-wcprops
entries
format
prop-base
props
text-base
CompilerContext.cs.svn-base
Enums.cs.svn-base
EventArgs.cs.svn-base
LanguageCompiler.cs.svn-base
SyntaxError.cs.svn-base
tmp
prop-base
props
text-base
AST
.svn
all-wcprops
entries
format
prop-base
props
text-base
AstNode.cs.svn-base
GenericNode.cs.svn-base
tmp
prop-base
props
text-base
Grammar
.svn
all-wcprops
entries
format
prop-base
props
text-base
BnfElement.cs.svn-base
BnfExpression.cs.svn-base
Grammar.cs.svn-base
GrammarData.cs.svn-base
GrammarDataBuilder.cs.svn-base
tmp
prop-base
props
text-base
NonTerminals
.svn
all-wcprops
entries
format
prop-base
props
text-base
NonTerminal.cs.svn-base
tmp
prop-base
props
text-base
Parser
.svn
all-wcprops
entries
format
prop-base
props
text-base
Parser.cs.svn-base
ParserStack.cs.svn-base
tmp
prop-base
props
text-base
Scanner
.svn
all-wcprops
entries
format
prop-base
props
text-base
Scanner.cs.svn-base
SourceFile.cs.svn-base
Token.cs.svn-base
tmp
prop-base
props
text-base
Terminals
.svn
all-wcprops
entries
format
prop-base
props
text-base
_Terminal.cs.svn-base
CharLiteral.cs.svn-base
CommentTerminal.cs.svn-base
ConstantSetTerminal.cs.svn-base
CustomTerminal.cs.svn-base
IdentifierTerminal.cs.svn-base
NumberTerminal.cs.svn-base
RegExBasedTerminal.cs.svn-base
StringLiteral.cs.svn-base
SymbolTerminal.cs.svn-base
tmp
prop-base
props
text-base
TokenFilters
.svn
all-wcprops
entries
format
prop-base
props
text-base
BraceMatchFilter.cs.svn-base
CodeOutlineFilter.cs.svn-base
TokenFilter.cs.svn-base
tmp
prop-base
props
text-base
Properties
.svn
all-wcprops
entries
format
prop-base
props
text-base
AssemblyInfo.cs.svn-base
tmp
prop-base
props
text-base
#region License
/* **********************************************************************************
 * Copyright (c) Roman Ivantsov
 * This source code is subject to terms and conditions of the MIT License
 * for Irony. A copy of the license can be found in the License.txt file
 * at the root of this distribution. 
 * By using this source code in any fashion, you are agreeing to be bound by the terms of the 
 * MIT License.
 * You must not remove this notice from this software.
 * **********************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Configuration;
using Irony.Compiler;
using Irony.GrammarExplorer.Properties;

namespace Irony.GrammarExplorer {
  public partial class fmGrammarExplorer : Form {
    public fmGrammarExplorer() {
      InitializeComponent();
    }
    private void fmExploreGrammar_Load(object sender, EventArgs e) {
      try {
        cboLanguage.SelectedIndex = Settings.Default.LanguageIndex;
        txtSource.Text = Settings.Default.SourceSample;
      } catch { }
    }
    private void fmExploreGrammar_FormClosing(object sender, FormClosingEventArgs e) {
      Settings.Default.SourceSample = txtSource.Text;
      Settings.Default.LanguageIndex = cboLanguage.SelectedIndex;
      Settings.Default.Save();
    }//method



    LanguageCompiler Compiler  {
      get {return _compiler;}
    } LanguageCompiler  _compiler;

    private void btnParse_Click(object sender, EventArgs e) {
      AstNode rootNode = null;
      ResetResultPanels();
      if (chkShowTrace.Checked) {
        Compiler.Parser.ActionSelected += Parser_ParserAction;
        Compiler.Parser.TokenReceived +=  Parser_TokenReceived; 
        _tokens.Clear();
      }
      try {
        SourceFile source = new SourceFile(txtSource.Text, "source", 8);
        rootNode = Compiler.Parse(txtSource.Text);
      } catch (Exception ex) {
        lstErrors.Items.Add(ex);
        lstErrors.Items.Add("Parser state: " + Compiler.Parser.CurrentState);
        tabResults.SelectedTab = pageParseErrors;
        ShowParseStack();
        throw;
      } finally {
        Compiler.Parser.ActionSelected -= Parser_ParserAction;
        Compiler.Parser.TokenReceived -= Parser_TokenReceived;
      }
      if (Compiler.Context.Errors.Count > 0) {
        if (tabResults.SelectedTab == pageResult)
          tabResults.SelectedTab = pageParseErrors;
        foreach (SyntaxError err in Compiler.Context.Errors) 
          lstErrors.Items.Add(err);
      }
      if (chkShowTrace.Checked)
        foreach (Token tkn in _tokens)
          lstTokens.Items.Add(tkn);
      ShowStats();
      ShowAstNodes(rootNode);
    }

    private void ResetResultPanels() {
      _tokens.Clear();
      lblStatLines.Text = "compiling...";
      lblStatTokens.Text = "";
      lblStatTime.Text = "";
      lstTokens.Items.Clear();
      lstErrors.Items.Clear();
      lstParseTrace.Items.Clear();
      lstParseStack.Items.Clear();
      lstTokens.Items.Clear();
      tvAstNodes.Nodes.Clear();
      lblErrCount.Text = "";
      txtErrDetails.Text = "";
      Application.DoEvents();
    }
    private void ShowStats() {
      lblStatLines.Text = Compiler.Parser.LineCount.ToString();
      lblStatTokens.Text = Compiler.Parser.TokenCount.ToString();
      lblStatTime.Text = Compiler.CompileTime.ToString();
      lblErrCount.Text = Compiler.Context.Errors.Count.ToString();
      Application.DoEvents();
      //Note: this time is "pure" compile time; actual delay after cliking "Compile" includes time to fill TreeView control 
      //  showing compiled syntax tree. 
    }

    private TokenList _tokens = new TokenList();
    void Parser_TokenReceived(object sender, TokenEventArgs e) {
      _tokens.Add(e.Token);
    }


    void Parser_ParserAction(object sender, ParserActionEventArgs e) {
      lstParseTrace.Items.Add(e.ToString());
    }

    private void ShowParseStack() {
      lstParseStack.Items.Clear();
      for (int i = 0; i < Compiler.Parser.Stack.Count; i++ ) {
        lstParseStack.Items.Add(Compiler.Parser.Stack[i]);
      }
    }

    private void ShowAstNodes(AstNode rootNode) {
      tvAstNodes.Nodes.Clear();
      AddAstNode(null, rootNode);
    }
    private void AddAstNode(TreeNode parent, AstNode node) {
      if (node == null) return;
      if (node is Token) {
        Token token = node as Token;
        if (token.Terminal.Category != TokenCategory.Content) return; 
      }
      string txt = node.ToString();
      TreeNode tn = (parent == null? 
        tvAstNodes.Nodes.Add(txt) : parent.Nodes.Add(txt) );
      tn.Tag = node; 
      GenericNode genNode = node as GenericNode;
      if (genNode == null) return;
      foreach(AstNode child in genNode.ChildNodes)
        AddAstNode(tn, child);
     
    }
    private void RefreshGrammarInfo() {
      lstProds.Items.Clear();
      txtLR0Items.Text = "";
      txtParserStates.Text = "" ;
      txtGrammarErrors.Text = "(no errors found)";
      if (Compiler == null) return;
      GrammarData data = Compiler.Parser.Data;
      txtTerms.Text = TerminalsToText(data.Terminals);
      txtNonTerms.Text = NonTerminalsToText(data.NonTerminals);
      //Productions and LR0 items
      foreach (Production pr in data.Productions) {
        lstProds.Items.Add(pr);
      }
      //States
      txtParserStates.Text = StatesToText(data.States);
      //Validation errors
      if (data.Errors.Count > 0)
        txtGrammarErrors.Text = data.Errors.ToString(Environment.NewLine);
      lblInitTime.Text = Compiler.InitTime.ToString();
    }//methold

    private void lstProds_SelectedIndexChanged(object sender, EventArgs e) {
      Production pr = (Production) lstProds.SelectedItem;
      if (pr == null) return;
      string text = string.Empty;
      string nl = Environment.NewLine;
      foreach (LR0Item item in pr.LR0Items) {
        text += item.ToString() + nl + 
          "    Tail-Nullable = " + item.TailIsNullable + nl +
          "    Tail-Firsts   = " + item.TailFirsts.ToString(" ") + nl;
      }
      txtLR0Items.Text = text;
    }
    
    public string LoadFile(string fileName) {
      StreamReader rdr = new StreamReader(fileName);
      string result = rdr.ReadToEnd();
      return result;
    }

    private void lstErrors_Click(object sender, EventArgs e) {
      txtErrDetails.Text = "";
      if (lstErrors.SelectedItem == null) return;
      SyntaxError se = lstErrors.SelectedItem as SyntaxError;
      if (se == null) return;
      ShowLocation(se.Location, 1);
      txtErrDetails.Text = se.Message + "\r\n (L:C = " + se.Location + ", parser state: " + se.State + ")";
    }

    private void lstTokens_Click(object sender, EventArgs e) {
      ShowSelectedToken();
    }
    private void ShowSelectedToken() {
      if (lstTokens.SelectedIndex < 0)
        return;
      Token token = (Token) lstTokens.SelectedItem;
      ShowLocation(token.Location, token.Length);
    }
    private void ShowLocation(SourceLocation location, int length) {
      if (location.Position < 0) return;
      txtSource.Select(location.Position, length);
      txtSource.ScrollToCaret();
      //lblLoc.Text = location.ToString();
    }

    private void lstTokens_SelectedIndexChanged(object sender, EventArgs e) {
      ShowSelectedToken();
    }

    private void ShowNode(TreeNode node) {
      if (node == null) return;
      AstNode ast = node.Tag as AstNode;
      if (ast == null) return;
      ShowLocation(ast.Location, 1); 
    }

    private void tvAstNodes_AfterSelect(object sender, TreeViewEventArgs e) {
      ShowNode(tvAstNodes.SelectedNode);
    }

    private void cboLanguage_SelectedIndexChanged(object sender, EventArgs e) {
      Grammar grammar = null; 
      switch (cboLanguage.SelectedIndex) {
        case 0: //ExpressionGrammar
          grammar = new Irony.Samples.ExpressionGrammar();
          break;
        case 1: //Scheme
          grammar = new Irony.Samples.Scheme.SchemeGrammar();
          break;
        case 2: //Python
          grammar = new Irony.Samples.Python.PythonGrammar();
          break;
        case 3: //Ruby
          grammar = new Irony.Samples.Ruby.RubyGrammar();
          break;
      }//switch
      try {
        _compiler = new LanguageCompiler(grammar);
      } finally {
      RefreshGrammarInfo();
      }//finally
    }

    private void btnFileOpen_Click(object sender, EventArgs e) {
      if (dlgOpenFile.ShowDialog() != DialogResult.OK) return;
      LoadSourceFile(dlgOpenFile.FileName);
      
    }
    private void LoadSourceFile(string path) {
      try {
        StreamReader rdr = new StreamReader(path);
        string src = rdr.ReadToEnd();
        string[] lines = src.Split(new string[] { "\r\n" }, StringSplitOptions.None);
        txtSource.Lines = lines;
        txtSource.Select(0, 0);
      } catch (Exception e) {
        MessageBox.Show(e.Message);
      }
    }

    #region utilites: TerminalsToText, NonTerminalsToText
    public string TerminalsToText(TerminalList terminals) {
      StringBuilder sb = new StringBuilder();
      foreach (Terminal term in terminals) {
        sb.Append(term.ToString());
        sb.AppendLine();
      }
      return sb.ToString();
    }
    public string NonTerminalsToText(NonTerminalList nonTerminals) {
      StringBuilder sb = new StringBuilder();
      foreach (NonTerminal nt in nonTerminals) {
        sb.Append(nt.Name);
        sb.Append(nt.Nullable ? "  (Nullable) " : "");
        sb.AppendLine();
        foreach (Production pr in nt.Productions) {
          sb.Append("   ");
          sb.AppendLine(pr.ToString());
        }
        sb.Append("  FIRSTS: ");
        sb.AppendLine(nt.Firsts.ToString(" "));
        sb.AppendLine();
      }//foreachc nt
      return sb.ToString();
    }
    public string StatesToText(ParserStateList states) {
      StringBuilder sb = new StringBuilder();
      foreach (ParserState state in states) {
        sb.Append("State ");
        sb.AppendLine(state.Name);
        //LRItems
        foreach (LRItem item in state.Items) {
          sb.Append("    ");
          sb.AppendLine(item.ToString());
        }
        //Transitions
        sb.Append("     Transitions: ");
        foreach (string key in state.Actions.Keys) {
          ActionRecord action = state.Actions[key];
          if (action.NewState == null) continue;
          sb.Append(key);
          sb.Append("->");
          sb.Append(action.NewState.Name);
          sb.Append("; ");
        }
        sb.AppendLine();
      }//foreach
      return sb.ToString();
    }
    #endregion

    private void lstErrors_SelectedIndexChanged(object sender, EventArgs e) {

    }




  }//class
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Roman Ivantsov
Architect Pulsar Informatics, Inc
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 4 Jan 2008
Article Copyright 2008 by Roman Ivantsov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid