C FLEX-正则表达式-在AWK中匹配注释

C FLEX-正则表达式-在AWK中匹配注释,c,awk,lex,flex-lexer,C,Awk,Lex,Flex Lexer,我正在开发一个扫描文件并突出显示gawk标记的程序。Flex用于为gawk生成lexer扫描仪 我的问题是:我没有为Flex编写正则表达式的经验。现在我不知道如何生成一个正则表达式来匹配整个注释。请参阅下面将被扫描的示例.awk程序: #!/usr/bin/awk -f ############################################################################### # # @(#) solve.awk - sudoku solver

我正在开发一个扫描文件并突出显示gawk标记的程序。Flex用于为gawk生成lexer扫描仪

我的问题是:我没有为Flex编写正则表达式的经验。现在我不知道如何生成一个正则表达式来匹配整个注释。请参阅下面将被扫描的示例.awk程序:

#!/usr/bin/awk -f
###############################################################################
#
# @(#) solve.awk - sudoku solver in awk using efficient backtracking algorithm
# @(#) $Id: solve.awk,v 1.16 2008/03/24 04:04:44 bduncan Exp bduncan $
# @(#) Copyright (C) 2005-2008, Bill Duncan, <bduncan-sudoku@beachnet.org>
#
# License:
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
# Description:
# - uses simple recursive backtracking algorithm with up-front and
# ongoing elimination of invalid tries by tracking for each row,
# column and region..
#
# Notes:
# - precalc of regmap didn't seem to make a difference
# - removed unmark() function and added a parameter to mark()
#
# Variables:
# regmap[r,c] ; pre-compiled, which region sector is r,c
# master[r,c] ; master matrix
# C[col, elem] ; true if elem in Column
# R[row, elem] ; true if elem in Row
# Q[reg, elem] ; true if elem in Region (Quadrant)
#
###############################################################################
BEGIN {
SUBSEP = "," # so we can dump and it looks nice
ORDER = 9
DEBUG = 0
count = 0
# precompile region map for faster lookup
# for (i = 0; i < ORDER; i++)
# for (j = 0; j < ORDER; j++)
# regmap[i+1,j+1] = int(i/3)*3+int(j/3)+1
}
function dump( i,j) {
printf "\n"
for (i=1;i<=ORDER;i++) {
if (!((i-1)%3)) printf "\n"
for (j=1;j<=ORDER;j++) {
if (!((j-1)%3)) printf " "
printf " %1d",master[i,j]
}
printf "\n"
}
printf "\n"
}
function fregmap(r,c) {
# return regmap[r,c]
return int((r-1)/3)*3+int((c-1)/3)+1
}
function inuse(r,c,try) {
# q = fregmap(r,c)
# can we use it or is it in use? returns true if already used, not avail
return (C[c,try] || R[r,try] || Q[fregmap(r,c),try])
}
function mark(r,c,try, flag, q) {
q = fregmap(r,c)
Q[q,try] = flag
R[r,try] = flag
C[c,try] = flag
master[r,c] = flag ? try : 0
}
function search(r,c, q,i,a,try) {
# find the next empty slot from here r,c
# if we've reached the end (no more empty) do check?
# for each available number, recurse search
count++
while (master[r,c]) {
if (++c > ORDER) {
c = 1
if (++r > ORDER) {
# then we're done filling! return goodness
return 1
}
}
}
# for each of the available numbers for this slot
for (try=1; try <= ORDER; try++) {
if (! inuse(r,c,try)) {
mark(r,c,try, 1)
if (search(r,c)) return 1
# else zero returned -- unwind
mark(r,c,try, 0) # unmark
}
}
return 0
}
############
# PATTERNS #
############
NF == 0 { next }
$1 ~ /^#/ { next }
NF != ORDER {
printf "error on line %d, NF=%d\n", FNR, NF
exit 1
}
{
++row
for (col=1; col <= ORDER; col++) {
mark(row,col,$col, 1)
}
}
END {
search(1,1)
printf "\n# Searches=%d\n", count
dump()
}
我目前正在使用^+匹配评论。这与所有字符匹配,但与该行中的其余字符不匹配。你如何匹配一段时间后发生的所有事情

可以查看Flex模式结构。

修饰符+表示“一个或多个前一个模式”,即文字,因此它只匹配包含从第1列开始的一个或多个连续哈希的行的起始部分

对于锚定的线匹配起点,您需要:

^#.*
阿菲尔,这是我的梦想。不匹配换行符。这意味着一行以开头,后跟除换行符以外的任何类型的零个或多个其他字符

不要忘记,awk注释不限于从行首开始:

awk '{
         # This comment is indented by a number of spaces
         print $1; # And this is preceded by a command
     }'
在广泛的范围内,任何时候你遇到一个问题,这都是一个评论的开始。限制不包括字符串和正则表达式的主体:

awk '{ print "# Not a comment" }'

awk '/#.*/ { print "Line contains a # comment: ", $0; }'

等等。因此,您需要在注释规则之前启动正则表达式规则和字符串规则。

可能类似于^.*$?不过我不太确定。谢谢你的回复。我试过用这个。但没法让它发挥作用,我开始明白了!