===== Page 6 ===== Q1. Design a LEX Code to count the number of lines, space, tab-meta character, and rest of characters in each Input pattern. Solution: % { #include<stdio.h> int line = 0 , space = 0 , tab = 0 , meta = 0 , other = 0; % } %% "\n" {line++;} " " {space++;} "\t" {tab++;} [\^\$\%\*\+\^\&\()\{}\}[]\v\"] {meta++;} . {other++;} %% int main(){ yylex(); printf("Line:%d\n",line); printf("Space:%d\n",space); printf("Tab:%d\n",tab); printf("Meta:%d\n",meta); printf("Other:%d\n",other); return 0; } int yywrap(){ return 1; } Output: int main(){ cout << "hello"; } Line:3 Space:3 Tab:1 Meta:7 Other:18 ===== Page 7 ===== Q2. Design a LEX Code to identify and print valid Identifier of C/C++ in given Input pattern. Solution: % { #include<stdio.h> % } %% int|float|return|void|if|...
Posts
Showing posts from March, 2025