如何从yylex()返回?

时间:2016-11-24 12:37:14

标签: lex

我正在编写lex代码以将标签转换为空格键(一个标签等于4个空格键)并计算这些空格键。代码如下:

    [Test]
    public void Libary1_Methode1_struct()
    {
        Speed speed = new Speed(new CommInterface());
        int response = 99;
        Mock<ILibary> mockLibary = new Mock< ILibary>();

        mockLibary.Setup(
            r =>
                r. Methode1(It.IsAny<ushort>(), It.IsAny<short>(), It.IsAny<short>(), It.IsAny<RealLibary.Struct1>()))
            .Callback<ushort, short, short, RealLibary.Struct1>(
                (hndl, a, b, dbaxis) =>
                {
                    dbaxis.data = new[] {0, 1, 2, 3};
                    dbaxis.dummy = 0;
                    dbaxis.type = 0;
                });

        RealLibary.Struct1 struct1 = new RealLibary.Struct1();
        List<object> list = new List<object>();
        list.Add(new short());
        list.Add(struct1);
        list.Add(new short());
        list.Add(new short());
        speed.Methode1(0, mockLibary.Object, list, out response);

        Assert.AreEqual(4, struct1.data.Length);
    }

输出应该打印一个语句:printf(“空格键是:%d”,spacebarCount);但事实并非如此。那么我该怎么做才能打印出我想要的输出呢?另外,我试图在主函数中替换下面的语句,但它没有用。

%{
 #include<stdio.h>
 int spacebarCount=0;
%}

%%
[\t]    {
        spacebarCount+=4;
        }
[ ]     {
        spacebarCount++;
        }
%%

int main()
    {
    yylex();
    printf("The spacebar is: %d",spacebarCount);
    spacebarCount=0;
    }

1 个答案:

答案 0 :(得分:0)

在flex中,可以通过将End-Of-File与模式<<EOF>>匹配来处理:

%{
 #include<stdio.h>
 int spacebarCount=0;
%}

%%
[\t]    {
        spacebarCount+=4;
        }
[ ]     {
        spacebarCount++;
        }
<<EOF>> {
        printf("The spacebar is: %d",spacebarCount);
        return 0;
        }
%%

int main()
    {
    yylex();
    }
相关问题