如何使用Flex和YACC实现#include构造?

Goz*_*zzy 12 parsing yacc lex include

在解析期间,如果遇到包含令牌,我想指示YACC打开指定为输入的文件并开始解析它.解析完成后,我想指示YACC返回文件并在include表达式后直接解析.我将限制包含深度级别设置为1.

小智 8

flex手册介绍了如何使用yypush_buffer_state()和yypop_buffer_state()执行此操作.

以下是使用多个输入缓冲区的官方手册部分.有一些示例代码.


Dig*_*oss 5

在处理器的词法和句法阶段之间进行通信是正常的.

因此,在解析器中识别include指令的语法(或者,为了使事情变得更容易,只需在词法分析器中识别它)并在词法层面进行切换.

例如,这里是一个简单的语言识别包含标准输入线abcd.file.当它看到.someStringsomeString作为包含文件打开然后返回读取标准输入.

%{
#include <stdio.h>
#include <stdlib.h>
void start_include(char *); int yylex(void); void yyerror(void *);
#define YYSTYPE char *
%}
%%
all:          all others | others;
others:       include_rule | rule_1 | rule_2 | 'Z' { YYACCEPT; };
include_rule: '.' '\n' { start_include($1); };
rule_1:       'a' 'b' '\n' { printf("random rule 1\n"); };
rule_2:       'c' 'd' '\n' { printf("random rule 2\n"); };
%%
FILE * f = NULL;
void start_include(char *s) {
        if ((f = fopen(s, "r")) == NULL)
                abort();
}
int yylex(void) {
        int c;
        static char s[100];
        if (f == NULL)
                f = stdin;
        c = getc(f);
        if (c == EOF) {
                f = stdin;
                c = getc(f);
        }
        if (c == '.') {
                scanf(" %s", s);
                yylval = s;
        } else if (c == EOF)
                return 'Z';
        return c;
}
Run Code Online (Sandbox Code Playgroud)

当我们运行它时......

$ cat > toplevel
ab
.myinclude
ab
$ cat > myinclude
cd
cd
$ yacc ip.y && cc -Wall y.tab.c -ly && ./a.out < toplevel
random rule 1
random rule 2
random rule 2
random rule 1
$ 
Run Code Online (Sandbox Code Playgroud)