0

contoh scanner dengan Pascal (Lexical Analyzer using Pascal)

Posted by Jujur Sitanggang on 3:56 PM

program Scan;
Uses crt;
Var
x : text;
y : Char;
token,Ttoken : String;

procedure input;
begin
read(x,y);
end;

procedure output;
begin
writeln(token:10,' ',Ttoken);
end;

procedure scaning;
begin
clrscr;
assign(x,'tespas.pas');
reset(x);
repeat
input;

if (y in['A'..'Z','a'..'z']) then
begin
repeat
begin
token := token + y;
input;
end;
until (not(y in['A'..'Z','a'..'z']));
Ttoken:='stringt';
output;
token := '';
end;

if (y in['0'..'9']) then
begin
repeat
begin
token := token + y;
input;
end;
until (not(y in['0'..'9']));
Ttoken := 'intcon';
output;
token := '';
end;

if (y in[':','=',';','.']) then
begin
repeat
begin
token := token + y;
input;
end;
until (not(y in[':','=',';','.']));
if token =':=' then
Ttoken := 'becomes';
if token ='=' then
Ttoken := 'egl';
if token ='.' then
Ttoken := 'period';
if token =';' then
Ttoken := 'semicolon';
if token =':' then
Ttoken := 'colon';
output;
token := '';
end;

if (y in['+','-','*','/']) then
begin
repeat
begin
token := token + y;
input;
end;
until (not(y in['+','-','*','/']));
if token ='+' then
Ttoken := 'plus';
if token ='-' then
Ttoken := 'minus';
if token ='*' then
Ttoken := 'times';
if token ='/' then
Ttoken := 'div';
output;
token := '';
end;

if (y in['<','>','=']) then
begin
repeat
begin
token := token + y;
input;
end;
until (not(y in['<','>','=']));
if token ='<' then
Ttoken := 'lss';
if token ='>' then
Ttoken := 'gtr';
if token ='<=' then
Ttoken := 'leg';
if token ='>=' then
Ttoken := 'geg';
token := '';
end;

if (y in['(',')','[',']']) then
begin
repeat
begin
token := token + y;
input;
end;
until (not(y in['(',')','[',']']));
if token ='(' then
Ttoken := 'lparent';
if token =')' then
Ttoken := 'rparent';
if token ='[' then
Ttoken := 'lbrack';
if token =']' then
Ttoken := 'rbrack';
token := '';
end;

until y = '#';
end;
begin
scaning;
readln;
end.


 Buat File Scan.txt di Foleder yang sama

0 Comments

Copyright Jujur Soaloon Sitangang Lipan All rights reserved. Theme by Sitanggang. | Bloggerized by Soalparna.