如何使用缩进作为块分隔符与bison和flex

问题描述 投票:10回答:4

我想知道如何在bison + flex中将缩进实现为块分隔符。就像在python中一样。我正在编写自己的编程语言(主要是为了好玩,但我打算将它与游戏引擎一起使用),我将尝试提出一些特殊的东西,以最小化样板并最大化开发速度。

我已经在C中编写了一个编译器(实际上是一个'langToy'到Nasm翻译器)但是失败了。由于某种原因,它只能处理整个源文件中的一个字符串(好吧,我已经醒了超过48小时 - 所以...你知道,脑子崩溃了)。

我不知道大括号和/或开始 - >结尾是否更容易实现(我没有问题)或者只是我的大脑锁定了。

提前致谢!


更新:好的,我不知道如何使用flex进行操作。将多个DEDENT返回给解析器时遇到问题。 Flex / Bison对我来说相对较新。


更新2:这是我到目前为止提出的flex文件;它不太明白:

%x t
%option noyywrap

%{
  int lineno = 0, ntab = 0, ltab = 0, dedent = 0;
%}

%%

<*>\n  { ntab = 0; BEGIN(t); }
<t>\t  { ++ntab; }
<t>.   { int i; /* my compiler complains not c99 if i use for( int i=0... */
         if( ntab > ltab )
           printf("> indent >\n");
         else if( ntab < ltab )
           for( i = 0; i < ltab - ntab; i++ )
             printf("< dedent <\n");
         else
           printf("=        =\n");

         ltab = ntab; ntab = 0;
         BEGIN(INITIAL);
         /* move to next rule */
         REJECT;}
.    /* ignore everything else for now */

%%

main()
{
  yyin = fopen( "test", "r" );
  yylex();
}

你可以试着玩弄它,也许你会看到我所缺少的东西。返回多个dedents对Haxe来说很容易(返回t_dedent(num);)。

此代码并不总是正确匹配缩进/ dedent。


更新3:我认为我会放弃对flex的希望,并以自己的方式去做,如果有人知道如何在flex中做到这一点,我会很高兴听到它。

compiler-construction bison flex-lexer
4个回答
14
投票

你需要做的是在每行的开头用flex计算空白量,并插入适当数量的INDENT / UNINDENT令牌,供解析器用来分组。一个问题是你想要做什么关于标签与空格 - 你只是想让它们与固定制表位相同,或者你是否想要缩进是一致的(所以如果一行开头是一个标签和下一个有空格,你发出错误信号,这可能有点困难)。

假设你想要固定的8列tabstops,你可以使用类似的东西

%{
/* globals to track current indentation */
int current_line_indent = 0;   /* indentation of the current line */
int indent_level = 0;          /* indentation level passed to the parser */
%}

%x indent /* start state for parsing the indentation */
%s normal /* normal start state for everything else */

%%
<indent>" "      { current_line_indent++; }
<indent>"\t"     { current_line_indent = (current_line_indent + 8) & ~7; }
<indent>"\n"     { current_line_indent = 0; /*ignoring blank line */ }
<indent>.        {
                   unput(*yytext);
                   if (current_line_indent > indent_level) {
                       indent_level++;
                       return INDENT;
                   } else if (current_line_indent < indent_level) {
                       indent_level--;
                       return UNINDENT;
                   } else {
                       BEGIN normal;
                   }
                 }

<normal>"\n"     { current_line_indent = 0; BEGIN indent; }
... other flex rules ...

您必须确保以缩进模式启动解析(以获取第一行的缩进)。


5
投票

克里斯的答案在可行的解决方案方面走了很长的路,感谢一大堆!不幸的是,它缺少了我需要的一些更重要的方面:

  • 多个例外(无意)同时出现。在调用baz之后,请考虑以下代码应该发出两个outdents: def foo(): if bar: baz()
  • 到达文件末尾并且仍处于某些缩进级别时发出异常。
  • 不同大小的缩进级别。 Chris的当前代码仅适用于1空格缩进。

基于Chris的代码,我提出了一个解决方案,该解决方案适用于我目前遇到的所有情况。我创建了一个模板项目,用于在github:https://github.com/lucasb-eyer/flex-bison-indentation上使用flex(和bison)解析基于缩进的文本。它是一个完全工作(基于CMake)的项目,它还跟踪当前令牌的行位置和列范围。

为了防止链接因任何原因而中断,这里是词法分析器的内容:

#include <stack>

int g_current_line_indent = 0;
std::stack<size_t> g_indent_levels;
int g_is_fake_outdent_symbol = 0;

static const unsigned int TAB_WIDTH = 2;

#define YY_USER_INIT { \
    g_indent_levels.push(0); \
    BEGIN(initial); \
}
#include "parser.hh"

%}

%x initial
%x indent
%s normal

%%
    int indent_caller = normal;

 /* Everything runs in the <normal> mode and enters the <indent> mode
    when a newline symbol is encountered.
    There is no newline symbol before the first line, so we need to go
    into the <indent> mode by hand there.
 */
<initial>.  { set_yycolumn(yycolumn-1); indent_caller = normal; yyless(0); BEGIN(indent); }
<initial>\n { indent_caller = normal; yyless(0); BEGIN(indent); }    

<indent>" "     { g_current_line_indent++; }
<indent>\t      { g_current_line_indent = (g_current_line_indent + TAB_WIDTH) & ~(TAB_WIDTH-1); }
<indent>\n      { g_current_line_indent = 0; /* ignoring blank line */ }
<indent><<EOF>> {
                    // When encountering the end of file, we want to emit an
                    // outdent for all indents currently left.
                    if(g_indent_levels.top() != 0) {
                        g_indent_levels.pop();

                        // See the same code below (<indent>.) for a rationale.
                        if(g_current_line_indent != g_indent_levels.top()) {
                            unput('\n');
                            for(size_t i = 0 ; i < g_indent_levels.top() ; ++i) {
                                unput(' ');
                            }
                        } else {
                            BEGIN(indent_caller);
                        }

                        return TOK_OUTDENT;
                    } else {
                        yyterminate();
                    }
                }

<indent>.       {
                    if(!g_is_fake_outdent_symbol) {
                        unput(*yytext);
                    }
                    g_is_fake_outdent_symbol = 0;
                    // -2: -1 for putting it back and -1 for ending at the last space.
                    set_yycolumn(yycolumn-1);

                    // Indentation level has increased. It can only ever
                    // increase by one level at a time. Remember how many
                    // spaces this level has and emit an indentation token.
                    if(g_current_line_indent > g_indent_levels.top()) {
                        g_indent_levels.push(g_current_line_indent);
                        BEGIN(indent_caller);
                        return TOK_INDENT;
                    } else if(g_current_line_indent < g_indent_levels.top()) {
                        // Outdenting is the most difficult, as we might need to
                        // outdent multiple times at once, but flex doesn't allow
                        // emitting multiple tokens at once! So we fake this by
                        // 'unput'ting fake lines which will give us the next
                        // outdent.
                        g_indent_levels.pop();

                        if(g_current_line_indent != g_indent_levels.top()) {
                            // Unput the rest of the current line, including the newline.
                            // We want to keep it untouched.
                            for(size_t i = 0 ; i < g_current_line_indent ; ++i) {
                                unput(' ');
                            }
                            unput('\n');
                            // Now, insert a fake character indented just so
                            // that we get a correct outdent the next time.
                            unput('.');
                            // Though we need to remember that it's a fake one
                            // so we can ignore the symbol.
                            g_is_fake_outdent_symbol = 1;
                            for(size_t i = 0 ; i < g_indent_levels.top() ; ++i) {
                                unput(' ');
                            }
                            unput('\n');
                        } else {
                            BEGIN(indent_caller);
                        }

                        return TOK_OUTDENT;
                    } else {
                        // No change in indentation, not much to do here...
                        BEGIN(indent_caller);
                    }
                }

<normal>\n    { g_current_line_indent = 0; indent_caller = YY_START; BEGIN(indent); }

1
投票

如果你使用一个去除所有空格的标记器(使用只是分隔标记),那么使用curly括号(等等)会更简单。有关python标记化的一些想法,请参阅this page(“编译器如何解析缩进?”一节)。

如果您在解析之前没有进行标记化,那么可能还有其他工作要做,这取决于您构建解析器的方式。


0
投票

您需要一个类似于此的规则(假设您使用制表符进行缩进):

\ t:{return TABDENT; }

坦率地说,我总是发现大括号(或开头/结尾)更易于编写和阅读,无论是作为人类还是作为词法分析器/解析器编写者。

© www.soinside.com 2019 - 2024. All rights reserved.