Граматика expression = term { ("+" | "-") term} . term = factor { ("*"|"/") factor} . factor = constant | variable | "(" expression ")" . variable = "x" | "y" | "z" . constant = digit {digit} . digit = "0" | "1" | " ... " | "9" . LL(2) Вихідний код "1 + 4 * 3 - ( 5 + 7 )" Лексичний аналіз ("1", "+", "4", "*", "3", "-", "(", "5", "+", "7", ")" ) Таблиця переходів expression term factor variable constant digit "(" ")" $ expression + term + factor + variable + constant + digit + + "(" + + ")" + + $ + 1 ( 4 * 3 - ( 5 + 7 )) $ + 1 - 4 * 3 5 + 7 $ |__+ |__1 |__- |__* | |__ 4 | |__ 3 |__+ |__ 5 |__ 7 (+, 1, (-, (*, 4, 5), (+,5,7))) Рекурсивний нисхідний аналізатор expression = term { ("+" | "-") term} . => is_applicable(), apply() term = factor { ("*"|"/") factor} . => is_applicable(), apply() factor = constant | variable | "(" expression ")" . => is_applicable(), apply() variable = "x" | "y" | "z" . => is_applicable(), apply() constant = digit {digit} . => is_applicable(), apply() digit = "0" | "1" | " ... " | "9" . => is_applicable(), apply() digit_is_applicable(s): return re.match("/[0-9]/", s) constant__is_applicable(s): return re.match("/[0-9]+/", s) term_is_applicable(s): x = re.split(r"/\*|\//", s) return factor_is_applicable(x[0]) expression_apply(s): x = re.split(r"/\+|-/", s, 1) return (+, term_apply(x[0]), term_apply(x[1])) digit_apply(s): ($, s) print(expression_apply("1 + 4 * 3 - ( 5 + 7 )")) (+, 1, (-, (*, 4, 5), (+,5,7)))