
(* lexical analyzer template (TP Lex V3.0), V1.0 3-2-91 AG *)

(* global definitions: *)
USES lexlib ;
        (*
        TP2 exercice 3 : compter le nombre de mots, de lettres et
                     de lignes d'un fichier
        *)
VAR nblignes,nblettres,nbmots,nbcar : integer;
fichier:string;

function yylex : Integer;

procedure yyaction ( yyruleno : Integer );
  (* local definitions: *)
begin
  (* actions: *)
  case yyruleno of
  1:
          begin inc(nbmots);inc(nblettres,yyleng); end;
  2:
  inc(nbcar);
  3:
   inc(nblignes); (* rq: Lex ne distingue pas blabla\n de blabla
                     en fin de fichier *)
  end;
end(*yyaction*);

(* DFA table: *)

type YYTRec = record
                cc : set of Char;
                s  : Integer;
              end;

const

yynmarks   = 5;
yynmatches = 5;
yyntrans   = 8;
yynstates  = 6;

yyk : array [1..yynmarks] of Integer = (
  { 0: }
  { 1: }
  { 2: }
  1,
  2,
  { 3: }
  2,
  { 4: }
  3,
  { 5: }
  1
);

yym : array [1..yynmatches] of Integer = (
{ 0: }
{ 1: }
{ 2: }
  1,
  2,
{ 3: }
  2,
{ 4: }
  3,
{ 5: }
  1
);

yyt : array [1..yyntrans] of YYTrec = (
{ 0: }
  ( cc: [ #1..#9,#11..'@','['..'`','{'..#255 ]; s: 3),
  ( cc: [ #10 ]; s: 4),
  ( cc: [ 'A'..'Z','a'..'z' ]; s: 2),
{ 1: }
  ( cc: [ #1..#9,#11..'@','['..'`','{'..#255 ]; s: 3),
  ( cc: [ #10 ]; s: 4),
  ( cc: [ 'A'..'Z','a'..'z' ]; s: 2),
{ 2: }
  ( cc: [ 'A'..'Z','a'..'z' ]; s: 5),
{ 3: }
{ 4: }
{ 5: }
  ( cc: [ 'A'..'Z','a'..'z' ]; s: 5)
);

yykl : array [0..yynstates-1] of Integer = (
{ 0: } 1,
{ 1: } 1,
{ 2: } 1,
{ 3: } 3,
{ 4: } 4,
{ 5: } 5
);

yykh : array [0..yynstates-1] of Integer = (
{ 0: } 0,
{ 1: } 0,
{ 2: } 2,
{ 3: } 3,
{ 4: } 4,
{ 5: } 5
);

yyml : array [0..yynstates-1] of Integer = (
{ 0: } 1,
{ 1: } 1,
{ 2: } 1,
{ 3: } 3,
{ 4: } 4,
{ 5: } 5
);

yymh : array [0..yynstates-1] of Integer = (
{ 0: } 0,
{ 1: } 0,
{ 2: } 2,
{ 3: } 3,
{ 4: } 4,
{ 5: } 5
);

yytl : array [0..yynstates-1] of Integer = (
{ 0: } 1,
{ 1: } 4,
{ 2: } 7,
{ 3: } 8,
{ 4: } 8,
{ 5: } 8
);

yyth : array [0..yynstates-1] of Integer = (
{ 0: } 3,
{ 1: } 6,
{ 2: } 7,
{ 3: } 7,
{ 4: } 7,
{ 5: } 8
);


var yyn : Integer;

label start, scan, action;

begin

start:

  (* initialize: *)

  yynew;

scan:

  (* mark positions and matches: *)

  for yyn := yykl[yystate] to     yykh[yystate] do yymark(yyk[yyn]);
  for yyn := yymh[yystate] downto yyml[yystate] do yymatch(yym[yyn]);

  if yytl[yystate]>yyth[yystate] then goto action; (* dead state *)

  (* get next character: *)

  yyscan;

  (* determine action: *)

  yyn := yytl[yystate];
  while (yyn<=yyth[yystate]) and not (yyactchar in yyt[yyn].cc) do inc(yyn);
  if yyn>yyth[yystate] then goto action;
    (* no transition on yyactchar in this state *)

  (* switch to new state: *)

  yystate := yyt[yyn].s;

  goto scan;

action:

  (* execute action: *)

  if yyfind(yyrule) then
    begin
      yyaction(yyrule);
      if yyreject then goto action;
    end
  else if not yydefault and yywrap then
    begin
      yyclear;
      return(0);
    end;

  if not yydone then goto start;

  yylex := yyretval;

end(*yylex*);


begin

(* sous l'Çditeur Turbo Pascal, tapez Ctrl-F1
sur les mots-clefs qui vous sont inconnus
comme pour paramcount ci-dessus *)

if paramcount<>1 then writeln('Tapez tp2ex3 nomfichier')
else begin
     fichier:=paramstr(1);
	nblettres:=0;
	nbmots:=0;
	nblignes:=0;
        nbcar:=0;

	assign(yyinput,fichier);
	reset(yyinput);

	yylex;
	writeln('analyse du fichier ', fichier);

	writeln('nb lettres : ',nblettres);
	writeln('nb mots : ',nbmots);
	writeln('nb lignes : ',nblignes);
        writeln('nb caracteres non alphabetiques : ',nbcar);
        writeln('taille : ',nbcar+nblettres+nbmots+2*nblignes);
     end
end.
