-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2016csb1043.l
executable file
·191 lines (153 loc) · 11.1 KB
/
2016csb1043.l
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
D int|float|char|bool
key for|if|else|switch|const|struct
op [+\-*/%<>=]|"++"|"--"|"=="|"||"|"&&"|&
vname [*]*[a-z]([a-z]|[0-9])*?("[".*"]")?
nvname1 ({vname}*[_#%$?@A-Z]+{vname}*)+
nvname2 ([*]*[0-9]+[^ \n;]*({vname}|{nvname1})+)
fname [a-z_]+
nfname {fname}*[0-9A-Z#%$?@]+{fname}*
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/* Struct for identifying the type of declaration*/
struct check{
bool isComment ;
bool isFun ;
bool isVar_Dec ;
bool isStruct;
};
typedef struct check id;
id* parse;
int counter = 0,check=0;
//vector<id> parse;
%}
%%
int line_num = 1;
/* To know all the line numbers */
\n line_num++;
/* For passing on headers */
^[ ]*"#".* ;
/* Verifying for correct variable declarartion */
[ ]*{D}[*]*[ ]+([ ]*({vname})[ ]*([=][ ]*([0-9]+|(["'].*["']))?)?[,]?)+";" {
/*Check for Documentation */
if(counter==0 || (!parse[counter-1].isComment && !parse[counter-1].isStruct)){
check++;
if(check == 1)
printf("Coding conventions are violated at : \n");
printf("Line %d Variable declaration is not documented.\n",line_num);
}
//cout<<"Line "<<line_num<<" Variable declaration is not documented"<<endl;
//parse.push_back(id(3));
if(counter>0 && parse[counter-1].isStruct)
parse[counter].isStruct = true;
parse[counter++].isVar_Dec = true;
}
/* Verifying for incorrect variable declaration */
[ ]*{D}[ ]+([ ]*(({nvname1}|{nvname2})|{vname})[ ]*([=][ ]*([0-9]+|(["'].*["']))?)?[,]?)+";" {
check++;
if(check == 1)
printf("Coding conventions are violated at : \n");
//cout<<"Line "<<line_num<<" Variable name are of invalid syntax"<<endl;
printf("Line %d Variable names are of invalid syntax.\n",line_num);
/*Check for Documentation */
//varDefect(yytext);
if(counter==0 || (!parse[counter-1].isComment && !parse[counter-1].isStruct))
printf("Line %d Variable declaration is not documented.\n",line_num);
if(counter>0 && parse[counter-1].isStruct)
parse[counter].isStruct = true;
parse[counter++].isVar_Dec = true;
//printf("%s",yytext);
}
/* For knowing statements are comments */
"/*"([^*]|"*"+[^/])*"*"+"/" {
/*To keep check of line numbers */
int i ;
for(i=0;i<yyleng;i++){
if(yytext[i] == '\n')
line_num++;
}
/* Pushing is comment to vector list for checking of
proper documentation */
if(counter>0 && parse[counter-1].isStruct)
parse[counter].isStruct = true;
parse[counter++].isComment = true;
}
/* Verifying for proper function names */
^[ ]*{D}[' ']+{fname}[ ]*"("([ ]*{D}[*]*[ ]+{vname}[,]?)*")" { /* Check for Documentation */
if(counter==0 || !parse[counter-1].isComment){
check++;
if(check == 1)
printf("Coding conventions are violated at : \n");
printf("Line %d Function is not documented.\n",line_num);
}
parse[counter++].isFun = true;
}
/*Using special declaration of struct */
[ ]*struct[ ]* {
/* Check for Documentation */
if(counter==0 || !parse[counter-1].isComment){
check++;
if(check == 1)
printf("Coding conventions are violated at : \n");
printf("Line %d Structure is not documented.\n",line_num);
}
parse[counter++].isStruct = true;
}
/*Verifying for invalid function name */
^[ ]*{D}[' ']+{nfname}[ ]*"("([ ]*{D}[*]*[ ]+{vname}[,]?)*")" {
check++;
if(check == 1)
printf("Coding conventions are violated at : \n");
printf("Line %d Function name has invalid syntax\n",line_num);
/* Check for Documentation */
if(counter==0 || !parse[counter-1].isComment)
printf("Line %d Function is not documented.\n",line_num);
parse[counter++].isFun = true;
}
/*Verifying for invalid params */
^[ ]*{D}[' ']+{fname}[ ]*"("([ ]*{D}[*]*[ ]+(({nvname2}|{nvname1})|{vname})[,]?)*")" {
check++;
if(check == 1)
printf("Coding conventions are violated at : \n");
printf("Line %d Parameters passed has invalid syntax\n",line_num);
/* Check for Documentation */
if(counter==0 || !parse[counter-1].isComment)
printf("Line %d Function is not documented.\n",line_num);
parse[counter++].isFun = true;
}
/*Verifying for both invalid params and funvtion name */
^[ ]*{D}[' ']+[^(;]+"(".*")" {
check++;
if(check == 1)
printf("Coding conventions are violated at : \n");
printf("Line %d Function name has invalid syntax\n",line_num);
printf("Line %d Parameters passed has invalid syntax\n",line_num);
/* Check for Documentation */
if(counter==0 || !parse[counter-1].isComment)
printf("Line %d Function is not documented.\n",line_num);
parse[counter++].isFun = true;
}
/*Parsing out names written in double quotes or single */
[ ]*(\"|\').*(\"|\') ;
/* Parsing out valid identifers opeartors and numbers */
([0-9]+("."[0-9]+)?)|{op}|{vname} ;
/* To classify Invalid identifers */
({nvname1}|{nvname2})* {
check++;
if(check == 1)
printf("Coding conventions are violated at : \n");
printf("Line %d Not a valid Identifier name\n",line_num);
}
/* for passing on braces and whitespaces*/
[{} \t();:,"'] ;
%%
int main(int argc,char* argv[]){
parse = (id*)malloc(1000*sizeof(id));
yyin = fopen(argv[1],"r");
yyout = fopen("./c.txt","w");
yylex();
if(check == 0)
printf("Program meets the coding conventions !\n");
return 0;
}