parsing - Explain the syntax of reserved.get(t.value,'ID') in lex.py -
code taken ply.lex documentation: http://www.dabeaz.com/ply/ply.html#ply_nn6
reserved = { 'if' : 'if', 'then' : 'then', 'else' : 'else', 'while' : 'while', ... } tokens = ['lparen','rparen',...,'id'] + list(reserved.values()) def t_id(t): r'[a-za-z_][a-za-z_0-9]*' t.type = reserved.get(t.value,'id') # check reserved words return t
for reserved
words, need change token type
. doing reserved.get()
passing t.value
understandable. should return entities in second column in reserved specification
.
but why passing id
? mean , purpose solve?
the second parameter specifies value return should key not exist in dictionary. in case, if value of t.value
not exist key in reserved
dictionary, string 'id'
returned instead.
in other words, a.get(b, c)
when a
dict equivalent a[b] if b in else c
(except presumably more efficient, key once in success case).
Comments
Post a Comment