Uploaded by manaschougule1008

A 52 Exp 2

advertisement
Experiment : 02
Q.) Implement the lexical analyzer for a simple ‘C’ program.
import re
# Regular expressions for basic token
types token_patterns = [
(r'int|float|char|void', 'KEYWORD'),
(r'\b[a-zA-Z_][a-zA-Z0-9_]*\b', 'IDENTIFIER'),
(r'\b\d+\b', 'CONSTANT'),
(r'\bif|else|while|for|return\b', 'CONTROL'),
(r'\b[\+\-\*/=<>]+', 'OPERATOR'),
(r'\b\(|\)|\{|\}|\[|\]|\,|\;|\:', 'SYMBOL')
]
# Function to perform lexical analysis
def analyze_c_program(input_code):
tokens = []
input_code = input_code.replace('\n', ' ')
while input_code:
found = False
for pattern, token_type in token_patterns:
match = re.match(pattern, input_code) if
match:
tokens.append((token_type, match.group()))
input_code = input_code[len(match.group()):] found =
True
break
if not found:
input_code = input_code[1:] # Skip invalid characters return
tokens
# Example C program
c_program = """
int main() {
int x = 10;
if (x > 5) {
return x;
} else {
return 0;
}
}
"""
tokens = analyze_c_program(c_program)
# Print the tokens
for token in tokens:
print(f"Token Type: {token[0]}, Token Value: {token[1]}")
OUTPUT:
Download