在另一个文件中使用lex生成的源代码

时间:2011-11-04 17:57:04

标签: c++ c parsing lex lexical-analysis

我想在另一个代码中使用lex生成的代码,但是我看到的所有示例都是将主函数嵌入到lex文件中而不是相反。

是否可以使用(包括)来自lex的c生成文件到其他具有类似内容的代码(不一定相同)?

#include<something>
int main(){
    Lexer l = Lexer("some string or input file");
    while (l.has_next()){
        Token * token = l.get_next_token();
        //somecode
    }
    //where token is just a simple object to hold the token type and lexeme
    return 0;
}

3 个答案:

答案 0 :(得分:7)

这就是我要开始的:
注意:这是使用C接口的示例
要使用C ++接口,请添加%option c++请参阅下面的

Test.lex

IdentPart1      [A-Za-z_]
Identifier      {IdentPart1}[A-Za-z_0-9]*
WHITESPACE      [ \t\r\n]

%option noyywrap

%%

{Identifier}      {return 257;}
{WHITESPACE}      {/* Ignore */}
.                 {return 258;}

%%

// This is the bit you want.
// It is best just to put this at the bottom of the lex file
// By default functions are extern. So you can create a header file with
// these as extern then included that header file in your code (See Lexer.h)
void* setUpBuffer(char const* text)
{
    YY_BUFFER_STATE buffer  = yy_scan_string(text);
    yy_switch_to_buffer(buffer);

    return buffer;
}

void tearDownBuffer(void* buffer)
{
    yy_delete_buffer((YY_BUFFER_STATE)buffer);
}

Lexer.h

#ifndef LOKI_A_LEXER_H
#define LOKI_A_LEXER_H

#include <string>

extern int   yylex();
extern char* yytext;
extern int   yyleng;

// Here is the interface to the lexer you set up above
extern void* setUpBuffer(char const* text);
extern void  tearDownBuffer(void* buffer);


class Lexer
{
    std::string         token;
    std::string         text;
    void*               buffer;
    public:
    Lexer(std::string const& t)
        : text(t)
    {
        // Use the interface to set up the buffer
        buffer  = setUpBuffer(text.c_str());
    }
    ~Lexer()
    {
        // Tear down your interface
        tearDownBuffer(buffer);
    }
    // Don't use RAW pointers
    // This is only a quick and dirty example.
    bool  nextToken()
    {
        int val = yylex();
        if (val != 0)
        {
            token = std::string(yytext, yyleng);
        }
        return val;
    }
    std::string const& theToken() const {return token;}
};

#endif

的main.cpp

#include "Lexer.h"
#include <iostream>

int main()
{
    Lexer l("some string or input file");


    // Did not like your hasToken() interface.
    // Just call nextToken() until it fails.
    while (l.nextToken())
    {
        std::cout << l.theToken() << "\n";
        delete token;
    }
    //where token is just a simple object to hold the token type and lexeme
    return 0;
}

构建

> flext test.lex
> g++ main.cpp  lex.yy.c
> ./a.out
some
string
or
input
file
>

或者,您可以使用C ++接口进行flex(其实验性)

test.lext

%option c++


IdentPart1      [A-Za-z_]
Identifier      {IdentPart1}[A-Za-z_0-9]*
WHITESPACE      [ \t\r\n]

%%

{Identifier}      {return 257;}
{WHITESPACE}      {/* Ignore */}
.                 {return 258;}

%%

// Note this needs to be here
// If you define no yywrap() in the options it gets added to the header file
// which leads to multiple definitions if you are not careful.
int yyFlexLexer::yywrap()   { return 1;}

的main.cpp

#include "MyLexer.h"
#include <iostream>
#include <sstream>

int main()
{
    std::istringstream  data("some string or input file");
    yyFlexLexer l(&data, &std::cout);


    while (l.yylex())
    {
        std::cout << std::string(l.YYText(), l.YYLeng()) << "\n";
    }
    //where token is just a simple object to hold the token type and lexeme
    return 0;
}

构建

> flex --header-file=MyLexer.h test.lex
> g++ main.cpp lex.yy.cc
> ./a.out
some
string
or
input
file
>

答案 1 :(得分:0)

不确定。我不确定生成的类;我们使用C生成 解析器,并从C ++中调用它们。或者您可以插入任何类型的包装器 你想要在lex文件中的代码,并从外面调用任何东西 生成的文件。

答案 2 :(得分:0)

关键字为%option reentrant%option c++

这里的例子是the ncr2a scanner

/** ncr2a_lex.l: Replace all NCRs by corresponding printable ASCII characters. */
%%
&#(1([01][0-9]|2[0-6])|3[2-9]|[4-9][0-9]); { /* accept 32..126 */
  /** `+2` skips '&#', `atoi()` ignores ';' at the end */
  fputc(atoi(yytext + 2), yyout); /* non-recursive version */
}

扫描仪代码可以保持不变。

这是使用它的程序:

/** ncr2a.c */
#include "ncr2a_lex.h"  

typedef struct {
  int i,j; /** put here whatever you need to keep extra state */
} State; 

int main () {
  yyscan_t scanner;
  State my_custom_data = {0,0};

  yylex_init(&scanner);
  yyset_extra(&my_custom_data, scanner);

  yylex(scanner);

  yylex_destroy(scanner);
  return 0;
}

构建ncr2a可执行文件:

flex -R -oncr2a_lex.c --header-file=ncr2a_lex.h ncr2a_lex.l 
cc -c -o ncr2a_lex.o ncr2a_lex.c
cc -o ncr2a ncr2a_lex.o ncr2a.c -lfl

实施例

$ echo 'three colons &#58;&#58;&#58;' | ./ncr2a
three colons :::

此示例使用stdin / stdout作为输入/输出,并调用yylex()一次。

从文件中读取:

yyin = fopen("input.txt", "r" );

@Loki Astari's answer显示如何从字符串中读取(buffer = yy_scan_string(text, scanner); yy_switch_to_buffer(buffer, scanner)

要为每个令牌调用yylex()一次,请在return文件中生成完整令牌的规则定义中添加*.l