0% found this document useful (0 votes)
31 views5 pages

Codigo

The document is a Visual Basic program that implements a lexical and syntactic analyzer for mathematical expressions. It reads an input expression, identifies tokens such as operators, identifiers, and numbers, and checks for syntax errors. The program outputs recognized tokens, a symbol table, and validation results for the expression's syntax.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Codigo

The document is a Visual Basic program that implements a lexical and syntactic analyzer for mathematical expressions. It reads an input expression, identifies tokens such as operators, identifiers, and numbers, and checks for syntax errors. The program outputs recognized tokens, a symbol table, and validation results for the expression's syntax.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Module Module1

Sub Main()

[Link]("INGRESE UNA EXPRESION ...")


[Link](" ")
Dim myString As String = [Link]
[Link](" ")
Dim myChar As String
Dim TestLen As Integer = Len(myString)
Dim exp(TestLen) As String
Dim i As Integer
Dim J As Integer
[Link]("ANALIZADOR LEXICO: ")

[Link](" TOKENS RECONOCIDOS: ")


[Link](" ")
For i = 0 To (TestLen - 1)

myChar = [Link](i)

If myChar = "+" Or myChar = "-" Or myChar = "/" Or myChar = "*" Or


myChar = "=" Or myChar = "^" Then
exp(i) = "op"
[Link]("<" & exp(i) & "," & myChar & ">")

ElseIf myChar = "a" Or myChar = "b" Or myChar = "c" Or myChar = "d" Or


myChar = "e" Or myChar = "f" Or myChar = "g" Or myChar = "h" Or myChar = "i" Or
myChar = "j" Or myChar = "k" Or myChar = "l" Then
exp(i) = "exp"
[Link]("<" & exp(i) & "," & myChar & ">")

ElseIf myChar = "1" Or myChar = "2" Or myChar = "3" Or myChar = "4" Or


myChar = "5" Or myChar = "6" Or myChar = "7" Or myChar = "8" Or myChar = "9" Then
exp(i) = "num"
[Link]("<" & exp(i) & "," & myChar & ">")

ElseIf myChar = "(" Then


exp(i) = "par_d"
[Link]("<" & exp(i) & "," & myChar & ">")

ElseIf myChar = ")" Then


exp(i) = "par_iz"
[Link]("<" & exp(i) & "," & myChar & ">")

Else
[Link](myChar & " TOKEN NO RECONOCIDO ")
End If
Next
[Link](" ")
Dim z As Integer
J = 0
z = 1
[Link](" ")
[Link](" TABLA DE SIMBOLOS :")

For J = 0 To (TestLen - 1)

myChar = [Link](J)
If myChar = "+" Or myChar = "-" Or myChar = "/" Or myChar = "*" Or
myChar = "=" Or myChar = "^" Then
' [Link]("<" & myChar & ">")

ElseIf myChar = "a" Or myChar = "b" Or myChar = "c" Or myChar = "d" Or


myChar = "e" Or myChar = "f" Or myChar = "g" Or myChar = "h" Or myChar = "i" Or
myChar = "j" Or myChar = "k" Or myChar = "l" Then
[Link](" <id," & z & "> = " & myChar)
z = z + 1

ElseIf myChar = "1" Or myChar = "2" Or myChar = "3" Or myChar = "4"


Or myChar = "5" Or myChar = "6" Or myChar = "7" Or myChar = "8" Or myChar = "9"
Then
'[Link]("<" & myChar & ">")

ElseIf myChar = "(" Then


' [Link](myChar)

ElseIf myChar = ")" Then


'[Link](myChar)

Else
[Link](myChar & " ")
[Link](myChar & " TOKEN NO RECONOCIDO")
End If
Next
J = 0
z = 1
[Link](" ")
[Link](" EXPRESION ANALIZADA LEXCAMENTE :")
[Link](" ")

For J = 0 To (TestLen - 1)

myChar = [Link](J)

If myChar = "+" Or myChar = "-" Or myChar = "/" Or myChar = "*" Or


myChar = "=" Or myChar = "^" Then
[Link]("<" & myChar & ">")

ElseIf myChar = "a" Or myChar = "b" Or myChar = "c" Or myChar = "d" Or


myChar = "e" Or myChar = "f" Or myChar = "g" Or myChar = "h" Or myChar = "i" Or
myChar = "j" Or myChar = "k" Or myChar = "l" Then
[Link]("<id," & z & ">")
z = z + 1
ElseIf myChar = "1" Or myChar = "2" Or myChar = "3" Or myChar = "4" Or
myChar = "5" Or myChar = "6" Or myChar = "7" Or myChar = "8" Or myChar = "9" Then
[Link]("<" & myChar & ">")

ElseIf myChar = "(" Then


[Link](myChar)

ElseIf myChar = ")" Then


[Link](myChar)

Else
[Link](myChar & " TOKEN NO RECONOCIDO")
End If
Next

[Link](" ")
Dim g As Integer
Dim h As Integer
Dim e As Integer

i = 0
h = 1
e = 0
g = 0
[Link](" ")
[Link]("ANALIZADOR SINTACTICO: ")

'[Link](":::::TOKENS:::::")

For i = 0 To TestLen - 1

If i = 0 Then
If [Link](i) = "(" Then
g = g + 1

Else
If exp(i) = "exp" Or exp(i) = "num" Then

Else
If [Link](i) = "-" Then

Else
If [Link](i) = "+" Or [Link](i) = "*"
Or [Link](i) = "/" Or [Link](i) = "^" Or [Link](i) = "="
Then
[Link](" ERROR EN EL CARACTER " & i
& " :")
[Link](" no se puede escribir un
operador " & [Link](i) & " al comienzo de una expresion")
h = 0

End If
End If
End If
End If

Else
Select Case exp(i)

Case "exp"

If [Link](i - 1) = "(" Then

Else
If exp(i - 1) = "op" Then

Else
[Link](" ERROR EN EL CARACTER " & i
& " :")
[Link](" no se puede poner una
expresion despues de una expresion")
h = 0
End If
End If

Case "num"
If [Link](i - 1) = "(" Then

Else
If exp(i - 1) = "op" Then

Else
[Link](" ERROR EN EL CARACTER " & i
& " :")
[Link](" no se puede poner una
expresion despues de una expresion")
h = 0
End If
End If

Case "op"

If exp(i - 1) = "par_d" Or [Link](i - 1) = "=" Then

If [Link](i) = "-" Then

Else
[Link](" ERROR EN EL CARACTER " & i
& " :")
[Link](" no se puede escribir el
operador " & [Link](i) & " despues de un " & [Link](i - 1))
h = 0
End If
Else
If exp(i - 1) = "exp" Or exp(i - 1) = "num" Then

Else
If [Link](i - 1) = ")" Then

Else
[Link](" ERROR EN EL CARACTER "
& i & " :")
[Link](" no se puede poner un
operador depues de un operador")
h = 0

End If
End If
End If

Case "par_d"
g = g + 1

If [Link](i - 1) = "(" Then

Else
If exp(i - 1) = "op" Then

Else
[Link](" ERROR EN EL CARACTER " & i
& " :")
[Link](" no se puede poner una
parentesis cerrado despues de una parentesis abierto")
h = 0

End If

End If
Case "par_iz"

e = e + 1
If exp(i - 1) = "exp" Or exp(i - 1) = "num" Then

Else
If exp(i - 1) = "par_iz" Then

Else
If exp(i - 1) = "op" Then
[Link](" ERROR EN EL CARACTER "
& i & " :")
[Link](" no se puede poner un
parentesis abierto luego de un operador")
Else
[Link](" ERROR EN EL CARACTER "
& i & " :")
[Link](" no se puede poner dos
parentesis juntos sin ninguna expresion")
End If
h = 0

End If

End If
End Select

End If
Next

If h = 1 Then
[Link](" NO MUESTRA ERRORES")
End If
[Link](" ")
[Link](" RESULTADO DEL ANALIZADOR SINTACTICO:")
If h = 1 Then
If e = g Then
[Link](" ::LA EXPRESION ESTA BIEN ESCRITA")
Else
[Link](" ::LE FALTA UNA PARENTESIS")
End If
Else
[Link](" ::ERROR EN LA EXPRESION")
End If

Dim expresion As String = [Link]

End Sub
End Module

You might also like