Skip to content

Commit

Permalink
Merge pull request #2 from Denis-Olejnik/dev
Browse files Browse the repository at this point in the history
Lexical & syntax (AST) analyzers is released!
  • Loading branch information
DenisOlejnik authored Jan 5, 2023
2 parents b1af931 + 41e8f94 commit 5da8a10
Show file tree
Hide file tree
Showing 16 changed files with 636 additions and 113 deletions.
4 changes: 3 additions & 1 deletion LexicalAnalyzer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Source\Analyzer.cs" />
<Compile Include="Source\Lexer.cs" />
<Compile Include="Source\Lex.cs" />
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
Expand All @@ -56,6 +56,8 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Source\Parser.cs" />
<Compile Include="Source\ParserRules.cs" />
<EmbeddedResource Include="MainForm.resx">
<DependentUpon>MainForm.cs</DependentUpon>
</EmbeddedResource>
Expand Down
212 changes: 179 additions & 33 deletions MainForm.Designer.cs

Large diffs are not rendered by default.

122 changes: 107 additions & 15 deletions MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using LexicalAnalyzer.LexicalAnalyzer.Source;
using LexicalAnalyzer.Source;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
Expand All @@ -8,9 +10,29 @@ namespace LexicalAnalyzer
{
public partial class MainForm : Form
{
private const bool DEV_MODE = true;
private bool syntaxTreeIsExpanded = false;

private ArrayList list = new ArrayList();

public MainForm()
{
InitializeComponent();

if (DEV_MODE)
{
textBox_FilePath.Text = "X:\\Dev\\Projects\\GUMRF\\LexicalAnalyzer\\Tests\\Normal code Simple.txt";
if (File.Exists(textBox_FilePath.Text))
{
using (StreamReader reader = new StreamReader(textBox_FilePath.Text))
{
string fileContent = reader.ReadToEnd();
textBox_FileViewer.Text = fileContent;
}

fillTabsContent();
}
}
}

private void button_openFile_Click(object sender, EventArgs e)
Expand All @@ -34,7 +56,7 @@ private void button_openFile_Click(object sender, EventArgs e)
textBox_FilePath.Text = filePath;
textBox_FileViewer.Text = fileContent;
}
addListLexToDataTableView();
fillTabsContent();
}
}
}
Expand All @@ -45,39 +67,109 @@ private void button_openFile_Click(object sender, EventArgs e)
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Unexpected exception", MessageBoxButtons.OK);
throw;
MessageBox.Show(exception.Message, this.GetType().Name, MessageBoxButtons.OK);
}
}

private void button_ToggleTreeViewVisib_Click(object sender, EventArgs e)
{
try
{
if (syntaxTreeIsExpanded)
{
SyntaxTreeView?.CollapseAll();
}
else
{
SyntaxTreeView?.ExpandAll();
}
syntaxTreeIsExpanded = !syntaxTreeIsExpanded;
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, this.GetType().Name, MessageBoxButtons.OK);
}
}

private void button_ShowDeepestTreeView_Click(object sender, EventArgs e)
{
try
{
GetDeeperLevel(SyntaxTreeView?.Nodes[0]);
list.Sort();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, this.GetType().Name, MessageBoxButtons.OK);
}
}

private void GetDeeperLevel(TreeNode node)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
GetDeeperLevel(node.Nodes[i]);
list.Add(node.Nodes[i].Level);
}
}

private void textBox_FilePath_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (Int32)(Keys.Enter))
{
if (File.Exists(textBox_FilePath.Text))
try
{
using (StreamReader reader = new StreamReader(textBox_FilePath.Text))
if (File.Exists(textBox_FilePath.Text))
{
string fileContent = reader.ReadToEnd();
textBox_FileViewer.Text = fileContent;
using (StreamReader reader = new StreamReader(textBox_FilePath.Text))
{
string fileContent = reader.ReadToEnd();
textBox_FileViewer.Text = fileContent;
}
fillTabsContent();
}
addListLexToDataTableView();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, this.GetType().Name, MessageBoxButtons.OK);
throw;
}
}
}

private void addListLexToDataTableView()
private void fillTabsContent()
{
dataGridView_table.Rows.Clear();

// Transferring data to the method and then analyzing it
Analyzer lexicAnalyzer = new Analyzer();
List<Lex> lexicList = lexicAnalyzer.getLexemesList(textBox_FileViewer.Text);
Lexer lexicalAnalyzer = new Lexer();
Parser parser = new Parser();

// Fill in the table of lexemes
dataGridView_table?.Rows.Clear();
List<Lex> lexicList = lexicalAnalyzer.getLexemesList(textBox_FileViewer.Text);

for (int i = 0; i < lexicList.Count; i++)
{
dataGridView_table.Rows.Add(i, lexicList[i].lexemWord, lexicList[i].lexemType);
dataGridView_table.Rows.Add(i + 1, lexicList[i].word, lexicList[i].type);
}

// Build a syntax tree
parser.GenerateAbstractSyntaxTree(SyntaxTreeView, lexicList);
}

private void button_RegenerateTreeView_Click(object sender, EventArgs e)
{
if (File.Exists(textBox_FilePath.Text))
{
using (StreamReader reader = new StreamReader(textBox_FilePath.Text))
{
string fileContent = reader.ReadToEnd();
textBox_FileViewer.Text = fileContent;
}

fillTabsContent();
SyntaxTreeView?.ExpandAll();
syntaxTreeIsExpanded = true;
}

}
}
}
4 changes: 2 additions & 2 deletions MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@
<metadata name="Number.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Type.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<metadata name="Value.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Value.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<metadata name="Type.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
</root>
47 changes: 33 additions & 14 deletions Source/Lex.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LexicalAnalyzer.LexicalAnalyzer.Source
namespace LexicalAnalyzer.LexicalAnalyzer.Source
{
// TODO: Replace text type with enum?
// This may be more convenient than the current implementation.
// Address/search by type (.Semicolon, ,Variable) , but display static name in DataGridTable.



public class Lex
{
public readonly string lexemType;
public readonly string lexemWord;
public Lex(string lexemType, string lexemWord)
public readonly Type type;
public readonly string word;

public enum Type
{
this.lexemType = lexemType;
this.lexemWord = lexemWord;
Assign, // :=
Colon, // :
Comment_Open, // {
Comment_Close, // }
End, // .
Logical_AND, // and
Logical_NOT, // not
Logical_OR, // or
Logical_XOR, // xor
Parenthesis, // ()
Semicolon, // ;
Condition, // true/false
Constant, // const
Variable, // variable
Null, // null-value
}

public Lex(Type _lexemType, string _lexemWord)
{
this.type = _lexemType;
this.word = _lexemWord;
}

}
}
Loading

0 comments on commit 5da8a10

Please sign in to comment.