#!/usr/bin/env python3 class WrongGrammar(Exception): pass symbols_set = "abc " phrase = input("Введите строку (a, b, c, пробел, <, > разрешены) : ") last_char = '/\\' index = -1 def next_char(char): global index global last_char last_char = char print(char, end='') try: index += 1 new_char = phrase[index] return new_char except IndexError: print("\nINDEX ERROR") index = len(phrase) return '/\\' def in_set(char, _set): if _set.find(char) == -1: return False else: return True; def quotes_lang(char): global last_char if char == '<': char = next_char(char) char = prob_w_quotes(char) if (char == '>') != (last_char == '>'): char = next_char(char) else: raise WrongGrammar('No >') elif in_set(char, symbols_set): char = symbols(char) char = quotes_lang(char) else: pass return char def prob_w_quotes(char): if in_set(char, symbols_set): char = symbols(char) char = prob_w_quotes(char) elif char == '<': char = next_char(char) char = symbols(char) char = prob_right_q(char) else: pass return char def prob_right_q(char): if char == '>': char = next_char(char) char = symbols(char) char = prob_w_quotes(char) elif in_set(char, symbols_set): char = symbols(char) char = prob_right_q(char) elif char == '<': char = next_char(char) char = symbols(char) char = prob_right_q(char) else: pass return char def symbols(char): if char == 'a': char = next_char(char) char = symbols(char) elif char == 'b': char = next_char(char) char = symbols(char) elif char == 'c': char = next_char(char) char = symbols(char) elif char == ' ': char = next_char(char) char = symbols(char) else: pass return char try: index = 0 quotes_lang(phrase[0]) if index != len(phrase): raise WrongGrammar("Wrong Grammar") print("\nГрамматика подходит") except WrongGrammar: print("\nIndex:", index, "\nLen:", len(phrase)) print("\nГрамматика не подходит")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question