Chinaunix首页 | 论坛 | 博客
  • 博客访问: 724108
  • 博文数量: 251
  • 博客积分: 10367
  • 博客等级: 上将
  • 技术积分: 2750
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-10 14:43
文章分类

全部博文(251)

文章存档

2009年(2)

2008年(86)

2007年(163)

分类: C/C++

2008-03-19 16:28:42

该词法分析能处理嵌套注释

"/*" { char c;
           int num = 1;
           int n = 0;
           while( num ){
                c = input( );
                n ++;
                if( c == '/' ){
                     while( ( c = input( ) ) == '/' )
                          ;
                     if( c == '*' )
                          num ++;
                }
                else if( c == '*' ){
                     while( ( c = input( ) ) == '*' )
                          ;
                     if( c == '/' )
                          num --;
                }
                if( c == '\n' )
                     EM_newline();
           }
           adjust();
           charPos += n;
           continue;
        }


测试结果:

[heixia@localhost chap2]$ ./lextest test1.tig
       LET 43
      TYPE 48
        ID 52 arrtype
        EQ 59
     ARRAY 60
        OF 65
        ID 67 int
       VAR 72
        ID 75 arr1
     COLON 79
        ID 80 arrtype
    ASSIGN 87
        ID 89 arrtype
    LBRACK 96
       INT 97 10
    RBRACK 99
        OF 100
       INT 102 0
        IN 104
        ID 108 arr1
       END 113


原文件:

/* an array type and an array variable*/
let
    type arrtype = array of int
    var arr1:arrtype := arrtype [10] of 0
in
    arr1
end


文件:词法分析.rar
大小:63KB
下载:下载
               
阅读(653) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~