詞法分析器與123foo很好,並將它分成兩個標記。
但試着找到允許這兩個標記並排坐在一起的語法部分。因此我敢打賭,詞法分析器在看到這兩個令牌時會產生一個錯誤。
注意詞法分析器不關心空格(除非你明確地告訴它空格)。在這種情況下,它只是拋出空白遠:
[ \t\v\n\f] { count(); } // Throw away white space without looking.
只是爲了檢查這是我建:
wget http://www.lysator.liu.se/c/ANSI-C-grammar-l.html > l.l
wget http://www.lysator.liu.se/c/ANSI-C-grammar-y.html > y.y
編輯後的文件湖L若要停止在編譯器抱怨未申報的功能:
#include "y.tab.h"
// Add the following lines
int yywrap();
void count();
void comment();
void count();
int check_type();
// Done adding lines
%}
創建以下文件:main.c中:
#include <stdio.h>
extern int yylex();
int main()
{
int x;
while((x = yylex()) != 0)
{
fprintf(stdout, "Token(%d)\n", x);
}
}
構建:
$ bison -d y.y
y.y: conflicts: 1 shift/reduce
$ flex l.l
$ gcc main.c lex.yy.c
$ ./a.out
123foo
123Token(259)
fooToken(258)
是它,它一分爲二令牌。
你讓我在「我想解析一些C++代碼」。 – 2010-11-24 09:21:27