qq_17001551 2015-10-27 01:42 采纳率: 0%
浏览 1831

编译程序原理 lex程序 如何获取当前token的行

代码如下:
test.l的代码

%{
/* some initial C programs */ 
#define BEGINSYM 1 
#define INTEGER 2 
#define IDNAME 3 
#define REAL 4 
#define SEMICOLONSYM 5
#define ASSIGNSYM 6 
#define STRING 7
%} 
/* regular definitions */
Digit [0-9]
Letter [a-zA-Z]
IntLit {Digit}+
Id {Letter}({Letter}|{Digit}|_)*
%%
[ \t\n]              {/* skip white spaces */}
[Bb][Ee][Gg][Ii][Nn] {return(BEGINSYM);}
{IntLit}             {return(INTEGER);}
{Id} {
       printf("var has %d characters, ",yyleng);
       return(IDNAME);
     }
({IntLit}[.]{IntLit})([Ee][+-]?{IntLit})? {return(REAL);}
\"[^\"\n]*\" {stripquotes(); return(STRING);}
";"          {return(SEMICOLONSYM);}
":="         {return(ASSIGNSYM);}
%%
/* some final C programs */
stripquotes()
{
  /* handling string within a quoted string */
  int frompos, topos=0, numquotes = 2;
  for(frompos=1; frompos<yyleng; frompos++){
    yytext[topos++] = yytext[frompos];
  }
  yyleng -= numquotes;
  yytext[yyleng] = '\0';
}
void main(){
  int i;
  i = yylex();
  while (i>0 && i < 8) {
    printf("<%s> is %d\n",yytext,i);
    i = yylex(); 
  }
}

代码主要是识别下面文件的token 的值以及对应number,

测试的text如下

begin
123.4 321.4E21
x:=365;
"this is a string"

最后执行lex程序档
输出如下结果:
图片说明

上面token以及对应的token number有了,但是我要输出当前token所在的文本的行数 请问我要怎么做呢? 谢谢
编译程序原理

  • 写回答

1条回答 默认 最新

  • baidu_32364343 2015-10-27 15:23
    关注

    你是不是yzu的??

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置