Presentation on theme: "Notes on Python Regular Expressions and parser generators (by D"— Presentation transcript:
1 Notes on Python Regular Expressions and parser generators (by D Notes on Python Regular Expressions and parser generators (by D. Parson)These are the Python supplements to the author’s slides for Chapter 1 and Section 2.1.C310Spring2014.html has a link to the author’s slides, which are password protected by your K.U. Windows login / password used to access your student account.
2 Regular Expressions in Python re module in the optional Python text.A RE is a pattern in the form of a string.compile(pattern [, flags]) compiles an RE expression into a finite automaton object.Return value can be used by other functions.Flags are for case, multiline, and meta-character options.search(pattern, string [, flags) searches string for the first match of pattern.match(pattern, string [, flags) checks at string’s beginning.Both return a MatchObject or None.
4 Python Regular Expression Examples >>> m1 = search('a+z*(b.d)', 'abcdefghi')>>> m1<_sre.SRE_Match object at 0x11c520>>>> m1.groups()('bcd',)>>> m1.start()>>> m1.end()4>>> m1.start(0)>>> m1.start(1)1 # Group 0 is the entire match, 1 is the first parenthesized subexpression, etc.
5 Learn the major Meta-characters! Text – verbatim text. – any character except newline^ – matches start of the string (anchor)$ – matches end of the string* – Kleene start, 0 or more subpattern repetitions+ – Kleene plus, 1 or more subpattern repetitions? – optional, 0 or 1 subpattern occurrence| – alternation, either left or right subpattern() – group a subexpression inside parentheses\ – escape a meta-character (make it normal)[set of chars], [^set of chars not matched]
7 More Python RE Examples (sub) >>> sub('a([^b]+)b', 'A\\1B', 'a123b45ab67a9b aab')'A123B45ab67A9B AaB'The parenthesized subexpression matches one or more occurrences of anything except for b.The matched substring of the first parenthesized subexpression is group 1.The replacement pattern \1 says “insert group 1 at this point.”Effect is to re-insert characters between a and b.
8 (a|b)c+d is a simple example expression. Finite State AutomataA regular expression compiler translates a regular expression into a finite state automaton.This could be a linked data structure or code. It looks like a graph of mapping steps needed for the regular expression.There are nondeterministic and deterministic flavors.(a|b)c+d is a simple example expression.ccas1εstarts3s4acceptbcds2
9 Lookahead 1 types of parsers. LL(1) and LR(1) grammars require a parser to get at most 1 look-ahead terminal from the scanner.LL(1) cannot handle left-recursive grammar productions. It can handle other recursion.LR(1) and its variants can handle left, right and nested recursion; left is the most efficient.A generated parser is essentially a deterministic finite state automaton that uses a stack to keep track of nested syntactic structures.This topic is covered exhaustively in compiler design.
10 Parser generators in Python. YAPPS2 is an LL(1) parser generator.PLY is a Python LALR(1) (subset of LR(1)) equivalent to UNIX YACC and GNU Bison that are used to generate compilers for C code.Both generate Python executable parsers from stylized Python code.