Cs 606
Cs 606
Assignment 2:
Solution 1:
The lexical analyzer is the first phase of a compiler. It reads the source code character by
character and groups the characters into meaningful sequences called tokens. These tokens
represent fundamental syntactic elements like keywords, operators, identifiers, constants, etc.
The lexical analyzer performs the following roles:
1. Input Buffering: Reads the input program in chunks to handle large files efficiently.
2. Scanning: Scans the characters and groups them into tokens.
3. Pattern Matching: Matches sequences of characters to pre-defined patterns (regular
expressions) for tokens like keywords, operators, or identifiers.
4. Token Generation: Creates tokens (e.g., id, num, keyword) and passes them to the
syntax analyzer.
C Code:
int main() {
int x = 10;
float y = 20.5;
printf("Sum: %f", x + y);
return 0;
}
Conclusion
The lexical analyzer's output serves as input to the syntax analyzer, forming the foundation for
the rest of the compilation process. Its primary focus is ensuring that the source code is
transformed into a structured sequence of tokens for further processing.
Solution 2:
Line 3: x = x + 5;
Line 4: }
Summary of Tokens:
1. Keywords: int, if
2. Identifiers: x (occurs multiple times)
3. Operators: =, >, +
4. Constants: 20, 10, 5
5. Punctuation: (, ), {, }, ;
Purpose:
The lexical analyzer will generate these tokens from the input code, removing unnecessary
whitespaces and comments, and then pass the tokens to the syntax analyzer for parsing.