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
|
import ply.yacc as yacc
|
||||||
|
|
||||||
tokens = [
|
tokens = [
|
||||||
'program',
|
|
||||||
'end',
|
|
||||||
'doubleColon',
|
'doubleColon',
|
||||||
'coma',
|
'coma',
|
||||||
'integer',
|
|
||||||
'real',
|
|
||||||
'openBra',
|
'openBra',
|
||||||
'closedBra',
|
'closedBra',
|
||||||
'int',
|
'int',
|
||||||
'rea',
|
'rea',
|
||||||
'subroutine',
|
|
||||||
'parens',
|
'parens',
|
||||||
'openParen',
|
'openParen',
|
||||||
'closedParen',
|
'closedParen',
|
||||||
'read',
|
|
||||||
'print',
|
|
||||||
'if',
|
|
||||||
'then',
|
|
||||||
'else',
|
|
||||||
'elif',
|
|
||||||
'do',
|
|
||||||
'swap',
|
|
||||||
'exit',
|
|
||||||
'string',
|
'string',
|
||||||
'plus',
|
'plus',
|
||||||
'minus',
|
'minus',
|
||||||
|
|
@ -41,22 +27,38 @@ tokens = [
|
||||||
'lessEquals',
|
'lessEquals',
|
||||||
'moreEquals',
|
'moreEquals',
|
||||||
'id',
|
'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_doubleColon = r'::'
|
||||||
t_coma = r','
|
t_coma = r','
|
||||||
t_openBra = r'\['
|
t_openBra = r'\['
|
||||||
|
|
@ -67,19 +69,19 @@ t_closedParen = r'\)'
|
||||||
t_plus = r'\+'
|
t_plus = r'\+'
|
||||||
t_minus = r'-'
|
t_minus = r'-'
|
||||||
t_mul = r'\*'
|
t_mul = r'\*'
|
||||||
t_string = r'".*"'
|
t_string = r'\"(.|\s)*\"'
|
||||||
t_or = r'or'
|
t_or = r'\.or\.'
|
||||||
t_and = r'and'
|
t_and = r'\.and\.'
|
||||||
t_not = r'not'
|
t_not = r'\.not\.'
|
||||||
t_doubleEquals = r'=='
|
t_doubleEquals = r'\=\='
|
||||||
t_equals = r'='
|
t_equals = r'\='
|
||||||
t_notEquals = r'/='
|
t_notEquals = r'\/\='
|
||||||
t_div = r'/'
|
t_div = r'\/'
|
||||||
t_lessEquals = r'<='
|
t_lessEquals = r'\<\='
|
||||||
t_less = r'<'
|
t_less = r'\<'
|
||||||
t_moreEquals = r'>='
|
t_moreEquals = r'\>\='
|
||||||
t_more = r'>'
|
t_more = r'\>'
|
||||||
t_ignore = ' \n'
|
t_ignore = ' \t\r\n\f\v'
|
||||||
|
|
||||||
def t_int(t):
|
def t_int(t):
|
||||||
r'\d+'
|
r'\d+'
|
||||||
|
|
@ -95,6 +97,9 @@ def t_rea(t):
|
||||||
|
|
||||||
def t_id(t):
|
def t_id(t):
|
||||||
r'[a-zA-Z_][a-zA-Z_0-9]*'
|
r'[a-zA-Z_][a-zA-Z_0-9]*'
|
||||||
|
if t.value in reserved:
|
||||||
|
t.type = reserved[ t.value ]
|
||||||
|
else:
|
||||||
t.type = 'id'
|
t.type = 'id'
|
||||||
return t
|
return t
|
||||||
|
|
||||||
|
|
@ -286,18 +291,35 @@ def p_error(p):
|
||||||
parser = yacc.yacc()
|
parser = yacc.yacc()
|
||||||
|
|
||||||
fort_program = '''
|
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
|
end program
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
#parser.parse(fort_program)
|
parser.parse(fort_program)
|
||||||
|
|
||||||
lexer.input(fort_program)
|
#lexer.input(fort_program)
|
||||||
while True:
|
#while True:
|
||||||
tok = lexer.token()
|
# tok = lexer.token()
|
||||||
if not tok:
|
# if not tok:
|
||||||
break
|
# break
|
||||||
print(tok)
|
# print(tok)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue