python - Identifying keyword pairs in lex -


I am trying to write a parser for a file type that is used to add keywords (separate from a location) And the correct way to do this can be some examples of tokens:

  angle spring angle angle collision  

block definitions and tokens are also Those that end the block, for example:

Newlines seem important, I am using to determine if I am a Looking at the keyword or just a regular old string (the keyword should be the first token on each line) Am I reaching it correctly? Should I synchronize everything in return for this and should be more strictly defined during the yacc phase?

Thank you for your time!

The problem is that you try to reasonably treat a token in the form of multiple tokens Are there. If a keyword contains an empty space, it means that spaces are part of the keyword token .

If you define token with your keyword spaces, you never want to handle them parsing them. This means that you should match the keyword with a normal identifier match.

For example:

token keyword from ply.lex import = [r'some ', rkeyword', 'rkeyword' with token, r'other keyword ',] keyword =' ​​| ''. (Keyword. Keywords (for keywords in keywords '', '\ s +')) @TOKEN (keyword) def t_KEYWORD (t): # Remove spaces value = ''. (X.isspace () if not x.isspace () x for x (x) ()

note @ token (keyword) line: you You can set the dosstring of a function dynamically by using the TOKEN decorator. It allows complex rezuxes to be used to define tokens, even if they need to use the expression Do not require "defined" and do not have simple string literals.


Alternatively, space The keywords separated from are meant to behave as many keywords, so you can have a general definition of identifiers and keywords, and modify your grammar to use multiple keywords instead of one. For example, you have to A grammar rule would be:

  Def p_dynamics (p): Instead of 'dynamics block and dynamics'  

:

 < Code> def p_dynamics (p): 'dynamics blockEND_DYNAMICS'  
< P> You may have easy to implement a solution.


Comments