正则表达式 -- compilation-error-regexp-alist

本文介绍Emacs中compile程序包如何通过正则表达式列表compilation-error-regexp-alist来解析不同编译器的错误信息。该列表涵盖了多种编译器格式,并详细解释了正则表达式的构成及其如何帮助定位文件及行号。

compile 程序包在分析编译器给出的出错信息时,所使用的正则表达式列表 compilation-error-regexp-alist。这个列表变量从 Emacs 的源代码里节选出来列在下面:

(

 ;; 4.3BSD grep, cc, lint pass 1:

 ;; /usr/src/foo/foo.c(8): warning: w may be used before set

 ;; or GNU utilities:

 ;; foo.c:8: error message

 ;; or HP-UX 7.0 fc:

 ;; foo.f :16 some horrible error message

 ("/n//([ /t/n]+//)[:(][ /t]*//([0-9]+//)[:) /t]" 1 2)

 

 ;; 4.3 BSD lint pass 2

 ;; strcmp: variable # of args. llib-lc(359) :: /usr/src/foo/foo.c(8)

 ("[ /t:]//([^:( /t/n]+//)[:(](+[ /t]*//([0-9]+//))[:) ]*$" 1 2)

 

 ;; 4.3 BSD lint pass 3

 ;; bloofle defined( /users/wolfgang/fooc(4) ), but nerver used

 ;; This used to be

 ("([ /t]*//([^:( /t/n]+//)[:(][ /t]*//([0-9]+//))" 1 2)

 

 ;; Ultrix 3.0 f77:

 ;; Error on line 3 of t.f: Execution error unclassifiable statement

 ;; Unknown who does this:

 ;; Line 45 of "foo.c": bloofel undefined

 ("/n//(Error on //)?[Ll]ine[ /t]+//([0-9]+//)[ /t]+/of[ /t]+/"?//([^/"/n]+//)/"?:" 3 2)

 

 ;; Apollo cc, 4.3 BSD fc:

 ;; "foo.f", line 3: Error: syntax error near end of statement

 ;; IBM RS6000:

 ;; "vvouch.c", line 19.5: 1506-046 (S) Syntax error.

 ;; Unknown compiler:

 ;; File "foobar.ml", lines 5-8, characters 20-155: blah blah

 ("/"//([^,/" /n/t]+//)/", lines? //([0-9]+//)[-:.,]" 1 2)

 

 ;; MIPS RISC CC - the one distributed with Ultrix:

 ;; ccom: Error: foo.c, line 2: syntax error

 ("Eroor: //([^,/" /n/t]+//), line //([0-9]+//):" 1 2)

 

 ;; IBM AIX PS/2 C version 1.1:

 ;; ****** Error number 140 in line 8 of file error.c ******

 ("in line //([0-9]+//) of file //([^ /n]+[^. //n]//)//.?" 2 1)

 )

 

这个列表里的元素都是由三个部分组成的:一个正则表达式和两个数字。正则表达式的作用是按特定编译器所使用的格式对出错进行匹配,它被归组为几个子表达式。第一个数字告诉 Emacs 出错信息里的文件名被报存在第几个子表达式里;第二个数字则用来指明行号报存在第几个子表达式里。

 

比如说,列表的最后一项包含着正则表达式:

"in line //([0-9]+//) of file //([^ /n]+[^. //n]//)//.?"

然后是数字 2 和 1,这就意味着被归为第二组的子表达式包含着文件名,被归为第一组的子表达式包含着行号。如果使用的是运行着 IBM 版 UNIX 的 Intel x86 PC 上的 C 编译器,看到的出错信息就将是下面这样的:

****** Error number 140 in line 8 of file errors.c ******

正则表达式对出错信息中“in line”之前的内容不做处理。接下来,它找到了与第一个子表达式“[0-9]+”相匹配的行号 “8”,以及与第二个子表达式“[^/n]+[^. /n]”(一个或更多个不是空格也不是换行符的单个字符)相匹配的文件名“error.c”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值