Every programming language does the process of parsing tokens into its meaningful lexical form so that the compiler or interpreter can convert it into its meaningful output. In this article, you will learn about the parsing in Python and what module helps in parsing.
What is Parsing?
Parsing is defined as the process of converting codes to machine language to analyze the correct syntax of the code. Python provides a library called a parser. For example, if an application takes data from the user and the data is not in the required format, in such cases you can use a parser that converts the data into the required format for further processing.
Parsing can be performed in many ways such as using a parser module, parsing through a regular expression, parsing through string methods like the split() and strip() methods.
Let us consider the below code to understand how the parser module helps in parsing.
import parser
print("Input expression for parser module")
expression = "2 + 2"
print(" parsing the input expression")
parsing = parser.expr(expression)
print(parsing)
print(" Converting parsed object to code object")
code = parsing.compile()
print(code)
print(" Parsed result: ")
res = eval(code)
print(res)
Output:
Explanation:
First we have imported the parser module that will help us witness the parsing process. Then, we have the print() function to display a output message. The parser.expr() take the Python expression to parse it into object along and will also show the hexadecimal memory location. Another print() function will display the message: Converting parsed object to code object.
On the next line we have used the parsing.compile() method that does the parsing of object expression to code object. The code has the complied output. The parser then churns the object into a module that also takes another memory location. To evaluate the compiled code we have to use the eval() function which we will store in the res object and display it using the print().
Here a string expression is not concatenating but its numbers were working as integers (resulting in 2+2 = 4) because that expression has been parsed in a different approach.
Conclusion:
Software testers and quality assurance team leverages this module to perform different testing. In this article, we have learned how learned how parsing in Python takes place and how Python expression gets converted to data from one format to another.
Although leveraging a separate parsing module does double parsing of a Python program, it will be less efficient. But for testing team, finding the bug or for researchers, creating an interpreter or compiler – this module is very useful.