2012-11-25 56 views
3

我有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:[^] 
+0

你讀過flex/bison書嗎?其主要樣本可以幫助你。 –

+1

我沒有任何書,我從互聯網上學習=) – blueyan

回答

2

你必須尋找回車符(\ r),是常見的Windows系統中插入兩個字符按下回車鍵時,這兩個角色是新行(\ n)和回車(\ r),以這種方式添加它:

rule4: [ \t\r] 

它會正常工作。

+0

它解釋甚至輸出: ]發現一個錯誤:[ 第一個括號後,它被打印在屏幕'\ r'上,打印馬車返回chatacter意味着尋找寫入指針到行的開頭。 – Luca

+0

謝謝,它的作品像魅力! – blueyan