-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlex.l
More file actions
135 lines (125 loc) · 6.08 KB
/
lex.l
File metadata and controls
135 lines (125 loc) · 6.08 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
%{
#include "parser.tab.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *token_type;
char *lexeme;
int line_number;
} Token;
Token token_list[100];
int token_count = 0;
int line_number = 0;
int isPresent(const char *text) {
for (int i = 0; i < token_count; i++) {
if(strcmp(text,token_list[i].lexeme) == 0){
return 0;
}
}
return 1;
}
void store_token(const char *type, const char *text) {
if(isPresent(text)){
token_list[token_count].token_type = strdup(type);
token_list[token_count].lexeme = strdup(text);
token_list[token_count].line_number = line_number;
token_count++;
}
}
%}
%%
"#include" {store_token("Keyword", yytext); return PREPROCESSOR; }
[<>] {store_token("Punctuation", yytext); return SPECIAL_SYMBOL;}
"iostream" {store_token("Identifiers", yytext); return HEADER_FILE; }
"stack" {store_token("Identifiers", yytext); return STACK; }
"int" {store_token("DataTypes", yytext); return INT; }
"char*" {store_token("DataTypes", yytext); return CHARSTAR; }
"void" {store_token("Keyword", yytext); return VOID; }
"main" {store_token("Identifiers", yytext); return MAIN;}
"using" {store_token("Keyword", yytext); return USING; }
"namespace" {store_token("Keyword", yytext); return NAMESPACE; }
"std" {store_token("Identifiers", yytext); return STD; }
"cout" {store_token("STL_Function", yytext); return COUT; }
"endl" {store_token("Keyword", yytext); return ENDL; }
"for" {store_token("Keyword", yytext); return FOR; }
"if" {store_token("Keyword", yytext); return IF; }
"return" {store_token("Keyword", yytext); return RETURN; }
"size" {store_token("Identifiers", yytext); return SIZE; }
"pop" {store_token("Identifiers", yytext); return POP; }
"push" {store_token("Identifiers", yytext); return PUSH; }
"empty" {store_token("Identifiers", yytext); return EMPTY; }
"top" {store_token("Identifiers", yytext); return TOP; }
"\""([^\"\n]+)"\"" {yylval.str = strdup(yytext);store_token("Identifiers", yytext); return STRING_LITERAL; }
"(" {store_token("Punctuation", yytext); return LPAREN; }
")" {store_token("Punctuation", yytext); return RPAREN; }
"{" {store_token("Punctuation", yytext); return LBRACE; }
"}" {store_token("Punctuation", yytext); return RBRACE; }
";" {store_token("Punctuation", yytext); return TERMINATOR_SYMBOL; }
"," {store_token("Punctuation", yytext); return COMMA; }
"=" {store_token("Operators", yytext); return ASSIGNMENT_OPERATOR; }
"+" {store_token("Operators", yytext); return INCREMENT_OPERATOR;}
"<<" {store_token("Operators", yytext); return OPERATOR; }
"[" {store_token("Punctuation", yytext); return LBRACKET; }
"]" {store_token("Punctuation", yytext); return RBRACKET; }
"." {store_token("Operators", yytext); return DOT;}
[0-9]+ {yylval.num = atoi(yytext);store_token("Literals", yytext); return NUMBER; }
[A-Za-z_][A-Za-z0-9_]* {yylval.id = strdup(yytext);store_token("Identifiers", yytext); return IDENTIFIER; }
[\n] {line_number++;}
[ \t\r]+ { }
. { }
%%
int yywrap() {
return 1;
}
void print_line(){
printf(" Line : %d",line_number);
}
void print_tokens() {
printf("Total Tokens : %d\n",token_count);
printf("=======================================================\n");
printf("| %-20s | %-15s | %-10s |\n", "Token Type", "Lexeme","Line No.");
printf("=======================================================\n");
// Identifiers
for (int i = 0; i < token_count; i++) {
if (strcmp(token_list[i].token_type, "Identifiers") == 0) {
printf("| %-20s | %-15s | %-10d |\n", token_list[i].token_type, token_list[i].lexeme, token_list[i].line_number);
}
}
printf("_______________________________________________________\n");
// Operators
for (int i = 0; i < token_count; i++) {
if (strcmp(token_list[i].token_type, "Operators") == 0) {
printf("| %-20s | %-15s | %-10d |\n", token_list[i].token_type, token_list[i].lexeme, token_list[i].line_number);
}
}
printf("_______________________________________________________\n");
// Punctuation
for (int i = 0; i < token_count; i++) {
if (strcmp(token_list[i].token_type, "Punctuation") == 0) {
printf("| %-20s | %-15s | %-10d |\n", token_list[i].token_type, token_list[i].lexeme, token_list[i].line_number);
}
}
printf("_______________________________________________________\n");
// Keywords
for (int i = 0; i < token_count; i++) {
if (strcmp(token_list[i].token_type, "Keyword") == 0) {
printf("| %-20s | %-15s | %-10d |\n", token_list[i].token_type, token_list[i].lexeme, token_list[i].line_number);
}
}
printf("_______________________________________________________\n");
// Literals
for (int i = 0; i < token_count; i++) {
if (strcmp(token_list[i].token_type, "Literals") == 0) {
printf("| %-20s | %-15s | %-10d |\n", token_list[i].token_type, token_list[i].lexeme, token_list[i].line_number);
}
}
printf("_______________________________________________________\n");
// Standard Library function
for (int i = 0; i < token_count; i++) {
if (strcmp(token_list[i].token_type, "STL_Function") == 0) {
printf("| %-20s | %-15s | %-10d |\n", token_list[i].token_type, token_list[i].lexeme, token_list[i].line_number);
}
}
printf("=======================================================\n\n");
}