在lex中解析注释时出错 [英] Error while parsing comments in lex

查看:200
本文介绍了在lex中解析注释时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Lex代码:

identifier [\._a-zA-Z0-9\/]+
comment "//"

<*>{comment}  {
    cout<<"Comment\n";
  char c;
  while((c= yyinput()) != '\n')
    {
    }
}

<INITIAL>{s}{e}{t} {
   BEGIN(SAMPLE_STATE);
   return SET;
}

<SAMPLE_STATE>{identifier} {
    strncpy(yylval.str, yytext,1023);
    yylval.str[1023] = '\0';
  return IDENTIFIER;
}

在上面的lex代码中,当解析"//set name"时没有错误.请注意所分析句子中"//"后面的空格.但是,当解析"//set name"时,报告错误.您能指出我要去哪里了吗?谢谢.

In the above lex code, there is no error when "// set name" is parsed. Please notice the space after "//" in the sentence parsed. However, when "//set name" is parsed, there is an error reported. Could you point to where I am going wrong? Thanks.

该错误已由yyerror捕获并报告

SampleParser.y:43: int CMTSTapTestSeq_yyerror(char*): Assertion `0 && "Error parsing Sample file\n"' failed. 

这个断言是我添加的.

推荐答案

我认为您在简化示例时犯了一个错误,因为您提供的代码可以正常工作,并且没有您指出的错误.我对其进行了编码和测试(为方便起见,我使用了 C 而不是C ++).但是,我看到您发布了一个稍后的问题,其中包含更多代码,可以更好地解释该问题.我也回答了那个.

I think you have made a mistake in simplifying your example, as the code you supplied works fine and does not have the fault you indicated. I coded it up and tested it (I used C instead of C++ for convenience). However, I see you posted a later question with more code that explained the problem better. I answered that one also.

s s
e e
t t
identifier [\._a-zA-Z0-9\/]+
comment "//"

%s SAMPLE_STATE

%{
//#include <iostream>
//using namespace std;
#include <stdio.h>
#define SET 1
#define IDENTIFIER 2
#define yyinput input
%}

%%
<*>{comment}  {
   // cout<<"Comment\n";
   printf("Comment\n");
  char c;
  while((c= yyinput()) != '\n')
    {
    }
}

<INITIAL>{s}{e}{t} {
   BEGIN(SAMPLE_STATE);
   //return SET;
   printf("SET\n");
}

<SAMPLE_STATE>{identifier} {
    //strncpy(yylval.str, yytext,1023);
    //yylval.str[1023] = '\0';
  //return IDENTIFIER;
  printf("identifier");
}

接受两者:

//set name
// set name

这篇关于在lex中解析注释时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆