tpc6 fix
This commit is contained in:
parent
cf4352b80d
commit
cfbb5ac6e7
3 changed files with 118 additions and 0 deletions
22
TPC6/example1
Normal file
22
TPC6/example1
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
/* factorial.p
|
||||||
|
-- 2023-03-20
|
||||||
|
-- by jcr
|
||||||
|
*/
|
||||||
|
|
||||||
|
int i;
|
||||||
|
|
||||||
|
// Função que calcula o factorial dum número n
|
||||||
|
function fact(n){
|
||||||
|
int res = 1;
|
||||||
|
while res > 1 {
|
||||||
|
res = res * n;
|
||||||
|
res = res - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Programa principal
|
||||||
|
program myFact{
|
||||||
|
for i in [1..10]{
|
||||||
|
print(i, fact(i));
|
||||||
|
}
|
||||||
|
}
|
17
TPC6/example2
Normal file
17
TPC6/example2
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
/* max.p: calcula o maior inteiro duma lista desordenada
|
||||||
|
-- 2023-03-20
|
||||||
|
-- by jcr
|
||||||
|
*/
|
||||||
|
|
||||||
|
int i = 10, a[10] = {1,2,3,4,5,6,7,8,9,10};
|
||||||
|
|
||||||
|
// Programa principal
|
||||||
|
program myMax{
|
||||||
|
int max = a[0];
|
||||||
|
for i in [1..9]{
|
||||||
|
if max < a[i] {
|
||||||
|
max = a[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
print(max);
|
||||||
|
}
|
79
TPC6/tpc6.py
Normal file
79
TPC6/tpc6.py
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
import ply.lex as lex
|
||||||
|
|
||||||
|
|
||||||
|
reserved = {
|
||||||
|
"function": "FUNC",
|
||||||
|
"program": "PROG",
|
||||||
|
"while": "WHILE",
|
||||||
|
"for": "FOR",
|
||||||
|
"in": "IN",
|
||||||
|
"if": "IF",
|
||||||
|
"else": "ELSE",
|
||||||
|
"int": "INT",
|
||||||
|
"float": "FLOAT",
|
||||||
|
}
|
||||||
|
|
||||||
|
states = (
|
||||||
|
('mlcom', "exclusive"),
|
||||||
|
)
|
||||||
|
|
||||||
|
tokens = ["ID", "NUM","GT", "LT", "COMMENT","OPENCOM","CLOSECOM","DDOT"] + list(reserved.values())
|
||||||
|
literals = ",=*+-;{}[]()"
|
||||||
|
|
||||||
|
|
||||||
|
def t_ID(t):
|
||||||
|
r"[a-zA-Z_][a-zA-Z_0-9]*"
|
||||||
|
t.type = reserved.get(t.value, "ID")
|
||||||
|
return t
|
||||||
|
|
||||||
|
def t_NUM(t):
|
||||||
|
r"\d+(\.\d+)?"
|
||||||
|
if "." in t.value:
|
||||||
|
t.value = float(t.value)
|
||||||
|
else:
|
||||||
|
t.value = int(t.value)
|
||||||
|
return t
|
||||||
|
def t_DDOT(t):
|
||||||
|
r"\.\."
|
||||||
|
return t
|
||||||
|
|
||||||
|
def t_GT(t):
|
||||||
|
r">"
|
||||||
|
return t
|
||||||
|
|
||||||
|
def t_LT(t):
|
||||||
|
r"<"
|
||||||
|
return t
|
||||||
|
|
||||||
|
def t_INITIAL_OPENCOM(t):
|
||||||
|
r"\/\*"
|
||||||
|
t.lexer.begin('mlcom')
|
||||||
|
return t
|
||||||
|
|
||||||
|
def t_mlcom_CLOSECOM(t):
|
||||||
|
r"\*\/"
|
||||||
|
t.lexer.begin("INITIAL")
|
||||||
|
return t
|
||||||
|
|
||||||
|
def t_COMMENT(t):
|
||||||
|
r"\/\/.*"
|
||||||
|
pass
|
||||||
|
|
||||||
|
def t_mlcom_COMMENT(t):
|
||||||
|
r"[^*]+"
|
||||||
|
pass
|
||||||
|
|
||||||
|
t_ANY_ignore = " \t\n"
|
||||||
|
|
||||||
|
def t_ANY_error(t):
|
||||||
|
print("Illegal character '%s'" % t.value[0])
|
||||||
|
t.lexer.skip(1)
|
||||||
|
|
||||||
|
lexer = lex.lex()
|
||||||
|
|
||||||
|
f = open("example2","r")
|
||||||
|
|
||||||
|
lexer.input(f.read())
|
||||||
|
|
||||||
|
for tok in lexer:
|
||||||
|
print(tok)
|
Loading…
Add table
Add a link
Reference in a new issue