Python lex - TypeError: Unknown textCalling an external command in PythonWhat are metaclasses in Python?Is there a way to run Python on Android?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?

Does a druid starting with a bow start with no arrows?

Is it canonical bit space?

Why is consensus so controversial in Britain?

Theorems that impeded progress

Why was the shrinking from 8″ made only to 5.25″ and not smaller (4″ or less)?

CEO ridiculed me with gay jokes and grabbed me and wouldn't let go - now getting pushed out of company

Doing something right before you need it - expression for this?

What is going on with Captain Marvel's blood colour?

How to prevent "they're falling in love" trope

Modeling an IP Address

Facing a paradox: Earnshaw's theorem in one dimension

Stopping power of mountain vs road bike

How do conventional missiles fly?

What's the difference between 'rename' and 'mv'?

Why can't we play rap on piano?

What reasons are there for a Capitalist to oppose a 100% inheritance tax?

Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?

Can one be a co-translator of a book, if he does not know the language that the book is translated into?

How can I fix/modify my tub/shower combo so the water comes out of the showerhead?

Took a trip to a parallel universe, need help deciphering

What mechanic is there to disable a threat instead of killing it?

Assassin's bullet with mercury

What is the intuition behind short exact sequences of groups; in particular, what is the intuition behind group extensions?

Were any external disk drives stacked vertically?



Python lex - TypeError: Unknown text


Calling an external command in PythonWhat are metaclasses in Python?Is there a way to run Python on Android?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm trying to write a simple lex parser. The cope is currently:



from ply import lex

tokens = (
'COMMENT',
'OTHER'
)

t_COMMENT = r'^#.*n'

t_OTHER = r'^[^#].*n'

def t_error(t):
raise TypeError("Unknown text '%s'" % (t.value,))

lex.lex()

lex.input(yaml)
for tok in iter(lex.token, None):
print repr(tok.type), repr(tok.value)


But is fails to parse simple input file:



 # This is a real comment
#And this one also

#/*
# *
# *Variable de feeu
# */
ma_var: True

It is done, over, kaput


With the following output:



l
'COMMENT' '# This is a real commentn'
Traceback (most recent call last):
File "parser_adoc.py", line 62, in <module>
main2()
File "parser_adoc.py", line 57, in main2
for tok in iter(lex.token, None):
File "/usr/lib/python2.7/site-packages/ply/lex.py", line 384, in token
newtok = self.lexerrorf(tok)
File "parser_adoc.py", line 44, in t_error
raise TypeError("Unknown text '%s'" % (t.value,))
TypeError: Unknown text '#And this one also

#/*
# *
# *Variable de feeu
# */
ma_var: True

this is done
'


So in summary, I defined 2 regex:



  • One for line beginning with #


  • One for lines beginning not with #


But it's not working.
I don't understand what's wrong with my regex.



Could you help?



Simon










share|improve this question






























    0















    I'm trying to write a simple lex parser. The cope is currently:



    from ply import lex

    tokens = (
    'COMMENT',
    'OTHER'
    )

    t_COMMENT = r'^#.*n'

    t_OTHER = r'^[^#].*n'

    def t_error(t):
    raise TypeError("Unknown text '%s'" % (t.value,))

    lex.lex()

    lex.input(yaml)
    for tok in iter(lex.token, None):
    print repr(tok.type), repr(tok.value)


    But is fails to parse simple input file:



     # This is a real comment
    #And this one also

    #/*
    # *
    # *Variable de feeu
    # */
    ma_var: True

    It is done, over, kaput


    With the following output:



    l
    'COMMENT' '# This is a real commentn'
    Traceback (most recent call last):
    File "parser_adoc.py", line 62, in <module>
    main2()
    File "parser_adoc.py", line 57, in main2
    for tok in iter(lex.token, None):
    File "/usr/lib/python2.7/site-packages/ply/lex.py", line 384, in token
    newtok = self.lexerrorf(tok)
    File "parser_adoc.py", line 44, in t_error
    raise TypeError("Unknown text '%s'" % (t.value,))
    TypeError: Unknown text '#And this one also

    #/*
    # *
    # *Variable de feeu
    # */
    ma_var: True

    this is done
    '


    So in summary, I defined 2 regex:



    • One for line beginning with #


    • One for lines beginning not with #


    But it's not working.
    I don't understand what's wrong with my regex.



    Could you help?



    Simon










    share|improve this question


























      0












      0








      0








      I'm trying to write a simple lex parser. The cope is currently:



      from ply import lex

      tokens = (
      'COMMENT',
      'OTHER'
      )

      t_COMMENT = r'^#.*n'

      t_OTHER = r'^[^#].*n'

      def t_error(t):
      raise TypeError("Unknown text '%s'" % (t.value,))

      lex.lex()

      lex.input(yaml)
      for tok in iter(lex.token, None):
      print repr(tok.type), repr(tok.value)


      But is fails to parse simple input file:



       # This is a real comment
      #And this one also

      #/*
      # *
      # *Variable de feeu
      # */
      ma_var: True

      It is done, over, kaput


      With the following output:



      l
      'COMMENT' '# This is a real commentn'
      Traceback (most recent call last):
      File "parser_adoc.py", line 62, in <module>
      main2()
      File "parser_adoc.py", line 57, in main2
      for tok in iter(lex.token, None):
      File "/usr/lib/python2.7/site-packages/ply/lex.py", line 384, in token
      newtok = self.lexerrorf(tok)
      File "parser_adoc.py", line 44, in t_error
      raise TypeError("Unknown text '%s'" % (t.value,))
      TypeError: Unknown text '#And this one also

      #/*
      # *
      # *Variable de feeu
      # */
      ma_var: True

      this is done
      '


      So in summary, I defined 2 regex:



      • One for line beginning with #


      • One for lines beginning not with #


      But it's not working.
      I don't understand what's wrong with my regex.



      Could you help?



      Simon










      share|improve this question
















      I'm trying to write a simple lex parser. The cope is currently:



      from ply import lex

      tokens = (
      'COMMENT',
      'OTHER'
      )

      t_COMMENT = r'^#.*n'

      t_OTHER = r'^[^#].*n'

      def t_error(t):
      raise TypeError("Unknown text '%s'" % (t.value,))

      lex.lex()

      lex.input(yaml)
      for tok in iter(lex.token, None):
      print repr(tok.type), repr(tok.value)


      But is fails to parse simple input file:



       # This is a real comment
      #And this one also

      #/*
      # *
      # *Variable de feeu
      # */
      ma_var: True

      It is done, over, kaput


      With the following output:



      l
      'COMMENT' '# This is a real commentn'
      Traceback (most recent call last):
      File "parser_adoc.py", line 62, in <module>
      main2()
      File "parser_adoc.py", line 57, in main2
      for tok in iter(lex.token, None):
      File "/usr/lib/python2.7/site-packages/ply/lex.py", line 384, in token
      newtok = self.lexerrorf(tok)
      File "parser_adoc.py", line 44, in t_error
      raise TypeError("Unknown text '%s'" % (t.value,))
      TypeError: Unknown text '#And this one also

      #/*
      # *
      # *Variable de feeu
      # */
      ma_var: True

      this is done
      '


      So in summary, I defined 2 regex:



      • One for line beginning with #


      • One for lines beginning not with #


      But it's not working.
      I don't understand what's wrong with my regex.



      Could you help?



      Simon







      python lex






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 23:58







      Raoul Debaze

















      asked Mar 8 at 23:53









      Raoul DebazeRaoul Debaze

      56112




      56112






















          1 Answer
          1






          active

          oldest

          votes


















          0














          In python regexes (which PLY uses), ^ refers to the beginning of the string, not the beginning of the line, unless multi-line mode has been set. So since both of your rules start with ^, they can only match on the first line.



          You could fix this by wrapping your regexes in (?m:...), which enables multi-line mode, but that's not even necessary here. Instead you can just remove the ^ from the beginning of your rules and it will work as you intend. Since both of your rules always match the entire line, the next token will always start at the beginning of the line - no need to anchor them.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072576%2fpython-lex-typeerror-unknown-text%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            In python regexes (which PLY uses), ^ refers to the beginning of the string, not the beginning of the line, unless multi-line mode has been set. So since both of your rules start with ^, they can only match on the first line.



            You could fix this by wrapping your regexes in (?m:...), which enables multi-line mode, but that's not even necessary here. Instead you can just remove the ^ from the beginning of your rules and it will work as you intend. Since both of your rules always match the entire line, the next token will always start at the beginning of the line - no need to anchor them.






            share|improve this answer



























              0














              In python regexes (which PLY uses), ^ refers to the beginning of the string, not the beginning of the line, unless multi-line mode has been set. So since both of your rules start with ^, they can only match on the first line.



              You could fix this by wrapping your regexes in (?m:...), which enables multi-line mode, but that's not even necessary here. Instead you can just remove the ^ from the beginning of your rules and it will work as you intend. Since both of your rules always match the entire line, the next token will always start at the beginning of the line - no need to anchor them.






              share|improve this answer

























                0












                0








                0







                In python regexes (which PLY uses), ^ refers to the beginning of the string, not the beginning of the line, unless multi-line mode has been set. So since both of your rules start with ^, they can only match on the first line.



                You could fix this by wrapping your regexes in (?m:...), which enables multi-line mode, but that's not even necessary here. Instead you can just remove the ^ from the beginning of your rules and it will work as you intend. Since both of your rules always match the entire line, the next token will always start at the beginning of the line - no need to anchor them.






                share|improve this answer













                In python regexes (which PLY uses), ^ refers to the beginning of the string, not the beginning of the line, unless multi-line mode has been set. So since both of your rules start with ^, they can only match on the first line.



                You could fix this by wrapping your regexes in (?m:...), which enables multi-line mode, but that's not even necessary here. Instead you can just remove the ^ from the beginning of your rules and it will work as you intend. Since both of your rules always match the entire line, the next token will always start at the beginning of the line - no need to anchor them.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 9 at 11:10









                sepp2ksepp2k

                300k39601617




                300k39601617





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072576%2fpython-lex-typeerror-unknown-text%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                    Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                    2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived