我有4條規則詞法分析器用C,新行令牌
rule1: match variable
rule2: match Float
rule3: match Integer
rule4: [ \t] ; //skipping space, tabs
rule5: \n ;
現在我想爲匹配誤差的規則,別的不是ID,浮法,整數,例如:」 & ^( )>〜......」
所以這是我創建的規則:
. { printf("found an error:%s\n",yytext);
err_count++;
}
但也有一些是錯誤的,它捕獲的掃描儀時跳轉到下一行一個未知的令牌。
我輸入:
12+32+22:18,18 99558^8*6 15.~646(5
My output
Found a number:12
Found a error:+
Found a number:32
Found a error:+
Found a number:22
Found a error::
Found a number:18
Found a error:,
Found a number:18
Found a error:
Found a number:99558
Found a error:^
Found a number:8
Found a error:*
Found a number:6
Found a error:
Found a float:15.
Found a error:~
Found a number:646
Found a error:(
Found a number:5
調試目的,我試圖找出用方括號
. {
printf("Found a error:[%s]\n",yytext);
err_count++;
}
bash show:
Found a number:12
Found a error:[+]
Found a number:32
Found a error:[+]
Found a number:22
Found a error:[:]
Found a number:18
Found a error:[,]
Found a number:18
]ound a error:[
Found a number:99558
Found a error:[^]
Found a number:8
Found a error:[*]
Found a number:6
]ound a error:[
Found a float:15.
Found a error:[~]
Found a number:646
Found a error:[(]
Found a number:5
redirect output to a txt file
Found a number:12
Found a error:[+]
Found a number:32
Found a error:[+]
Found a number:22
Found a error:[:]
Found a number:18
Found a error:[,]
Found a number:18
Found a error:[
]
Found a number:99558
Found a error:[^]
Found a number:8
Found a error:[*]
Found a number:6
Found a error:[
]
Found a float:15.
Found a error:[~]
Found a number:646
Found a error:[(]
Found a number:5
任何人都知道是什麼引起的問題包圍它的令牌?謝謝 我可以向你保證rule5:\ n;正常工作,這裏是證明
I add print to rule5"\n { printf("newline\n");
redirect output to a txt file
Found a number:12
Found a error:[+]
Found a number:32
Found a error:[+]
Found a number:22
Found a error:[:]
Found a number:18
Found a error:[,]
Found a number:18
Found a error:[
]
newline
Found a number:99558
Found a error:[^]
Found a number:8
Found a error:[*]
Found a number:6
Found a error:[
]
newline
Found a float:15.
Found a error:[~]
Found a number:646
Found a error:[(]
Found a number:5
Found a error:[
]
newline
Found a variable:T189_10
Found a error:[@]
Found a float:12.2
Found a error:[
]
newline
Found a number:1
Found a variable:ABB21u
Found a error:[=]
Found a variable:ier_
Found a error:[
]
newline
Found a error:[$]
Found a error:[%]
Found a error:[^]
你讀過flex/bison書嗎?其主要樣本可以幫助你。 –
我沒有任何書,我從互聯網上學習=) – blueyan