languages_and_translators/final_lang/lex_yacc.py

367 lines
6.3 KiB
Python
Raw Normal View History

2019-03-21 14:52:07 -06:00
import ply.lex as lex
2019-03-21 18:38:52 -06:00
import ply.yacc as yacc
tokens = [
'doubleColon',
'coma',
'openBra',
'closedBra',
'int',
'rea',
'parens',
'openParen',
'closedParen',
'string',
'plus',
'minus',
'mul',
'div',
'or',
'and',
'not',
'equals',
'doubleEquals',
'notEquals',
'less',
'more',
'lessEquals',
'moreEquals',
2019-03-21 21:09:36 -06:00
'id',
2019-03-21 21:39:07 -06:00
'program',
'end',
'read',
'print',
'if',
'then',
'else',
'elif',
'do',
'swap',
'exit',
'integer',
'real',
'subroutine',
2019-03-21 20:20:55 -06:00
]
2019-03-21 21:39:07 -06:00
reserved = {
'program' : 'program',
'end' : 'end',
'read' : 'read',
'print' : 'print',
'if' : 'if',
'then' : 'then',
'else' : 'else',
'elif' : 'elif',
'do' : 'do',
'swap' : 'swap',
'exit' : 'exit',
'integer' : 'integer',
'real' : 'real',
'subroutine' : 'subroutine',
}
2019-03-21 20:20:55 -06:00
2019-03-21 21:09:36 -06:00
t_doubleColon = r'::'
t_coma = r','
t_openBra = r'\['
t_closedBra = r'\]'
t_parens = r'\(\)'
t_openParen = r'\('
t_closedParen = r'\)'
2019-03-21 20:20:55 -06:00
t_plus = r'\+'
t_minus = r'-'
t_mul = r'\*'
2019-03-21 22:59:22 -06:00
t_string = r'\'[a-zA-Z0-9 \t\r\n\f()\[\]\&\!\@\#\$\%\^\-\=\+\/\,]*\''
2019-03-21 21:39:07 -06:00
t_or = r'\.or\.'
t_and = r'\.and\.'
t_not = r'\.not\.'
t_doubleEquals = r'\=\='
t_equals = r'\='
t_notEquals = r'\/\='
t_div = r'\/'
t_lessEquals = r'\<\='
t_less = r'\<'
t_moreEquals = r'\>\='
t_more = r'\>'
t_ignore = ' \t\r\n\f\v'
2019-03-21 20:20:55 -06:00
def t_int(t):
r'\d+'
t.value = int(t.value)
return t
def t_rea(t):
r'\d+\.\d+'
t.value = float(t.value)
return t
def t_id(t):
2019-03-21 21:09:36 -06:00
r'[a-zA-Z_][a-zA-Z_0-9]*'
2019-03-21 21:39:07 -06:00
if t.value in reserved:
t.type = reserved[ t.value ]
else:
t.type = 'id'
2019-03-21 20:20:55 -06:00
return t
def t_error(t):
print("Illegal character!")
2019-03-21 22:59:22 -06:00
print(t)
2019-03-21 20:20:55 -06:00
t.lexer.skip(1)
lexer = lex.lex()
def p_programa(p):
'''
programa : program id V F B end program
'''
print('+++ Valid program')
2019-03-21 20:20:55 -06:00
def p_V(p):
'''
V : V Tipo Dim doubleColon Rid
|
'''
def p_Rid(p):
'''
Rid : id
| Rid coma id
'''
def p_Tipo(p):
'''
Tipo : integer
| real
'''
def p_Dim(p):
'''
Dim : openBra int closedBra
| openBra int closedBra openBra int closedBra
|
'''
def p_F(p):
'''
F : F subroutine id B end subroutine
|
'''
def p_B(p):
'''
B : B S
|
'''
def p_S(p):
'''
S : Dimensional equals EA
2019-03-21 23:13:56 -06:00
| id parens
2019-03-21 20:20:55 -06:00
| read RDimensional
| print RDimOrString
| if Relif ElseOrEmpty end if
2019-03-21 23:13:56 -06:00
| do id equals EA coma EA IntOrEmpty then B end do
| do then B end do
2019-03-21 20:20:55 -06:00
| swap Dimensional coma Dimensional
| exit
'''
def p_Dimensional(p):
'''
Dimensional : id DimensionsOrEmpty
'''
def p_DimensionsOrEmpty(p):
'''
DimensionsOrEmpty : openParen EA ComaEAOrEmpty closedParen
|
'''
def p_ComaEAOrEmpty(p):
'''
ComaEAOrEmpty : coma EA
|
'''
def p_RDimensional(p):
'''
RDimensional : Dimensional
2019-03-21 21:09:36 -06:00
| RDimensional coma Dimensional
2019-03-21 20:20:55 -06:00
'''
def p_RDimOrString(p):
'''
RDimOrString : DimOrString
| RDimOrString coma DimOrString
'''
def p_DimOrString(p):
'''
DimOrString : Dimensional
| string
'''
def p_Relif(p):
'''
Relif : openParen EL closedParen then B
| Relif elif openParen EL closedParen then B
'''
def p_ElseOrEmpty(p):
'''
ElseOrEmpty : else B
|
'''
def p_IntOrEmpty(p):
'''
IntOrEmpty : coma int
|
'''
def p_EA(p):
'''
EA : MultDiv
| EA SumOrSub MultDiv
'''
def p_SumOrSub(p):
'''
SumOrSub : plus
| minus
'''
2019-03-21 21:09:36 -06:00
def p_MultDiv(p):
'''
MultDiv : EAParens
| MultDiv MDSymbols EAParens
'''
def p_MDSymbols(p):
'''
MDSymbols : mul
| div
'''
def p_EAParens(p):
'''
EAParens : EItem
| openParen EA closedParen
'''
def p_EL(p):
'''
EL : AND
| EL or AND
'''
def p_AND(p):
'''
AND : Equality
| AND and Equality
'''
def p_Equality(p):
'''
Equality : EItem EQSymbols EItem
| openParen EL closedParen
| not EL
'''
def p_EItem(p):
'''
EItem : Dimensional
| int
| rea
'''
def p_EQSymbols(p):
'''
EQSymbols : less
| more
| doubleEquals
| notEquals
| lessEquals
| moreEquals
'''
def p_error(p):
print('XXX Invalid program')
2019-03-21 22:59:22 -06:00
print(p)
2019-03-21 21:09:36 -06:00
parser = yacc.yacc()
fort_program = '''
2019-03-21 21:39:07 -06:00
program matrix
integer [100][100] :: matrix1, matrix2, resultMatrix
integer :: m1Rows, m1Columns, m2Rows, m2Columns, temp, temp2
subroutine sumMatrices
2019-03-21 23:13:56 -06:00
do temp = 1, m1Rows then
do temp2 = 1, m1Columns then
2019-03-21 21:39:07 -06:00
resultMatrix(temp,temp2) = matrix(temp,temp2) + matrix(temp,temp2)
2019-03-21 22:59:22 -06:00
resultMatrix(1,1) = 2
2019-03-21 21:39:07 -06:00
end do
end do
end subroutine
subroutine printResultMatrix
2019-03-21 23:13:56 -06:00
do temp = 1, m1Rows then
do temp2 = 1, m1Columns then
2019-03-21 22:59:22 -06:00
print resultMatrix(temp,temp2) , ' '
2019-03-21 21:39:07 -06:00
end do
2019-03-21 22:59:22 -06:00
print '\n'
2019-03-21 21:39:07 -06:00
end do
end subroutine
2019-03-21 22:59:22 -06:00
subroutine readMatrix1
2019-03-21 23:13:56 -06:00
do temp = 1, m1Rows then
do temp2 = 1, m1Columns then
2019-03-21 22:59:22 -06:00
print 'Enter value (', temp, ',', temp2, ') For matrix1\n'
read matrix1(temp,temp2)
end do
end do
end subroutine
subroutine readMatrix2
2019-03-21 23:13:56 -06:00
do temp = 1, m1Rows then
do temp2 = 1, m1Columns then
2019-03-21 22:59:22 -06:00
print 'Enter value (', temp, ',', temp2, ') For matrix2\n'
read matrix2(temp,temp2)
end do
end do
end subroutine
subroutine readM1Dimensions
print 'Enter the rows of the first matrix'
read m1Rows
print 'Enter the columns for the first matrix'
read m1Columns
end subroutine
subroutine readM2Dimensions
print 'Enter the rows of the second matrix'
read m2Rows
print 'Enter the columns for the second matrix'
read m2Columns
end subroutine
2019-03-21 23:13:56 -06:00
do then
readM1Dimensions()
readM2Dimensions()
2019-03-21 22:59:22 -06:00
if (m1Rows == m2Rows) then
exit
end if
end do
2019-03-21 23:13:56 -06:00
readMatrix1()
readMatrix2()
sumMatrices()
printResultMatrix()
2019-03-21 21:09:36 -06:00
end program
'''
2019-03-21 21:39:07 -06:00
parser.parse(fort_program)
2019-03-21 21:09:36 -06:00
2019-03-21 21:39:07 -06:00
#lexer.input(fort_program)
#while True:
# tok = lexer.token()
# if not tok:
# break
# print(tok)
2019-03-21 21:09:36 -06:00