How to Create a Syntax Checker in Visual Basic

Language
Introduction: This tutorial is on how to create a simple tool to check the amount of opening and closing braces in your script syntax. Steps of Creation: Step 1: First we want to create a form with: - Button1 - To begin the checking process - RichTextBox1 - To contain the source code Step 2: Next we want to go to the button1 click event and create two new integers, one to count the amount of opening braces and another to count the amount of closing braces.
  1. Dim openBraces As Integer = 0
  2. Dim closingBraces As Integer = 0
Step 3: Now we will iterate through each character in our script code contained in richtextbox1 and check if it is a opening or closing brace.
  1. For Each c As Char In RichTextBox1.Text
  2.     If (c = "{") Then openBraces += 1
  3.     If (c = "}") Then closingBraces += 1
  4. Next
Step 4: Finally we output the result...
  1. If (openBraces = closingBraces) Then
  2.         MsgBox("There are the same amount of opening and closing braces!")
  3.     ElseIf (openBraces > closingBraces) Then
  4.         MsgBox("There are too many opening braces!")
  5.     ElseIf (openBraces < closingBraces) Then
  6.         MsgBox("There are too many closing braces!")
  7. End If
Project Complete! That's it! Below is the full source code and download to the project files.
  1. Public Class Form1
  2.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  3.         Dim openBraces As Integer = 0
  4.         Dim closingBraces As Integer = 0
  5.         For Each c As Char In RichTextBox1.Text
  6.             If (c = "{") Then openBraces += 1
  7.             If (c = "}") Then closingBraces += 1
  8.         Next
  9.         If (openBraces = closingBraces) Then
  10.             MsgBox("There are the same amount of opening and closing braces!")
  11.         ElseIf (openBraces > closingBraces) Then
  12.             MsgBox("There are too many opening braces!")
  13.         ElseIf (openBraces < closingBraces) Then
  14.             MsgBox("There are too many closing braces!")
  15.         End If
  16.     End Sub
  17. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment