Este es el código fuente del analizador léxico para Visual basic 6.00

En este caso este no es un analizador lexico del todo sino es un scaner para las expresiones.

Variable -> letra+
Numero -> Digito+ (. Digito+)?

Su uso es de la siguiente forma:

Se aclara que esta versión es basada en programación estructurada, existe una versión basada en objetos para Visual basic.


 Lex.init "Hola Mundo!"
 while (Lex.yylex()<>"") ' Es decir mientras haya Tokens en el Buffer de entrada haga
   MsgBox Lex.CurrentToken.yyText ' Me muestra la palabra encontrada
   MsgBox Lex.CurrentToken.yyTipo ' Me muestra el tipo de la palabra es decir
                                  ' variable o Número
 wend
 

Attribute VB_Name = "Lex"
Option Explicit
' Diciembre 15 de 2005
' Autor: Fabio Palmieri
'
' ESTA VERSIÓN HA SIDO ACTUALIZADA Y LA ESTOY BUSCANDO
'
'
' Este es un scanner o lexer o analixador lexico
' que hice pa visual basic, es ineficiente pero es muy divertido,
' si la van a usar no olviden darme los creditos
' Fabio Palmieri Villa
'
' Bueno este es el unico que me sirve en la actualidad
' Los otros los tendre en un par de meses
' :)
' fabioandresp@gmail.com

Public Type Token
    yyLength As Integer
    yyLineX As Integer
    yyLineY As Integer
    yyText As String
    yyTipo As String
End Type
 
Public CurrentToken As Token
 
Private counterX As Integer
Private counterY As Integer
Private Const Errors = 0
Private Const Aceptacion = 255
Private Const Inicial = 1
    Private yyText As String
    Private yyLength As String
Private Epsilon As Integer
Private yyTipo As String
Private Buffer As String
Private Referencia As Integer
Private Names(255) As String * 1
 
' Tabla me dice para donde voy
Private Tabla(255, 255) As Integer
 
' Tabla Ok me dice si el estado actual es de aceptacion o y me permite determinar
' Expresiones minimales
Private TablaOk(255, 255) As Boolean
Private Sub CreateStrings()
 
End Sub
Private Sub CreateIDExpresion()
    Dim Refer As Integer
    Dim Index As Integer
    Dim i As Integer
        Refer = 20
        For i = 0 To 255
            Tabla(Refer, i) = Errors
        Next
        For i = 65 To 65 + 25
            Tabla(Refer, Expresion(Chr(i + 32))) = Refer + 1
            TablaOk(Refer, Expresion(Chr(i + 32))) = True
 
            Tabla(Refer, Expresion(Chr(i))) = Refer + 1
            TablaOk(Refer, Expresion(Chr(i))) = True
        Next
        Tabla(Refer, Expresion("Ñ")) = Refer + 1
        Tabla(Refer, Expresion("ñ")) = Refer + 1
        Tabla(Refer, Expresion("_")) = Refer + 1
        Tabla(Refer, Expresion(" ")) = Aceptacion
        Tabla(Refer, 13) = Aceptacion
        TablaOk(Refer, Expresion("Ñ")) = True
        TablaOk(Refer, Expresion("ñ")) = True
        TablaOk(Refer, Expresion("_")) = True
        TablaOk(Refer, Expresion(" ")) = True
        TablaOk(Refer, 13) = True
        Refer = Refer + 1
        For i = 0 To 255
            Tabla(Refer, i) = Errors
        Next
        For i = 65 To 65 + 25
            Tabla(Refer, Expresion(Chr(i + 32))) = Refer
            TablaOk(Refer, Expresion(Chr(i + 32))) = True
            Tabla(Refer, Expresion(Chr(i))) = Refer
            TablaOk(Refer, Expresion(Chr(i))) = True
        Next
        Tabla(Refer, Expresion("Ñ")) = Refer + 1
        Tabla(Refer, Expresion("ñ")) = Refer + 1
        Tabla(Refer, Expresion("_")) = Refer + 1
        Tabla(Refer, Expresion(" ")) = Aceptacion
        Tabla(Refer, 13) = Aceptacion
        TablaOk(Refer, Expresion("Ñ")) = True
        TablaOk(Refer, Expresion("ñ")) = True
        TablaOk(Refer, Expresion("_")) = True
        TablaOk(Refer, Expresion(" ")) = True
        TablaOk(Refer, 13) = True
End Sub
Private Sub CreateTable()
 
     Dim Refer As Integer
    Dim Index As Integer
    Dim i As Integer
 
    Index = 48
 
    ' Los 10 primeros estados estan reservados para usos futuros

    ' Referenciamos las Cabeceras para referenciar a los tokens
    For i = 0 To 10
        Names(Index + i) = Chr(Index + i)
    Next
 
    ' Creamos la Tabla de Estados a partir de la Expresion
    ' d = digito*(.)digito*

    ' Asumimos que todos los estados son Errores
    Refer = 10
    For i = 0 To 255
        Tabla(Refer, i) = Errors
    Next
    ' Estado (1)
    ' (digito)*
    Tabla(Refer, Expresion("0")) = Refer
    Tabla(Refer, Expresion("1")) = Refer
    Tabla(Refer, Expresion("2")) = Refer
    Tabla(Refer, Expresion("3")) = Refer
    Tabla(Refer, Expresion("4")) = Refer
    Tabla(Refer, Expresion("5")) = Refer
    Tabla(Refer, Expresion("6")) = Refer
    Tabla(Refer, Expresion("7")) = Refer
    Tabla(Refer, Expresion("8")) = Refer
    Tabla(Refer, Expresion("9")) = Refer
    ' (.) Cambio de estados
    Tabla(Refer, Expresion(".")) = Refer + 1
    Tabla(Refer, Expresion(" ")) = Aceptacion
 
    TablaOk(Refer, Expresion("0")) = True
    TablaOk(Refer, Expresion("1")) = True
    TablaOk(Refer, Expresion("2")) = True
    TablaOk(Refer, Expresion("3")) = True
    TablaOk(Refer, Expresion("4")) = True
    TablaOk(Refer, Expresion("5")) = True
    TablaOk(Refer, Expresion("6")) = True
    TablaOk(Refer, Expresion("7")) = True
    TablaOk(Refer, Expresion("8")) = True
    TablaOk(Refer, Expresion("9")) = True
    ' (.) Cambio de estados
    TablaOk(Refer, Expresion(".")) = True
    TablaOk(Refer, Expresion(" ")) = True
    Refer = Refer + 1
    For i = 0 To 255
        Tabla(Refer, i) = Errors
    Next
    ' Estado (1)
    ' (digito)*
    Tabla(Refer, Expresion("0")) = Refer
    Tabla(Refer, Expresion("1")) = Refer
    Tabla(Refer, Expresion("2")) = Refer
    Tabla(Refer, Expresion("3")) = Refer
    Tabla(Refer, Expresion("4")) = Refer
    Tabla(Refer, Expresion("5")) = Refer
    Tabla(Refer, Expresion("6")) = Refer
    Tabla(Refer, Expresion("7")) = Refer
    Tabla(Refer, Expresion("8")) = Refer
    Tabla(Refer, Expresion("9")) = Refer
    ' (.) Cambio de estados
    Tabla(Refer, Expresion(" ")) = Aceptacion
 
    TablaOk(Refer, Expresion("0")) = True
    TablaOk(Refer, Expresion("1")) = True
    TablaOk(Refer, Expresion("2")) = True
    TablaOk(Refer, Expresion("3")) = True
    TablaOk(Refer, Expresion("4")) = True
    TablaOk(Refer, Expresion("5")) = True
    TablaOk(Refer, Expresion("6")) = True
    TablaOk(Refer, Expresion("7")) = True
    TablaOk(Refer, Expresion("8")) = True
    TablaOk(Refer, Expresion("9")) = True
    TablaOk(Refer, Expresion(".")) = True
    ' (.) Cambio de estados
    TablaOk(Refer, Expresion(" ")) = True
 
    Refer = 35
    For i = 0 To 255
        Tabla(Refer, i) = Errors
    Next
    ' Estado (1)
    ' (digito)*
    Tabla(Refer, Expresion("0")) = Refer + 1
    Tabla(Refer, Expresion("1")) = Refer + 1
    Tabla(Refer, Expresion("2")) = Refer + 1
    Tabla(Refer, Expresion("3")) = Refer + 1
    Tabla(Refer, Expresion("4")) = Refer + 1
    Tabla(Refer, Expresion("5")) = Refer + 1
    Tabla(Refer, Expresion("6")) = Refer + 1
    Tabla(Refer, Expresion("7")) = Refer + 1
    Tabla(Refer, Expresion("8")) = Refer + 1
    Tabla(Refer, Expresion("9")) = Refer + 1
    ' (.) Cambio de estados
    Tabla(Refer, Expresion(" ")) = Aceptacion
 
    TablaOk(Refer, Expresion("0")) = True
    TablaOk(Refer, Expresion("1")) = True
    TablaOk(Refer, Expresion("2")) = True
    TablaOk(Refer, Expresion("3")) = True
    TablaOk(Refer, Expresion("4")) = True
    TablaOk(Refer, Expresion("5")) = True
    TablaOk(Refer, Expresion("6")) = True
    TablaOk(Refer, Expresion("7")) = True
    TablaOk(Refer, Expresion("8")) = True
    TablaOk(Refer, Expresion("9")) = True
    TablaOk(Refer, Expresion(".")) = True
    ' (.) Cambio de estados
    TablaOk(Refer, Expresion(" ")) = True
 
    Refer = Refer + 1
    For i = 0 To 255
        Tabla(Refer, i) = Errors
    Next
    ' Estado (1)
    ' (digito)*
    Tabla(Refer, Expresion("0")) = Refer
    Tabla(Refer, Expresion("1")) = Refer
    Tabla(Refer, Expresion("2")) = Refer
    Tabla(Refer, Expresion("3")) = Refer
    Tabla(Refer, Expresion("4")) = Refer
    Tabla(Refer, Expresion("5")) = Refer
    Tabla(Refer, Expresion("6")) = Refer
    Tabla(Refer, Expresion("7")) = Refer
    Tabla(Refer, Expresion("8")) = Refer
    Tabla(Refer, Expresion("9")) = Refer
    ' (.) Cambio de estados
    Tabla(Refer, Expresion(" ")) = Aceptacion
 
    TablaOk(Refer, Expresion("0")) = True
    TablaOk(Refer, Expresion("1")) = True
    TablaOk(Refer, Expresion("2")) = True
    TablaOk(Refer, Expresion("3")) = True
    TablaOk(Refer, Expresion("4")) = True
    TablaOk(Refer, Expresion("5")) = True
    TablaOk(Refer, Expresion("6")) = True
    TablaOk(Refer, Expresion("7")) = True
    TablaOk(Refer, Expresion("8")) = True
    TablaOk(Refer, Expresion("9")) = True
    TablaOk(Refer, Expresion(".")) = True
    ' (.) Cambio de estados
    TablaOk(Refer, Expresion(" ")) = True
 
 
End Sub
Private Function Estado(a As Integer)
    Estado = Str(a)
End Function
Public Sub GetBuffer(myBuffer As String)
    Buffer = " " + myBuffer + "  "
End Sub
Public Sub Init(myBuffer As String)
    Epsilon = 254
    Referencia = 1
    counterX = 1
    counterY = 1
 
    Dim i As Integer
        i = 1
    Dim j As Integer
        j = 1
 
        While (i <= 255)
            While (j <= 255)
                TablaOk(i, j) = False
            j = j + 1
            Wend
        i = i + 1
        Wend
 
    ' Estados numericos
    ' (digito)*(.)?(digito)*
    CreateTable
 
    ' Estados ID Alpha
    ' (letra)+
    CreateIDExpresion
 
    ' Estados los chars's
    CreateStrings
    GetBuffer myBuffer
 
End Sub
 
Private Function yyEstado(a As Integer)
    Select Case a
        Case 0
         yyEstado = "Error"
        Case 1
         yyEstado = "Inicio"
        Case 255
         yyEstado = "Aceptacion"
        Case Else
         yyEstado = "Esperando..."
        End Select
End Function
Private Function GetToken()
  Dim Size As Integer
  Dim Estado As Integer
  Dim yyText As String
  Dim devolver As Boolean
  Dim nextStage As Integer
 
  Size = Len(Buffer)
  Estado = Inicial
 
  While (Referencia <= Size) 'And (Estado <> Errors Or Estado <> Aceptacion))
    counterX = counterX + 1
    ' ----------------------------------
    If yyToken() = Chr(13) Then
        counterX = 1
        counterY = counterY + 1
    End If
 
    If yyToken() = " " And Estado = Inicial Then
        ' Token Ignorado
        Referencia = Referencia + 1
    End If
 
            ' Si es un Token no Ignoradao haga
            ' ----------------------------------
            Select Case Estado
                Case Inicial
                    Select Case LCase$(yyToken())
                        Case """"
                            yyTipo = "Strings"
                            Estado = 30
 
                        Case "."
                            yyTipo = "Number"
                            Estado = 35
 
                        Case ".", ",", "#", "!", "¡", "¿", "?", "<", ">", "%", "&", "@", "~", ":", ";", "'"
                            yyTipo = "Simbolo"
                            GetToken = yyToken()
                            Referencia = Referencia + 1
                            Exit Function
 
                        Case "{", "}", "(", ")", "[", "]"
                            yyTipo = "Agrupador"
                            GetToken = yyToken()
                            Referencia = Referencia + 1
                            Exit Function
 
                        Case "+", "-", "*", "/", "^"
                            yyTipo = "Operador"
                            GetToken = yyToken()
                            Referencia = Referencia + 1
                            Exit Function
 
                        Case "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "."
                            yyTipo = "Numero"
                            Estado = 10
                        Case "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"
                              , "ñ", "o", "p" , "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "_"
                            yyTipo = "ID"
                            Estado = 20
                        Case Else
                            yyTipo = "Desconocido"
                    End Select
                Case Aceptacion
                            If devolver = True Then
                                yyText = Mid$(yyText, 1, Len(yyText) - 1)
                                GetToken = yyText
                                Exit Function
                            End If
                Case Errors
                        GetToken = yyText
                        Exit Function
                Case Else
            End Select
            ' ----------------------------------
            ' Fin Action Performed by no Ignorated Token

        Estado = Tabla(Estado, Expresion(yyToken()))
            nextStage = Tabla(Estado, Expresion(yyNext()))
 
            ' Reconocimiento Maximal
            If yyToken() <> " " Then
 
                If TablaOk(Estado, Expresion(yyToken())) = True        ' Esto va dentro de una .
                        And                                            ' misma linea de codigo
                  TablaOk(nextStage, Expresion(yyToken())) = True Then '
                    ' La expresion que viene se puede seguir usando.
                    ' Reconocimiento maxinal

                Else
                    If yyNext() = " " Or yyNext() = "" Then
                        ' Aqui Termina Normal Mente la Expresion
                        ' Con Episilon o Token Ignorado
                        ' Hacemos la variable Devolver = true
                         devolver = True
                    Else
                        ' Aqui se obtiene la Expresion sin error
                        yyText = yyText + yyToken()
                        Referencia = Referencia + 1
                        GetToken = yyText
                        Exit Function
                    End If
                End If
            End If
 
 
        yyText = yyText + yyToken()
        Referencia = Referencia + 1
 
 
    ' ----------------------------------
    ' Consumir del Buffer

  Wend
 
 
 
  GetToken = ""
  Exit Function
 
  'MsgBox yyText

End Function
Public Function Yylex()
    ' Esta funcione devuelve el string y agrega sus propiedades
        CurrentToken.yyText = GetToken()
        CurrentToken.yyLength = Len(yyText)
        CurrentToken.yyLineX = counterX
        CurrentToken.yyLineY = counterY
        CurrentToken.yyTipo = yyTipo
    Yylex = CurrentToken.yyText
End Function
Public Function Get_YyToken()
    Get_YyToken = Yylex
End Function
Public Function CounterTokens()
    Referencia = 1
    Dim a As String
    Dim ct As Long
    ct = 0
    Do
        a = Yylex
        ct = ct + 1
    Loop Until a = ""
    CounterTokens = ct
End Function
Public Sub ScanerIn(Buff As String, a As ListBox)
        Init Buff
        Dim b As String
        Do
            b = Yylex
            a.AddItem b
        Loop Until b = ""
End Sub
Private Sub Parser()
 
End Sub
'------------------------------------------------
'- Funciones
'------------------------------------------------
Private Function TokenIs(ByVal a As String)
    TokenIs = Mid$(Buffer, a, 1)
End Function
Private Function yyToken()
    yyToken = Mid$(Buffer, Referencia, 1)
End Function
Private Function yyNext()
    yyNext = ""
    On Error Resume Next
    yyNext = Mid$(Buffer, Referencia + 1, 1)
End Function
Private Function Expresion(ByVal a As String)
    Select Case a
    Case "", " "
        Expresion = 254
    Case Else
        Expresion = Asc(a)
    End Select
End Function
Private Function StageIs(a As Boolean)
If a = True Then
    StageIs = "Aceptacion"
Else
    StageIs = "Estado de Ciclo"
End If
End Function