Finished parser
This commit is contained in:
parent
d5760e38ea
commit
8fec222a89
2 changed files with 86 additions and 2110 deletions
|
|
@ -2,29 +2,15 @@ import ply.lex as lex
|
|||
import ply.yacc as yacc
|
||||
|
||||
tokens = [
|
||||
'program',
|
||||
'end',
|
||||
'doubleColon',
|
||||
'coma',
|
||||
'integer',
|
||||
'real',
|
||||
'openBra',
|
||||
'closedBra',
|
||||
'int',
|
||||
'rea',
|
||||
'subroutine',
|
||||
'parens',
|
||||
'openParen',
|
||||
'closedParen',
|
||||
'read',
|
||||
'print',
|
||||
'if',
|
||||
'then',
|
||||
'else',
|
||||
'elif',
|
||||
'do',
|
||||
'swap',
|
||||
'exit',
|
||||
'string',
|
||||
'plus',
|
||||
'minus',
|
||||
|
|
@ -41,22 +27,38 @@ tokens = [
|
|||
'lessEquals',
|
||||
'moreEquals',
|
||||
'id',
|
||||
'program',
|
||||
'end',
|
||||
'read',
|
||||
'print',
|
||||
'if',
|
||||
'then',
|
||||
'else',
|
||||
'elif',
|
||||
'do',
|
||||
'swap',
|
||||
'exit',
|
||||
'integer',
|
||||
'real',
|
||||
'subroutine',
|
||||
]
|
||||
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',
|
||||
}
|
||||
|
||||
t_program = r'program'
|
||||
t_end = r'end'
|
||||
t_read = r'read'
|
||||
t_print = r'print'
|
||||
t_if = r'if'
|
||||
t_then = r'then'
|
||||
t_else = r'else'
|
||||
t_elif = r'elif'
|
||||
t_do = r'do'
|
||||
t_swap = r'swap'
|
||||
t_exit = r'exit'
|
||||
t_integer = r'integer'
|
||||
t_real = r'real'
|
||||
t_subroutine = r'subroutine'
|
||||
t_doubleColon = r'::'
|
||||
t_coma = r','
|
||||
t_openBra = r'\['
|
||||
|
|
@ -67,19 +69,19 @@ t_closedParen = r'\)'
|
|||
t_plus = r'\+'
|
||||
t_minus = r'-'
|
||||
t_mul = r'\*'
|
||||
t_string = r'".*"'
|
||||
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 = ' \n'
|
||||
t_string = r'\"(.|\s)*\"'
|
||||
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'
|
||||
|
||||
def t_int(t):
|
||||
r'\d+'
|
||||
|
|
@ -95,7 +97,10 @@ def t_rea(t):
|
|||
|
||||
def t_id(t):
|
||||
r'[a-zA-Z_][a-zA-Z_0-9]*'
|
||||
t.type = 'id'
|
||||
if t.value in reserved:
|
||||
t.type = reserved[ t.value ]
|
||||
else:
|
||||
t.type = 'id'
|
||||
return t
|
||||
|
||||
def t_error(t):
|
||||
|
|
@ -286,18 +291,35 @@ def p_error(p):
|
|||
parser = yacc.yacc()
|
||||
|
||||
fort_program = '''
|
||||
program _matrix
|
||||
program matrix
|
||||
integer [100][100] :: matrix1, matrix2, resultMatrix
|
||||
integer :: m1Rows, m1Columns, m2Rows, m2Columns, temp, temp2
|
||||
subroutine sumMatrices
|
||||
do temp = 1, m1Rows
|
||||
do temp2 = 1, m1Columns
|
||||
resultMatrix(temp,temp2) = matrix(temp,temp2) + matrix(temp,temp2)
|
||||
end do
|
||||
end do
|
||||
end subroutine
|
||||
subroutine printResultMatrix
|
||||
do temp = 1, m1Rows
|
||||
do temp2 = 1, m1Columns
|
||||
print resultMatrix(temp,temp2), " "
|
||||
end do
|
||||
print "\n"
|
||||
end do
|
||||
end subroutine
|
||||
end program
|
||||
'''
|
||||
|
||||
|
||||
#parser.parse(fort_program)
|
||||
parser.parse(fort_program)
|
||||
|
||||
lexer.input(fort_program)
|
||||
while True:
|
||||
tok = lexer.token()
|
||||
if not tok:
|
||||
break
|
||||
print(tok)
|
||||
#lexer.input(fort_program)
|
||||
#while True:
|
||||
# tok = lexer.token()
|
||||
# if not tok:
|
||||
# break
|
||||
# print(tok)
|
||||
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue