【问题标题】:Regex for VHDL IdentifierVHDL 标识符的正则表达式
【发布时间】:2011-12-19 01:31:42
【问题描述】:

我正在尝试解析我的 VHDL 代码以进行一些额外的检查。

我正在寻找一个正则表达式来检查 VHDL 中的正确标识符。而且我对正则表达式还是很陌生。

它有以下规则:

  • 只能包含字母 (A..Z a..z) 数字 (0..9) 和下划线 ('_')

  • 必须以字母开头

  • 不能以下划线字符结尾

  • 不能包含两个连续的下划线字符

所以我目前的问题是检查两个连续的下划线字符...

更新:我想我只是自己回答了这个问题......请仔细检查

[A-Za-z](_?[A-Za-z0-9])*

【问题讨论】:

  • 这涵盖了基本标识符。但 VHDL 也支持扩展标识符,用反斜杠括起来,它可以包含任何图形 ISO Latin-1 字符(包括反斜杠,如果加倍)。
  • @sebs,是的,你是对的,我删除了我的答案。

标签: regex vhdl


【解决方案1】:
(?!.*__)[a-zA-Z][\w]*[^_]

这应该可以解决问题。

解释:

 # (?!.*__)[a-zA-Z][\w]*[^_]
# 
# Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!.*__)»
#    Match any single character that is not a line break character «.*»
#       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
#    Match the characters “__” literally «__»
# Match a single character present in the list below «[a-zA-Z]»
#    A character in the range between “a” and “z” «a-z»
#    A character in the range between “A” and “Z” «A-Z»
# Match a single character that is a “word character” (letters, digits, etc.) «[\w]*»
#    Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
# Match any character that is NOT a “_” «[^_]»

【讨论】:

    【解决方案2】:

    基本标识符

    在您对问题的更新中,您建议:{letter}({underscore}?{letter_or_digit})*。这正是 VHDL 规范对表达式的建议。还值得注意的是,基本标识符不区分大小写。也就是说,idID 被视为相同的标识符。

    扩展标识符

    但是,在 VHDL 中,也有扩展标识符。一个不错的正则表达式是:

    ({backslash}{Any ISO 8859-1 except backslash}*{backslash})+
    

    保留字

    另外,请注意以下标识符不是传统上处理的,而是保留字:这是 2002 年规范中的列表。根据您正在实施的规范版本,保留字可能更多或更少

    abs access after alias all and architecture array assert attribute begin block
    body buffer bus case component configuration constant disconnect downto else
    elsif end entity exit file for function generate generic group guarded if
    impure in inertial inout is label library linkage literal loop map mod nand
    new next nor not null of on open or others out package port postponed
    procedural procedure process protected pure range record reference register
    reject rem report return rol ror select severity shared signal sla sll sra srl
    subtype then to transport type unaffected units until use variable wait when
    while with xnor xor
    

    字母

    还值得注意的是,在 VHDL 中 [A-Za-z] 并不是字母表中的所有字母。您还应该包括 ISO 8859-1 拉丁字符。你可以找到更多关于这些字符here的信息。

    但是,话虽如此,这里是多余的大写字母:

    À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß 
    

    这里是多余的小写字母:

    à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ
    

    【讨论】:

      【解决方案3】:

      我认为,更好的正则表达式是

      \\([^\\]|\\\\)+\\|[A-Za-z](_?[0-9A-Za-z])*
      

      【讨论】:

      • 参见 IEEE Std 1076-2008 15.2 字符集,15.4 标识符。自 -1993 年起,VHDL 大小写字母包括 ISO/IEC 8859-1 中的字母。
      猜你喜欢
      • 2013-10-19
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 2016-03-18
      相关资源
      最近更新 更多