diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 2f7f14e..d75a4ae 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -28,7 +28,7 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); + System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle(); this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage_SourceFile = new System.Windows.Forms.TabPage(); this.groupBox1 = new System.Windows.Forms.GroupBox(); @@ -101,6 +101,7 @@ private void InitializeComponent() // // textBox_FileViewer // + this.textBox_FileViewer.Cursor = System.Windows.Forms.Cursors.Default; this.textBox_FileViewer.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(254))); this.textBox_FileViewer.Location = new System.Drawing.Point(6, 46); this.textBox_FileViewer.Multiline = true; @@ -108,7 +109,7 @@ private void InitializeComponent() this.textBox_FileViewer.ReadOnly = true; this.textBox_FileViewer.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox_FileViewer.Size = new System.Drawing.Size(748, 369); - this.textBox_FileViewer.TabIndex = 2; + this.textBox_FileViewer.TabIndex = 3; // // button_openFile // @@ -126,7 +127,7 @@ private void InitializeComponent() this.textBox_FilePath.Location = new System.Drawing.Point(6, 19); this.textBox_FilePath.Name = "textBox_FilePath"; this.textBox_FilePath.Size = new System.Drawing.Size(613, 20); - this.textBox_FilePath.TabIndex = 0; + this.textBox_FilePath.TabIndex = 2; this.textBox_FilePath.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox_FilePath_KeyPress); // // tabPage_LexicalTable @@ -162,14 +163,14 @@ private void InitializeComponent() this.Number, this.Value, this.Type}); - dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; - dataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Window; - dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(254))); - dataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.ControlText; - dataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.ActiveCaption; - dataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.ControlText; - dataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.False; - this.dataGridView_table.DefaultCellStyle = dataGridViewCellStyle2; + dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft; + dataGridViewCellStyle6.BackColor = System.Drawing.SystemColors.Window; + dataGridViewCellStyle6.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(254))); + dataGridViewCellStyle6.ForeColor = System.Drawing.SystemColors.ControlText; + dataGridViewCellStyle6.SelectionBackColor = System.Drawing.SystemColors.ActiveCaption; + dataGridViewCellStyle6.SelectionForeColor = System.Drawing.SystemColors.ControlText; + dataGridViewCellStyle6.WrapMode = System.Windows.Forms.DataGridViewTriState.False; + this.dataGridView_table.DefaultCellStyle = dataGridViewCellStyle6; this.dataGridView_table.Location = new System.Drawing.Point(6, 19); this.dataGridView_table.MultiSelect = false; this.dataGridView_table.Name = "dataGridView_table"; @@ -234,18 +235,19 @@ private void InitializeComponent() this.button_RegenerateTreeView.Location = new System.Drawing.Point(343, 18); this.button_RegenerateTreeView.Name = "button_RegenerateTreeView"; this.button_RegenerateTreeView.Size = new System.Drawing.Size(129, 22); - this.button_RegenerateTreeView.TabIndex = 4; + this.button_RegenerateTreeView.TabIndex = 3; this.button_RegenerateTreeView.Text = "Regenerate tree"; this.button_RegenerateTreeView.UseVisualStyleBackColor = true; this.button_RegenerateTreeView.Click += new System.EventHandler(this.button_RegenerateTreeView_Click); // // button_ShowDeepestTreeView // + this.button_ShowDeepestTreeView.Enabled = false; this.button_ShowDeepestTreeView.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(254))); this.button_ShowDeepestTreeView.Location = new System.Drawing.Point(484, 18); this.button_ShowDeepestTreeView.Name = "button_ShowDeepestTreeView"; this.button_ShowDeepestTreeView.Size = new System.Drawing.Size(129, 22); - this.button_ShowDeepestTreeView.TabIndex = 3; + this.button_ShowDeepestTreeView.TabIndex = 2; this.button_ShowDeepestTreeView.Text = "Show deepest path"; this.button_ShowDeepestTreeView.UseVisualStyleBackColor = true; this.button_ShowDeepestTreeView.Click += new System.EventHandler(this.button_ShowDeepestTreeView_Click); @@ -256,7 +258,7 @@ private void InitializeComponent() this.button_ToggleTreeViewVisib.Location = new System.Drawing.Point(625, 18); this.button_ToggleTreeViewVisib.Name = "button_ToggleTreeViewVisib"; this.button_ToggleTreeViewVisib.Size = new System.Drawing.Size(129, 22); - this.button_ToggleTreeViewVisib.TabIndex = 2; + this.button_ToggleTreeViewVisib.TabIndex = 1; this.button_ToggleTreeViewVisib.Text = "Toggle Visibility"; this.button_ToggleTreeViewVisib.UseVisualStyleBackColor = true; this.button_ToggleTreeViewVisib.Click += new System.EventHandler(this.button_ToggleTreeViewVisib_Click); @@ -267,7 +269,7 @@ private void InitializeComponent() this.SyntaxTreeView.Location = new System.Drawing.Point(6, 46); this.SyntaxTreeView.Name = "SyntaxTreeView"; this.SyntaxTreeView.Size = new System.Drawing.Size(748, 369); - this.SyntaxTreeView.TabIndex = 0; + this.SyntaxTreeView.TabIndex = 4; // // tabPage_CodeGeneration // @@ -293,6 +295,7 @@ private void InitializeComponent() // // button_SimpleButton // + this.button_SimpleButton.Enabled = false; this.button_SimpleButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(254))); this.button_SimpleButton.Location = new System.Drawing.Point(484, 18); this.button_SimpleButton.Name = "button_SimpleButton"; @@ -303,6 +306,7 @@ private void InitializeComponent() // // button_AnotherButton // + this.button_AnotherButton.Enabled = false; this.button_AnotherButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(254))); this.button_AnotherButton.Location = new System.Drawing.Point(625, 18); this.button_AnotherButton.Name = "button_AnotherButton"; diff --git a/MainForm.cs b/MainForm.cs index e7572c0..2a2bb8c 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -10,10 +10,8 @@ namespace LexicalAnalyzer { public partial class MainForm : Form { - private const bool DEV_MODE = true; + private const bool DEV_MODE = false; private bool syntaxTreeIsExpanded = false; - - private ArrayList list = new ArrayList(); public MainForm() { @@ -21,16 +19,28 @@ public MainForm() if (DEV_MODE) { - textBox_FilePath.Text = "X:\\Dev\\Projects\\GUMRF\\LexicalAnalyzer\\Tests\\Normal code Simple.txt"; - if (File.Exists(textBox_FilePath.Text)) + try { - using (StreamReader reader = new StreamReader(textBox_FilePath.Text)) + textBox_FilePath.Text = "X:\\Dev\\Projects\\GUMRF\\LexicalAnalyzer\\Samples\\Sample 3.txt"; + 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(); + fillTabsContent(); + } + else throw new FileNotFoundException($"The following file was not found: \n{textBox_FilePath.Text}!"); + } + catch (FileNotFoundException fileNotFound) + { + MessageBox.Show(fileNotFound.Message, "File not found!", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + catch (Exception exception) + { + MessageBox.Show(exception.Message, this.GetType().Name, MessageBoxButtons.OK); } } } @@ -63,7 +73,6 @@ private void button_openFile_Click(object sender, EventArgs e) catch (FileNotFoundException fileNotFound) { MessageBox.Show(fileNotFound.Message, "File not found!", MessageBoxButtons.OK); - throw; } catch (Exception exception) { @@ -95,8 +104,7 @@ private void button_ShowDeepestTreeView_Click(object sender, EventArgs e) { try { - GetDeeperLevel(SyntaxTreeView?.Nodes[0]); - list.Sort(); + throw new NotImplementedException(); } catch (Exception exception) { @@ -104,15 +112,6 @@ private void button_ShowDeepestTreeView_Click(object sender, EventArgs e) } } - 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)) @@ -153,6 +152,7 @@ private void fillTabsContent() // Build a syntax tree parser.GenerateAbstractSyntaxTree(SyntaxTreeView, lexicList); + syntaxTreeIsExpanded = false; } private void button_RegenerateTreeView_Click(object sender, EventArgs e) @@ -166,8 +166,7 @@ private void button_RegenerateTreeView_Click(object sender, EventArgs e) } fillTabsContent(); - SyntaxTreeView?.ExpandAll(); - syntaxTreeIsExpanded = true; + button_ToggleTreeViewVisib_Click(sender, e); } } diff --git a/README.md b/README.md index 0c4caff..5c51ae6 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,40 @@ # Lexical analyzer -The program performs lexical analysis of a program written in the Pascal programming language. +The program implements lexical and syntactic parsers. The program is written for code that is very similar to Pascal (PascalABC). -![OnStartupPic](/readme/LexicalAnalyzer_OnStartup.png?raw=true) +### Implemented features +- Lexical analyzer +- Syntax analyzer +- Basic error search (Unknown type/value) -![ReadsFilePic](/readme/LexicalAnalyzer_ReadsFile.png?raw=true) - -![LexicalTablePic](/readme/LexicalAnalyzer_LexicalTable.png?raw=true) - -![ErrorExamplePic](/readme/LexicalAnalyzer_ErrorExample.png?raw=true) +### Implemented lexems +- Variable ("true" and "true1" are different) +- States (true, false) +- Separators (';' does not equal ':') ## Not implemented: +- Search for errors in the logic or spelling of lexemes - Floating point number detection -- Arrays[] -- ... -- A lot of other things. \ No newline at end of file +- Arrays [] +- Loops (for, while, etc) +- A lot of other things. + +## Screenshots +![Program startup](/readme/0_Startup.png) + +![Source file example](/readme/1_SourceFileExample.png) + +![Lexical table example](/readme/2_LexicalTableExample.png) + +![Syntax tree example](/readme/3_SyntaxTreeExample.png) + +![Error example](/readme/4_ErrorExample.png) + +This abstract syntax tree is based on the file ```\Samples\Sample 2.txt``` +![Abstract syntax tree for Sample code #1](/readme/_AbstractSyntaxTree.png) + + +## License + +[MIT](https://choosealicense.com/licenses/mit/) + diff --git a/Tests/Without 'end.'.txt b/Samples/Sample 1.txt similarity index 100% rename from Tests/Without 'end.'.txt rename to Samples/Sample 1.txt diff --git a/Tests/Normal code Simple.txt b/Samples/Sample 2.txt similarity index 100% rename from Tests/Normal code Simple.txt rename to Samples/Sample 2.txt diff --git a/Tests/Normal code Simple 2.txt b/Samples/Sample 3.txt similarity index 100% rename from Tests/Normal code Simple 2.txt rename to Samples/Sample 3.txt diff --git a/Tests/Normal code.txt b/Samples/Sample with errors.txt similarity index 100% rename from Tests/Normal code.txt rename to Samples/Sample with errors.txt diff --git a/Tests/AST.png b/Tests/AST.png deleted file mode 100644 index fd5df78..0000000 Binary files a/Tests/AST.png and /dev/null differ diff --git a/Tests/Normal code - Binary.txt b/Tests/Normal code - Binary.txt deleted file mode 100644 index 12be583..0000000 --- a/Tests/Normal code - Binary.txt +++ /dev/null @@ -1,12 +0,0 @@ -program bool; - var x, y: boolean; -begin - x := true; - y := x and true; - y := false1; - y := true | false; - if x = true then - writeln(x); - -end. - \ No newline at end of file diff --git a/Tests/PriorityTable.drawio.png b/Tests/PriorityTable.drawio.png deleted file mode 100644 index a6796e6..0000000 Binary files a/Tests/PriorityTable.drawio.png and /dev/null differ diff --git a/readme/0_Startup.png b/readme/0_Startup.png new file mode 100644 index 0000000..8ccb58b Binary files /dev/null and b/readme/0_Startup.png differ diff --git a/readme/1_SourceFileExample.png b/readme/1_SourceFileExample.png new file mode 100644 index 0000000..fa75e94 Binary files /dev/null and b/readme/1_SourceFileExample.png differ diff --git a/readme/2_LexicalTableExample.png b/readme/2_LexicalTableExample.png new file mode 100644 index 0000000..f80a449 Binary files /dev/null and b/readme/2_LexicalTableExample.png differ diff --git a/readme/3_SyntaxTreeExample.png b/readme/3_SyntaxTreeExample.png new file mode 100644 index 0000000..5c35b89 Binary files /dev/null and b/readme/3_SyntaxTreeExample.png differ diff --git a/readme/4_ErrorExample.png b/readme/4_ErrorExample.png new file mode 100644 index 0000000..afe6241 Binary files /dev/null and b/readme/4_ErrorExample.png differ diff --git a/readme/LexicalAnalyzer_ErrorExample.png b/readme/LexicalAnalyzer_ErrorExample.png deleted file mode 100644 index 2e4b58e..0000000 Binary files a/readme/LexicalAnalyzer_ErrorExample.png and /dev/null differ diff --git a/readme/LexicalAnalyzer_LexicalTable.png b/readme/LexicalAnalyzer_LexicalTable.png deleted file mode 100644 index aafbb9c..0000000 Binary files a/readme/LexicalAnalyzer_LexicalTable.png and /dev/null differ diff --git a/readme/LexicalAnalyzer_OnStartup.png b/readme/LexicalAnalyzer_OnStartup.png deleted file mode 100644 index bb42217..0000000 Binary files a/readme/LexicalAnalyzer_OnStartup.png and /dev/null differ diff --git a/readme/LexicalAnalyzer_ReadsFile.png b/readme/LexicalAnalyzer_ReadsFile.png deleted file mode 100644 index d129ce0..0000000 Binary files a/readme/LexicalAnalyzer_ReadsFile.png and /dev/null differ diff --git a/Tests/AST.drawio.png b/readme/_AbstractSyntaxTree.png similarity index 100% rename from Tests/AST.drawio.png rename to readme/_AbstractSyntaxTree.png