博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

hope_process

I think ,therefore I am.
  heixia108.cublog.cn

关于作者
    既然目标是地平线

   留给世界的只能是背影
   
|| << >> ||
我的分类


tiger(2) 词法分析
该词法分析能处理嵌套注释

"/*" { 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
下载:下载
               

发表于: 2008-03-19,修改于: 2008-04-08 22:46,已浏览163次,有评论0条 推荐 投诉


网友评论
 发表评论