Morteza Kashi
A Lexical Analyzer program providing a scanner for a programming language whose lexical specifications are given
This nice program is a design and implementation of a scanner for a programming language whose lexical specifications are given below. The scanner identifies and outputs tokens (valid words and punctuation) in the source program. Its output is a token that can thereafter can be used by the syntactic analyzer to verify that the program is syntactically valid. The syntax of the language will be specified later. As a reference, a sample program is given below.
Atomic lexical elements of the language
id |
::= |
letter alphanum* |
num |
::= |
nonzero digit* fraction | 0 fraction |
alphanum |
::= |
letter | digit | _ |
fraction |
::= |
.digit* nonzero | .0 | e |
letter |
::= |
a..z |A..Z |
digit |
::= |
0..9 |
nonzero |
::= |
1..9 |
Operators, punctuation and reserved words
== |
+ |
( |
do |
<> |
- |
) |
else |
< |
* |
{ |
if |
> |
/ |
} |
int |
<= |
= |
[ |
program |
>= |
and |
] |
read |
; |
not |
/* |
float |
. |
or |
*/ |
return |
, |
in |
fi |
then |
|
out |
write |
for |
In this program , I have taken advantage of Linked list as a Data Structure. This data structure contains information such as (1) the token type (2) its value (or lexeme) when applicable and (3) its location in the source code.
My lexical analyzer program prints all above information for tokens to a file "output.txt" on C drive in your computer.
My lexical analyzer program, also, prints out any error with the address of that error in a given text for a program. These errors will be printed out in a file "Error.txt" on your C drive in your computer.
A sample program
program@{
sum(in:int n, out:int res){
/* do the factorial of n numbers */
int i;
float val[100F];
float res;
i = 1000000000000000;
res = 0.0;
~while (!i<=n) do{
read(val[i]);
res = res +valkhu5yr_fds6ghf[^i];
i = i + %1;
};
};
int n#;
float ave;
read(n);
ave = (sum(n)/n);
write(ave)&;
};
As you can see, there are some errors in that program that should be found by my program.
If you want to execute this program on your own computer, please find the "input.txt" file ,containing the mentioned sample program , by clicking on the following link. Please note that you must put this file on C drive in your computer as it is designed in this program.
Again, please note that your out put would be located in "output.txt" file on your C drive of your computer as it is designed in my program.
If you like to see how your output and error files will look like in advance , pleas click on the following link:
Please find my complete lexical analyzer C++ program y clicking on the following link:
Please click on the following link, should you want to see the execution of the program. To do that, Save the file "LexicalAnalyzer.exe" on your desktop or somewhere else on your computer. Then, double click on it to see the execution of the program.
Please contact me by clicking HERE and filling out the coming simple form, should you have any question
about this program.