Path: blob/main/third_party/ply/example/yply/yparse.py
7087 views
# parser for Unix yacc-based grammars1#2# Author: David Beazley ([email protected])3# Date : October 2, 200645import ylex6tokens = ylex.tokens78from ply import *910tokenlist = []11preclist = []1213emit_code = 11415def p_yacc(p):16'''yacc : defsection rulesection'''1718def p_defsection(p):19'''defsection : definitions SECTION20| SECTION'''21p.lexer.lastsection = 122print "tokens = ", repr(tokenlist)2324print "precedence = ", repr(preclist)2526print "# -------------- RULES ----------------"272829def p_rulesection(p):30'''rulesection : rules SECTION'''3132print "# -------------- RULES END ----------------"33print_code(p[2],0)3435def p_definitions(p):36'''definitions : definitions definition37| definition'''3839def p_definition_literal(p):40'''definition : LITERAL'''41print_code(p[1],0)4243def p_definition_start(p):44'''definition : START ID'''45print "start = '%s'" % p[2]4647def p_definition_token(p):48'''definition : toktype opttype idlist optsemi '''49for i in p[3]:50if i[0] not in "'\"":51tokenlist.append(i)52if p[1] == '%left':53preclist.append(('left',) + tuple(p[3]))54elif p[1] == '%right':55preclist.append(('right',) + tuple(p[3]))56elif p[1] == '%nonassoc':57preclist.append(('nonassoc',)+ tuple(p[3]))5859def p_toktype(p):60'''toktype : TOKEN61| LEFT62| RIGHT63| NONASSOC'''64p[0] = p[1]6566def p_opttype(p):67'''opttype : '<' ID '>'68| empty'''6970def p_idlist(p):71'''idlist : idlist optcomma tokenid72| tokenid'''73if len(p) == 2:74p[0] = [p[1]]75else:76p[0] = p[1]77p[1].append(p[3])7879def p_tokenid(p):80'''tokenid : ID81| ID NUMBER82| QLITERAL83| QLITERAL NUMBER'''84p[0] = p[1]8586def p_optsemi(p):87'''optsemi : ';'88| empty'''8990def p_optcomma(p):91'''optcomma : ','92| empty'''9394def p_definition_type(p):95'''definition : TYPE '<' ID '>' namelist optsemi'''96# type declarations are ignored9798def p_namelist(p):99'''namelist : namelist optcomma ID100| ID'''101102def p_definition_union(p):103'''definition : UNION CODE optsemi'''104# Union declarations are ignored105106def p_rules(p):107'''rules : rules rule108| rule'''109if len(p) == 2:110rule = p[1]111else:112rule = p[2]113114# Print out a Python equivalent of this rule115116embedded = [ ] # Embedded actions (a mess)117embed_count = 0118119rulename = rule[0]120rulecount = 1121for r in rule[1]:122# r contains one of the rule possibilities123print "def p_%s_%d(p):" % (rulename,rulecount)124prod = []125prodcode = ""126for i in range(len(r)):127item = r[i]128if item[0] == '{': # A code block129if i == len(r) - 1:130prodcode = item131break132else:133# an embedded action134embed_name = "_embed%d_%s" % (embed_count,rulename)135prod.append(embed_name)136embedded.append((embed_name,item))137embed_count += 1138else:139prod.append(item)140print " '''%s : %s'''" % (rulename, " ".join(prod))141# Emit code142print_code(prodcode,4)143144rulecount += 1145146for e,code in embedded:147print "def p_%s(p):" % e148print " '''%s : '''" % e149print_code(code,4)150151152def p_rule(p):153'''rule : ID ':' rulelist ';' '''154p[0] = (p[1],[p[3]])155156def p_rule2(p):157'''rule : ID ':' rulelist morerules ';' '''158p[4].insert(0,p[3])159p[0] = (p[1],p[4])160161def p_rule_empty(p):162'''rule : ID ':' ';' '''163p[0] = (p[1],[[]])164165def p_rule_empty2(p):166'''rule : ID ':' morerules ';' '''167168p[3].insert(0,[])169p[0] = (p[1],p[3])170171def p_morerules(p):172'''morerules : morerules '|' rulelist173| '|' rulelist174| '|' '''175176if len(p) == 2:177p[0] = [[]]178elif len(p) == 3:179p[0] = [p[2]]180else:181p[0] = p[1]182p[0].append(p[3])183184# print "morerules", len(p), p[0]185186def p_rulelist(p):187'''rulelist : rulelist ruleitem188| ruleitem'''189190if len(p) == 2:191p[0] = [p[1]]192else:193p[0] = p[1]194p[1].append(p[2])195196def p_ruleitem(p):197'''ruleitem : ID198| QLITERAL199| CODE200| PREC'''201p[0] = p[1]202203def p_empty(p):204'''empty : '''205206def p_error(p):207pass208209yacc.yacc(debug=0)210211def print_code(code,indent):212if not emit_code: return213codelines = code.splitlines()214for c in codelines:215print "%s# %s" % (" "*indent,c)216217218219