Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is possible with Ply, but requires some data massaging and token building.</p> <pre><code>import re from ply import lex tokens = ('LOOP','ASSIGNMENT') literals = '=,' re_float = r'(\d+\.\d+)' re_int = r'(\d+)' re_ident = r'([A-Za-z]\w*)' re_expr = '(%s)' % '|'.join([re_float, re_int, re_ident]) re_loop = 'DO%s%s=%s,%s' % (re_int, re_ident, re_expr, re_expr) @lex.TOKEN(re_loop) def t_LOOP(t): return t re_assignment = '%s=%s' % (re_ident, re_expr) @lex.TOKEN(re_assignment) def t_ASSIGNMENT(t): return t def t_newline(t): r'\n+' t.lineno += len(t.value) # count newlines def t_error(t): print "syntax error at %s, line# %d" % (t.value, t.lineno) DATA = """-- Spaces are ignored in FORTRAN DO 5 I=1,10 -- Loop DO 5 I=1.10 -- Assignment (DO5I = 1.10)""" def preprocess(data): re_spaces=re.compile('\s*') re_comment=re.compile('--.*$') lines = [] for line in data.split('\n'): # split into lines line = re_spaces.sub('', line) line = re_comment.sub('', line) if not line: continue # skip blank lines line = line.upper() lines.append(line) return '\n'.join(lines)+'\n' print re_assignment lexer = lex.lex() lexer.input(preprocess(DATA)) while True: tok = lexer.token() if not tok: break print tok </code></pre> <p>First, you have to manually strip comments, spaces, and force to uppercase. Then you build a parser which basically implements your grammar inline. I think it would eventually become unmanageable. If it were me, I think it would be much easier to implement my own lexer which just does line-by-line regular expression matching. All Ply's lexer really does is build one huge regular expression with all your little regular expressions and then progressively matches tokens.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload