Created symbols tables
This commit is contained in:
parent
c4183d66d5
commit
5dfc8d2483
1 changed files with 24 additions and 1 deletions
|
|
@ -2,6 +2,15 @@ import ply.lex as lex
|
||||||
import ply.yacc as yacc
|
import ply.yacc as yacc
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
# Operations related to the table of symbols
|
||||||
|
symbolsNames = []
|
||||||
|
symbolsTypes = []
|
||||||
|
|
||||||
|
def addSymbol(name, symbolType):
|
||||||
|
symbolsNames.append(name)
|
||||||
|
symbolsTypes.append(symbolType)
|
||||||
|
|
||||||
|
|
||||||
tokens = [
|
tokens = [
|
||||||
'doubleColon',
|
'doubleColon',
|
||||||
'coma',
|
'coma',
|
||||||
|
|
@ -121,18 +130,32 @@ def p_V(p):
|
||||||
V : V Tipo Dim doubleColon Rid
|
V : V Tipo Dim doubleColon Rid
|
||||||
|
|
|
|
||||||
'''
|
'''
|
||||||
|
# Adds all the variables for this production into the
|
||||||
|
# symbols table.
|
||||||
|
if (len(p) > 1):
|
||||||
|
for name in p[5]:
|
||||||
|
addSymbol(name, p[2])
|
||||||
|
|
||||||
|
|
||||||
def p_Rid(p):
|
def p_Rid(p):
|
||||||
'''
|
'''
|
||||||
Rid : id
|
Rid : id
|
||||||
| Rid coma id
|
| Rid coma id
|
||||||
'''
|
'''
|
||||||
|
# p[0] will contain an list with all the variable
|
||||||
|
# names for this production in string format.
|
||||||
|
if (len(p) == 2):
|
||||||
|
p[0] = [p[1]]
|
||||||
|
else:
|
||||||
|
p[0] = p[1] + [p[3]]
|
||||||
|
|
||||||
def p_Tipo(p):
|
def p_Tipo(p):
|
||||||
'''
|
'''
|
||||||
Tipo : integer
|
Tipo : integer
|
||||||
| real
|
| real
|
||||||
'''
|
'''
|
||||||
|
# p[0] will contain the type in string format
|
||||||
|
p[0] = p[1]
|
||||||
|
|
||||||
def p_Dim(p):
|
def p_Dim(p):
|
||||||
'''
|
'''
|
||||||
|
|
@ -295,7 +318,7 @@ if (len(sys.argv) > 1):
|
||||||
programName = sys.argv[1]
|
programName = sys.argv[1]
|
||||||
programFile = open(programName, "r")
|
programFile = open(programName, "r")
|
||||||
# This is neccessary because the read method parses literal ends
|
# This is neccessary because the read method parses literal ends
|
||||||
# of line as \\n instead of \n.
|
# of lines as \\n instead of \n.
|
||||||
program = programFile.read().replace('\\n', '\n')
|
program = programFile.read().replace('\\n', '\n')
|
||||||
parser.parse(program)
|
parser.parse(program)
|
||||||
programFile.close()
|
programFile.close()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue