In: Computer Science
Implement Lexer(Scanner) that scans the whole source code entered by the user that includes Alphabets(characters), integers(digits), operators(arithmatic, logical and relational operators), and special symbols. output should be in the form of Symbol table that shows each token with its name, type and number of times entered(lexexmes). dictionary should be implemented in the code by writing RegX for each category. code entered from internet will be discarded. Implementation language will be of your own choice.
I really need the answer of this question, if someone can do that i would really appreciate it.
When an identifier in the source program is detected by the lexical analyzer, the identifier is entered into the symbol table.
A symbol table is a data structure containing a record for each identifier, with fields for the attributes of the identifier. The data structure allows us to find the record for each identifier quickly and to store or retrieve data from that record quickly.
Code:
function lexan : integer;
var lexbuf : array[0...100] of char;
c : char;
begin
loop begin
read a character into c;
if c is a blank or a tab then
do nothing
else if c is a newline then
lineno := Iineno + I
else if c is a digit then begin
set tokenval to the value of this and following digits;
return NUM
end
elseif c is a letter then begin
place c and successive letters and digits into lexbuf;
p := lookup(lexbuf);
if p=0 then
p := insert(lexbuf,ID);
tokenval := p
return the token field of table entry p ;
end
else begin /* token is a single character */
set tokenval to NONE; /*there is no attribute */
return integer ending of character c
end
end
end