小编Rav*_*ell的帖子

无法在perl6中编写语法来解析具有特殊字符的行

我有以下代码:https://gist.github.com/ravbell/d94b37f1a346a1f73b5a827d9eaf7c92

use v6;
#use Grammar::Tracer;


grammar invoice {

    token ws { \h*};
    token super-word {\S+};
    token super-phrase { <super-word> [\h  <super-word>]*}
    token line {^^ \h* [ <super-word> \h+]* <super-word>* \n};

    token invoice-prelude-start {^^'Invoice Summary'\n}
    token invoice-prelude-end {<line> <?before 'Start Invoice Details'\n>};

    rule invoice-prelude {
        <invoice-prelude-start>
        <line>*?
        <invoice-prelude-end>
        <line>
    }
}

multi sub MAIN(){ 

    my $t = q :to/EOQ/; 
    Invoice Summary
    asd fasdf
    asdfasdf
    asd 123-fasdf $1234.00
    qwe {rq} [we-r_q] we
    Start Invoice Details 
    EOQ


    say $t;
    say …
Run Code Online (Sandbox Code Playgroud)

regex grammar parsing perl6 raku

6
推荐指数
2
解决办法
156
查看次数

Python Lex-Yacc(PLY):无法识别行开头或字符串开头

我对PLY很陌生,对 Python 也只是个初学者。我正在尝试使用PLY-3.4和 python 2.7 来学习它。请参阅下面的代码。我正在尝试创建一个令牌 QTAG,它是一个由零个或多个空格组成的字符串,后跟“Q”或“q”,后跟“.”。以及一个正整数和一个或多个空格。例如,有效的 QTAG 是

"Q.11 "
"  Q.12 "
"q.13     "
'''
   Q.14 
'''
Run Code Online (Sandbox Code Playgroud)

无效的是

"asdf Q.15 "
"Q.  15 "
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

import ply.lex as lex

class LqbLexer:
     # List of token names.   This is always required
     tokens =  [
        'QTAG',
        'INT'
        ]


     # Regular expression rules for simple tokens

    def t_QTAG(self,t):
        r'^[ \t]*[Qq]\.[0-9]+\s+'
        t.value = int(t.value.strip()[2:])
        return t

    # A regular expression rule with some action code
    # Note addition …
Run Code Online (Sandbox Code Playgroud)

python regex ply tokenize lexer

0
推荐指数
1
解决办法
3246
查看次数

标签 统计

regex ×2

grammar ×1

lexer ×1

parsing ×1

perl6 ×1

ply ×1

python ×1

raku ×1

tokenize ×1