2013-02-27 162 views
0

这里是输入文件,.l文件,.y文件和输出。递归解析

问题是,解析器将无法递归识别方向..

它是确定只是第一...

我用同样的规则识别端口及其工作

但不是在案件方向的..

也它不显示与规则(COUT语句)相关.Y文件代码

输入文件。

start a b c d    //ports 

a:O b:I c:B d:O //direction of ports 

.L文件

[\t]+     {} 

[\n] {line_num++; cout"line_num:" line_num; } 

start     { cout< "beggining of file"; return START;} 



[a-zA-Z0-9_\-]+:[IOB] {cout<<"\ndirection:" << strdup(yytext); return DR; } 

[a-zA-Z0-9_\-]+  {cout<<"\nfound name:" strdup(yytext); return NAME;} 

.Y文件语法

doc : START ports dir 

ports : NAME ports { cout<<"\port in .y" $1;} 

     | NAME { cout<<"\nport in .y" $1;} 
     ; 


dir : DR dir  { cout<<"\ndirection in .y" $1;} 

    | DR   { cout<<"\ndirection in .y"<<$1; } 
    ; 

输出。文件

开始

found name:a 

found name:b 

found name:c 

found name:d 

line no-2 

direction:a:O 
+0

我没有看到放弃普通空间的lex规则,也就是我看到'[\ t] +',或者只是'\ s +'。你确定输入文件的第二行是用制表符分隔的,而不是空格吗? – 2013-02-27 14:23:56

+0

[\ t] +是完美的工作...我已经多次使用这个规则... – user2114865 2013-02-28 05:33:03

回答

1

你正在做的唯一清楚的错误是,你是不是设置的yylval在Flex行动的价值,所以$1是在所有的野牛动作的一些初始化值。您的Flex行动应该是这个样子:

[a-zA-Z0-9_\-]+  { yylval = strdup(text); 
         cout << "\nfound name:" << yylval; 
         return NAME; 
        } 

此外,请确保您指定的令牌DRNAME的类型是const char *

最后,不要忘记free()字符串,当你不再需要它们。

+0

thanx for ur answer .... – user2114865 2013-02-28 06:07:50