The main difference between scanning and parsing is that scanning is the process of reading the source code one character at a time in a methodical manner to convert them into tokens while parsing is the process of taking the tokens and generating a parse tree as the output.
Generally, a compiler is a software program that is capable of converting the source code into machine code so that the computer can execute that machine code. The compiler goes through multiple phases to compile a program. Scanning and parsing are two activities that occur during this compilation process. Overall, scanning occurs at the lexical analysis phase, whereas parsing occurs at the syntax analysis phase. Furthermore, the lexical analyzer performs scanning while the parser performs parsing.
Key Areas Covered
1. What is Scanning
-Definition, Functionality
2. What is Parsing
-Definition, Functionality
3. Difference Between Scanning and Parsing
-Comparison of key differences
Key Terms
Lexical Analysis, Parsing, Parse Tree, Scanning, Syntax Analysis
What is Scanning
The first phase of compilation is lexical analysis. The lexical analyzer performs this task. It takes the source code as the input. Lexical analyzer reads the source program a character at a time and then converts it into meaningful tokens. The process of reading the source code methodically is called scanning. In this process, the lexical analyzer considers specific information of the source code.
What is Parsing
The tokens generated from lexical analysis goes to the next phase, which is syntax analysis. The parser performs this task. It takes the tokens as input and generates a parse tree as output. Thus, this process is called parsing. Furthermore, the parser checks whether the expression made by the tokens is syntactically correct or not.
Moreover, in addition to lexical analysis and syntax analysis, there are other phases such as semantic analysis, intermediate code generation, code optimization etc. After performing all of the above phases, the source code will be converted into the equivalent machine code.
Difference Between Scanning and Parsing
Definition
Scanning is the process of reading the source code as a stream of characters to convert them to meaningful lexemes or tokens. In contrast, parsing is the process of taking the tokens generated at the lexical analysis phase and transforming them into a parse tree. Thus, this is the main difference between scanning and parsing.
Performed by
Further, the lexical analyzer performs scanning while parser performs parsing.
Associated Phase of the Compilation
Besides, scanning occurs during lexical analysis, whereas parsing occurs during syntax analysis. Hence, this is another difference between scanning and parsing.
Occurrence
Moreover, scanning happens first, while parsing happens after performing scanning.
Conclusion
In overall, a compiler is a software program that is responsible for converting the source code into equivalent machine code. It goes through several phases to accomplish this task. Here, the scanning and parsing are two activities that occur during this compilation process. However, the main difference between scanning and parsing is that scanning is the process of reading the source code one character at a time in a methodical manner to convert them to tokens while parsing is the process of taking the tokens and generating a parse tree as the output.
References:
1.“Compiler Phases – Javatpoint.” Www.javatpoint.com, Available here.
Image Courtesy:
1.”An ideal multi-language, multi-target compiler” By I, Surachit (CC BY-SA 3.0) via Commons Wikimedia
Leave a Reply