This Pascal source code program implements a simple scanner that reads a Pascal source code file, identifies the tokens (keywords, identifiers, operators, etc.) and writes the results to an output file. It uses procedures to read the input file, check each character, identify the tokens, and write the output. The main program calls the scanning procedure to analyze the input file and output the results.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
55 views3 pages
Listing Program Scanner
This Pascal source code program implements a simple scanner that reads a Pascal source code file, identifies the tokens (keywords, identifiers, operators, etc.) and writes the results to an output file. It uses procedures to read the input file, check each character, identify the tokens, and write the output. The main program calls the scanning procedure to analyze the input file and output the results.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
1 { Program : Scanner Sederhana untuk Source Program Pascal }
2 { Created : 04/12/2018 ILMU KOMPUTER UIN-SU }
3 4 program scanner; 5 6 var 7 prg_sumber,f_hasil:text; 8 karakter:Char; 9 kata,Nm_token:string; 10 11 procedure bacafile; { prosedur membaca file sumber } 12 begin 13 read(prg_sumber,karakter); 14 end; 15 16 procedure hasil; {prosedur utk menulis hasil setiap token} 17 begin 18 append(f_hasil); {menambah hasil scan ke file hasil} 19 writeln(f_hasil, kata : 15, ' : ', Nm_token); 20 writeln(kata : 15, ' : ', Nm_token); {mencetak hasil scan ke layar} 21 end; 22 23 function cekkeyword(s:string):string; {fungsi mengecek keyword atau bukan} 24 var 25 x : byte; 26 panjang : integer; 27 ftext : text; 28 data : string; 29 30 begin 31 panjang:= length(s); {konversi kata ke huruf kecil} 32 for x:=1 to panjang do 33 begin 34 if s[x]=upcase (s[x]) then 35 s[x] := s[x] 36 else 37 s[x]:= chr(ord(s[x])+32); 38 end; 39 40 assign(ftext,'keyword.txt'); 41 reset(ftext); 42 while not eof(ftext) do {mencocokkan dgn tabel keyword} 43 begin 44 readln(ftext,data); 45 if s=data then Nm_token:='Identifier / Keyword'; 46 end; 47 close(ftext); 48 end; 49 50 procedure periksa; {prosedur memeriksa setiap karakter file sumber} 51 begin 52 while not eof(prg_sumber) do {kerjakan sampai akhir file} 53 begin 54 bacafile; 55 if karakter ='{' then {mengabaikan/membuang komentar} 56 begin 57 repeat 58 begin 59 bacafile; 60 end; 61 until karakter = '}'; 62 kata:=''; 63 end; 64 65 if karakter = chr(39) then {mengecek tanda petik (‘)} 66 begin 67 repeat 68 begin 69 kata := kata + karakter; 70 bacafile; 71 end; 72 until karakter = chr(39) ; 73 kata := kata + karakter; 74 Nm_token := 'Konstanta'; 75 hasil; 76 kata :=''; 77 end; 78 79 if (karakter in['A'..'Z','a'..'z','_']) then {mengecek karakter/kata} 80 begin 81 repeat 82 begin 83 kata := kata + karakter; 84 bacafile; 85 end; 86 until (not(karakter in['A'..'Z','a'..'z','_'])); 87 Nm_token:='Identifier / Variabel'; 88 cekkeyword(kata); 89 hasil; 90 kata :=''; 91 end; 92 93 if (karakter in['0'..'9']) then {mengecek angka} 94 begin 95 repeat 96 begin 97 kata := kata + karakter; 98 bacafile; 99 end; 100 until (not(karakter in['0'..'9'])); 101 Nm_token := 'Konstanta / Integer'; 102 hasil; 103 kata :=''; 104 end; 105 106 if (karakter in['+','-','*','/',',','^']) then 107 begin 108 repeat 109 begin 110 kata := kata+karakter; 111 bacafile; 112 end; 113 until (not(karakter in['+','-','*','/',',','^'])); 114 Nm_token :='Operator'; 115 hasil; 116 kata:=''; 117 end; 118 119 if (karakter in['(',')','[',']','^',':',';',',','.']) then 120 begin 121 kata:=karakter; 122 Nm_token :='Delimiter'; 123 hasil; 124 kata:=''; 125 end; 126 end; 127 end; 128 129 begin {=program utama=} 130 Writeln('Contoh Program Scanner PASCAL '); 131 assign(prg_sumber,'URUT.PAS'); {menetapkan file sumber} 132 reset(prg_sumber); {membaca file sumber} 133 assign(f_hasil,'hasil.txt'); {menetapkan file hasil} 134 rewrite(f_hasil); {menghapus isi file hasil} 135 periksa; {menjalankan prosedur scan} 136 close(prg_sumber); {menutup file} 137 close(f_hasil); 138 readln; 139 end.