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).

see python documentation dict.get().


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -