Server for Information Technologies ������ ��������������
������� �������������� ����������
(095) 932-9212, 932-9213, 939-0783
E-mail: [email protected]
������ �������� ����(!) ������������� ���������� CIT Forum CD-ROM

LEX(1)

��������
lex - ��������� �������� �������� ������������ �������

���������

   lex  [-r] [-c] [-t] [-v] [-n] [���� ...]

��������
������� lex ���������� ���������, ������� ����� ������������ ��� �������� ������������ ������� ������.

������� ����� (�� ��������� - ����������� ����) �������� ������� �������� � ���������, ���������� �������������, � ��������� �� ����� C, ������� ����������� � ������ ������ �������������.

������������ ��������� lex.yy.c. ������ ����������� ������ � �����������, ��� ��� ������ ������������ ������� ��������� ��������������� C-���������, � ��������� ��������� �������� ����� �������� � �������� ���� ��� ���������. ������������ ������� ���������� �� ������� ���������� ������ yytext. ������������� ������� ����������� � ��� �������, � ������� ��� ������� � �����. ������� �������� ����� ��������� ���������� ������, ������� ��������� ������ �������� (��������, [abx-z] �������� a, b, x, y � z), � ��������� *, + � ?, ������������ ���������� ����������� ������� ��� ������ ��������, ��������������, ����� ��������������� ����� ���, ����� ������������� �����, 0 ��� ���� ���. ������ . - ��� ����� ���� �������� ASCII, ����� �������� ������. � ����������, �������������� ������ (����������� ��������) � ������������ ����� (�����������). ������ r{d,e} � ������� ���������� ��������� ����������� ��������� r ����� d � e. ��� ����� ������� ���������, ��� |, �� �������, ��� *, ?, + � ������������. ������ ^ � ������ ��������� ��������, ��� ������������� ������ ���������� � ������ ������, ������ $ � ����� ��������� �������, ����� ��������� �������� � �������������� ������� ��� ������ �������� ������. ������ / � ��������� ����������, ��� ������ ����� ��������� ������ �� ��������� ����� ���������� � yytext, �� ���������� ����� ������ ������� ��������������. ������ ��������� ����� �������������� ��� ������� ������, ���� �� ���� � ������� ��� ���� ��� ������������ \. ������: ��������� [a-zA-Z]+ ������� �������������� � �������� ����.

��� ������������ ���������� ��� �������: input( ) - ��������� ������; unput(c) - �������� ����������� ������; output(c) - ��������� �������� ������. ��� ��� ���������� � �������� ����������� �������, �� �� �������� ����� ��������. ������������ ���������-���������� ���������� yylex( ), ���������� �������� ��������� main( ), ������� �������� ����������. �������� REJECT � ������ ����� ������� ���������� ����� �� �������� ������������� � ������� � ������ ���������� �������������; ������� yymore( ) ����������� �������������� ������� � ��� �� ������� yytext; ������� yyless(p) ����������� ������ �������������� ������� ��������, ������� � ������� p, ������� ������ ���� ����� yytext � yytext+yyleng. ������� input � output ���������� ����� yyin � yyout ��� ������ � ������ (�� ��������� ��� ����� stdin � stdout ��������������).

������, ������������ � �������, ��������� ������� �� ����� C � ���������� � ���� lex.yy.c; ���� ��� ������������ ������ %%, ����������� ����������� � ������� ������� ����������� ����� lex.yy.c. ��� �������, ��� � � YACC, ������ ������ ����� %%. ������, ������� �� %% � ������������ �� � �������, ���������� ������� ����� ������� �������� ��� ������� ������; ������ ����������� ����� ����� ������������, ������ ��� ������� � �������� ������� { }. �������, ��� ����� ����������� ������ ��������.

������

     D       [0-9]
     %%
     if      printf("IF statement\n");
     [a-z]+  printf("tag, value %s\n",yytext);
     0{D}+   printf("octal number %s\n",yytext);
     {D}+    printf("decimal number %s\n",yytext);
     "++"    printf("unary op\n");
     "+"     printf("binary op\n");
     "/*"     skipcommnts();
     %%
      skipcommnts()
      {
        for(;;) {
          while (input() != '*')
            ;
          if (input() != '/')
            unput(yytext[yyleng-1]);
          else
            return;
        }
      }
������� �����, ��������������� ���������� lex, ������ ���������� � �������� yy ��� YY.

�����
-r ����������� �������� �������� �� ����� ratfor [��. ratfor(1)].
-c ����������� �������� �������� �� ����� C (������ ����� ��������� �������������� �� ���������).
-t ��������� ��������� �� ����������� ����� ������ ����� lex.yy.c.
-v ������ �������������� ������ � ����������� ��������.
-n �������� ������ ����������.

���� ������� ��������� ������, ��� ��������������� ��� ���� ����� ����. ���� ����� �� �������, �������������� ����������� ����.

������� ��������� ������ ��� ������������� ��������� �������� ����� ���� ����������� � ������� �����������:
%p n ����� ������� ����� n (�� ��������� - 2500)
%n n ����� ��������� (500)
%e n ����� ����� � ������ ������� (1000)
%a n ����� ��������� (2000)
%k n ����� ������� ����������� �������� (1000)
%o n ������ ��������� ������� (3000)

������������� ������ ��� �������� ����� ����� ����������� ������������� ������������� ������� ����� -v, ���� �� ������������ ����� -n.

��. �����
yacc(1).

��������
����� -r ��� �� ��������� ��������������.
Comments: [email protected]
Designed by Andrey Novikov
Copyright © CIT