introduction to parsing both the ini_header and comment tokensmatchers are simple regexesand since both use the t_ignore_ prefixboth will be correctly matched--and then discarded an alternative approach to ignoring matches is to define function that just uses the t_ prefix ( t_comment())and that has suite of pass (or return none)since if the return value is none the token is discarded for the key and value tokens we have used functions rather than regexes in such cases the regex to match must be specified in the function' docstring-and here the docstrings are raw strings since that is our practice for regexesand it means we don' have to escape backslashes when function is used the token is passed as token object (following the ply examplesnaming conventionsof type ply lex lextoken the matched text is held in the ply lex lextoken value attributeand we are permitted to change this if we wish we must always return from the function if we want the token included in the results in the case of the t_key(functionwe lowercase the matching key if the lowercase_keys variable (from an outer scopeis true and for the t_value(functionwe strip off the and any leading or trailing whitespace in addition to our own custom tokensit is conventional to define couple of ply-specific functions to provide error reporting def t_newline( ) "\ + lexer lineno +len( valuedef t_error( )line value lstrip( line find("\ "line line if =- else line[:iprint("failed to parse line { }{ }format( lineno line)the token' lexer attribute (of type ply lex lexerprovides access to the lexer itself here we have updated the lexer' lineno attribute by the number of newlines that have been matched notice that we don' have to specifically account for blank lines since the t_newline(matching function effectively does that for us if an error occurs the t_error(function is called we print an error message and at most one line of the input we add to the line number since ply' lexer lineno attribute starts counting from with all the token definitions in place we are ready to lex some data and create corresponding key-value dictionary