FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ScannerC/main.c at master · JRinderer/ScannerC · GitHub
JRinderer
ScannerC
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ScannerC
/
main.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
105 lines (81 loc) · 2.67 KB
Breadcrumbs
ScannerC
/
main.c
Copy path
File metadata and controls
105 lines (81 loc) · 2.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#include
"token.h"
#include
<stdio.h>
#include
<ctype.h>
#include
<memory.h>
#include
<stdlib.h>
#include
"scanner.h"
int
main
(
int
argc
,
char
*
argv
[])
{
FILE
*
filePtr
;
/*---------Begin process cmd-line args and Redirection--------*/
switch
(
argc
) {
case
1
:
// No parameters, use stdin
// printf("NO argument provided\n");
filePtr
=
stdin
;
break
;
case
2
:
// One parameter, use .lan file supplied
if
( (
filePtr
=
fopen
(
strcat
(
argv
[
1
],
".kl"
),
"r"
))
==
NULL
) {
printf
(
"Cannot open input file.\n"
);
exit
(
1
);
}
break
;
default
:
printf
(
"Syntax: scanner [file] (.lan is implicit)\n"
);
exit
(
0
);
}
/*---------End process cmd-line args and Redirection----------*/
/*---------Begin check if file empty-----------*/
fseek
(
filePtr
,
0
,
SEEK_END
);
if
(
ftell
(
filePtr
)
==
0
) {
printf
(
"File is empty.\n"
);
exit
(
1
);
}
else
{
rewind
(
filePtr
);
}
/*---------/End check if file empty-------------*/
/*---------Begin check for invalid characters and max length-------------*/
char
c
;
int
numLine
=
1
;
int
charCount
=
0
;
char
tempStr
[
MAX
];
// ! Remember: C doesn't do array out-of-bound checking!
int
i
;
int
isValid
=
1
;
// true
while
((
c
=
fgetc
(
filePtr
))
!=
EOF
) {
if
(
c
==
'\n'
)
numLine
++
;
// Exclude comment line starting with '//' and ending with new line
if
(
c
==
'/'
) {
if
(
fgetc
(
filePtr
)
==
'/'
) {
while
((
c
=
fgetc
(
filePtr
))
!=
'\n'
) {}
numLine
++
;
}
}
if
(
isalnum
(
c
)) {
tempStr
[
charCount
]
=
c
;
// array out-of-bound checking not happen here
charCount
++
;
if
(
charCount
>
MAX
) {
printf
(
"Word '%s' on line number %d is too long. \n"
,
tempStr
,
numLine
);
exit
(
1
);
}
}
else
if
(
isspace
(
c
)
||
isExAcceptableChar
(
c
)) {
charCount
=
0
;
}
else
{
printf
(
"Invalid character '%c' at line %d. \n"
,
c
,
numLine
);
isValid
=
0
;
// false
}
}
if
(
isValid
==
0
) {
printf
(
"Something wrong with the input file. \n"
);
exit
(
1
);
}
rewind
(
filePtr
);
/*---------/End check for invalid characters and max length-------------*/
/*---------Begin Job-------------*/
// At this time, the file is good. Now let scanner.c do the work
TokenType
tokenType
=
UNDEF
;
while
((
tokenType
=
getTokenType
(
filePtr
))
!=
EOT
) {}
getTokenType
();
/*---------/End Job-------------*/
fclose
(
filePtr
);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL