A parser is a software component used in parsing, where the source code is fractionalized into smaller parts so that software programs can understand it better. Namely, a parser serves to create a data structure from the input (frequently text) in the form of a logical diagram that is most often called a parse tree.
These parse trees make it easier for the software program to determine the context of each part of the source code and see how each one relates to the underlying code structure.
Two main ways parser tasks can be done
- Top-down parsing, in which the process begins with the start symbol and the parse trees are created from the root node in the data structure.
- Bottom-up parsing, where the parse trees are built starting at the leaves and ending with the root node. Here, the data structures end with the start symbol.
The three stages parsers go through
- Lexical analysis. The parser uses the input data to create tokens, which are the smaller unit in a programming language that has meaning. The lexer identifies keywords, identifiers, literals, operators, and other lexical elements and produces a sequence of tokens.
- Syntactic analysis. The parser checks whether the tokens it created form a helpful expression.
- Semantic parsing. The parser verifies whether the tokens are semantically consistent with a particular programming language by using syntax trees and symbol tables.