首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C/C++嵌套`#include“报头”语法的解释?

C/C++嵌套`#include“报头”语法的解释?
EN

Stack Overflow用户
提问于 2020-01-16 06:59:47
回答 1查看 109关注 0票数 0

我正在研究lex/flex中的开始状态和嵌套输入文件

flex和bison一书中,我对C/C++ #include "Header"语法的示例实现感到困惑:

下面是示例lex的一部分:

代码语言:javascript
复制
/* Companion source code for "flex & bison", published by O'Reilly
 * Media, ISBN 978-0-596-15597-1
 * Copyright (c) 2009, Taughannock Networks. All rights reserved.
 * See the README file for license conditions and contact info.
 * $Header: /home/johnl/flnb/code/RCS/fb2-3.l,v 2.3 2010/01/04 02:43:58 johnl Exp $
 */

/* fb2-3 skeleton for include files */

%option noyywrap warn nodefault
%x IFILE
  struct bufstack {
    struct bufstack *prev;  /* previous entry */
    YY_BUFFER_STATE bs;     /* saved buffer */
    int lineno;         /* saved line number */
    char *filename;     /* name of this file */
    FILE *f;            /* current file */
  } *curbs = 0;

  char *curfilename;        /* name of current input file */

  int newfile(char *fn);
  int popfile(void);

%%
^"#"[ \t]*include[ \t]*[\"<] { BEGIN IFILE; }

<IFILE>[^ \t\n\">]+          { 
                             { int c;
                   while((c = input()) && c != '\n') ;
                 }
                 yylineno++;
                 if(!newfile(yytext))
                                yyterminate(); /* no such file */
                 BEGIN INITIAL;
                           }

<IFILE>.|\n                { fprintf(stderr, "%4d bad include line\n", yylineno);
                     yyterminate();
               }
^.                         { fprintf(yyout, "%4d %s", yylineno, yytext); }
^\n                        { fprintf(yyout, "%4d %s", yylineno++, yytext); }
\n                         { ECHO; yylineno++; }
.                          { ECHO; }
<<EOF>>                    { if(!popfile()) { fprintf(yyout, "end of file, total lines: %4d %s", yylineno, yytext); yyterminate();}  }
%%

main(int argc, char **argv)
{
  if(argc < 2) {
    fprintf(stderr, "need filename\n");
    return 1;
  }
  if(newfile(argv[1]))
    yylex();
}

int
  newfile(char *fn)
{
  FILE *f = fopen(fn, "r");
  struct bufstack *bs = malloc(sizeof(struct bufstack));

  /* die if no file or no room */
  if(!f) { perror(fn); return 0; }
  if(!bs) { perror("malloc"); exit(1); }

  /* remember state */
  if(curbs)curbs->lineno = yylineno;
  bs->prev = curbs;

  /* set up current entry */
  bs->bs = yy_create_buffer(f, YY_BUF_SIZE);
  bs->f = f;
  bs->filename = fn;
  yy_switch_to_buffer(bs->bs);
  curbs = bs;
  yylineno = 1;
  curfilename = fn;
  return 1;
}

int
  popfile(void)
{
  struct bufstack *bs = curbs;
  struct bufstack *prevbs;

  if(!bs) return 0;

  /* get rid of current entry */
  fclose(bs->f);
  yy_delete_buffer(bs->bs);

  /* switch back to previous */
  prevbs = bs->prev;
  free(bs);

  if(!prevbs) return 0;

  yy_switch_to_buffer(prevbs->bs);
  curbs = prevbs;
  yylineno = curbs->lineno;
  curfilename = curbs->filename;
  return 1; 
}

请帮助我回答以下问题:

Header

  • Why的
  1. 为什么<IFILE>[^ \t\n\">]+匹配">,使用{ int c; while((c = input()) && c != '\n') ; }在行结束之前吃掉所有的字符?yytext是否与Header文件名完全匹配?
  2. 如何实现像java import java.util.Decoder; ?

这样的语法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-16 07:10:56

为什么^t\n\“>+匹配标题的末尾”或>?

答案是:没有。

但是它所做的是匹配所有字符,直到这些字符(和空格、制表符和换行符)匹配为止,当您到达这些字符时,匹配就停止了。因此,当您有一个匹配并且执行规则的代码时,您知道匹配后的文件中的下一个字符必须是">或空格字符。

让我们举一个例子:

代码语言:javascript
复制
#include <foo/bar.h>

规则匹配#include <.

  • The规则<IFILE>[^ \t\n\">]+匹配foo/bar.h

当您运行代码时

代码语言:javascript
复制
int c;
while((c = input()) && c != '\n') ;

它将从读取结束的>开始,然后继续读取并丢弃所有剩余的字符,直到行尾。

要验证这一点,您可以在循环中添加一些字符的输出。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59764452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档