-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1.l
More file actions
53 lines (42 loc) · 1.62 KB
/
Copy pathq1.l
File metadata and controls
53 lines (42 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
%{
#include <stdio.h>
int line_number = 1;
%}
/* Regular Expression Definitiions */
DIGIT [0-9]
LETTER [a-zA-Z_]
ID {LETTER}({LETTER}|{DIGIT})*
INT_CONST {DIGIT}+
FLOAT_CONST {DIGIT}+"."{DIGIT}+
WS [ \t]+
%%
"\n" { line_number++; }
{WS} { } /* Skip spaces and tabs */
/* Keywords */
"int" { printf("<KEYWORD, int, %d>\n", line_number); }
"float" { printf("<KEYWORD, float, %d>\n", line_number); }
"if" { printf("<KEYWORD, if, %d>\n", line_number); }
"else" { printf("<KEYWORD, else, %d>\n", line_number); }
"while" { printf("<KEYWORD, while, %d>\n", line_number); }
"print" { printf("<KEYWORD, print, %d>\n", line_number); }
/* Operators */
"=="|"!="|"<="|">="|"<"|">" { printf("<OP_REL, %s, %d>\n", yytext, line_number); }
"&&"|"||"|"!" { printf("<OP_LOGIC, %s, %d>\n", yytext, line_number); }
"+"|"-"|"*"|"/"|"%" { printf("<OP_ARITH, %s, %d>\n", yytext, line_number); }
"=" { printf("<ASSIGN, =, %d>\n", line_number); }
/* Delimiters */
";"|"("|")"|"{"|"}" { printf("<DELIMITER, %s, %d>\n", yytext, line_number); }
/* Constants and Identifiers */
{FLOAT_CONST} { printf("<FLOAT_CONST, %s, %d>\n", yytext, line_number); }
{INT_CONST} { printf("<INT_CONST, %s, %d>\n", yytext, line_number); }
{ID} { printf("<ID, %s, %d>\n", yytext, line_number); }
/* Lexical Error Reporting */
. { printf("Lexical Error: Unexpected '%s' at line %d\n", yytext, line_number); }
%%
int yywrap() {
return 1;
}
int main() {
yylex();
return 0;
}