This repository has been archived on 2026-03-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
TP-CMPL/ply/example.py
2022-03-05 21:56:48 +01:00

70 lines
1.1 KiB
Python

import ply.lex as lex
tokens = (
'INTEGER',
'FLOAT',
'ADD_OP',
'MUL_OP',
'BRACKET',
)
def t_BRACKET(t):
r'\(|\)'
if t.value == '(':
t.value = "L"
else:
t.value = "R"
return t
def t_MUL_OP(t):
r'\*|/'
return t
def t_ADD_OP(t):
r'\+|-'
return t
def t_FLOAT(t):
r'\d+\.\d*'
t.value = float(t.value)
return t
def t_INTEGER(t):
r'\d+'
t.value = int(t.value)
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
t_ignore = ' |\t'
def t_error(t):
print("Illegalcharacter '%s'" %t.value[0])
t.lexer.skip(1)
if __name__ == "__main__":
import sys
lexer = lex.lex()
lexer.input(sys.stdin.read())
lb = 0
for token in lexer:
if token.type == "BRACKET":
if token.value == "L":
lb += 1
else:
lb -= 1
if lb < 0:
print("BRACKET missing")
print("line %d : %s (%s) " % (token.lineno, token.type, token.value))
if lb != 0:
print("BRACKET missing")