def interpret(ast,htmllexer,htmlparser): if not ast: return for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) pass elif nodetype == "tag-element": tagname = node[1] tagargs = node[2] subast = node[3] closetagname = node[4] if (tagname != closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") print("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname,tagargs) interpret(subast,htmllexer,htmlparser) graphics.endtag() elif nodetype == "javascript-element": jstext = node[1]; jslexer.input(jstext) jsast = jsparser.parse(jstext,lexer=jslexer) result = js_interpreter.interpret(jsast) htmlast = htmlparser.parse(result,lexer=htmllexer) interpret(htmlast,htmllexer,htmlparser) return
def interpret(trees): # Hello, friend for tree in trees: # Hello, # ("word-element","Hello") nodetype=tree[0] # "word-element" if nodetype == "word-element": graphics.word(tree[1]) elif nodetype == "tag-element": # <b>Strong text</b> tagname = tree[1] # b tagargs = tree[2] # [] subtrees = tree[3] # ...Strong Text!... closetagname = tree[4] # b if(tagname!=closetagname): graphics.warning("mismatched tag") else: graphics.begintag(tagname,tagargs) interpret(subtrees) graphics.endtag() elif nodetype == "javascript-element": jstext = tree[1]; # "document.write(55);" jslexer = lex.lex(module=jstokens) jsparser = yacc.yacc(module=jsgrammar) jstree = jsparser.parse(jstext,lexer=jslexer) # jstree is a parse tree for JavaScript result = jsinterp.interpret(jstree) graphics.word(result)
def interpret(trees): # Hello, friend for tree in trees: # Hello, # ("word-element","Hello") nodetype = tree[0] # "word-element" if nodetype == "word-element": graphics.word(tree[1]) elif nodetype == "tag-element": # <b>Strong text</b> tagname = tree[1] # b tagargs = tree[2] # [] subtrees = tree[3] # ...Strong Text!... closetagname = tree[4] # b if tagname != closetagname: graphics.warning("mismatched tag") else: graphics.begintag(tagname, tagname) interpret(subtrees) graphics.endtag()
def interpret(ast): # graphics.initialize() for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1] tagargs = node[2] subast = node[3] closetagname = node[4] if tagname != closetagname: # this error has detected at the parse step. graphics.warning("(mismatched" + tagname + " " + closetagname + ")") else: graphics.begintag(tagname, tagargs) interpret(subast) graphics.endtag() elif nodetype == "javascript-element": jstext = node[1] jslexer = lex.lex(module=jstokens) jsparser = yacc.yacc(module=jsgrammar, tabmodule="parsetabjs") jsast = jsparser.parse(jstext, lexer=jslexer) result = jsinterp.jsinterp(jsast) htmlast = htmlparser.parse(result, lexer=htmllexer) interpret(htmlast)
def interpret(ast): for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1] tagargs = node[2] subast = node[3] closetagname = node[4] if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname, tagargs) interpret(subast) graphics.endtag() elif nodetype == "javascript-element": jstext = node[1] jsast = jsparser.parse(jstext, lexer=jslexer) result = jsinterp.interpret(jsast) # rather than just call graphics we re-interpret here. # graphics.word(result) htmlast = htmlparser.parse(result, lexer=htmllexer) interpret(htmlast)
def interpret(ast): # AST for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1]; tagargs = node[2]; subast = node[3]; closetagname = node[4]; if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname,tagargs); interpret(subast) graphics.endtag(); elif nodetype == "javascript-element": jstext = node[1]; jslexer = lex.lex(module=jstokens) if False: # JavaScript lexer/parser debugging print jstext jslexer.input(jstext) while True: tok = jslexer.token() if not tok: break print tok jsparser = yacc.yacc(module=jsgrammar,tabmodule="parsetabjs") jsast = jsparser.parse(jstext,lexer=jslexer) result = jsinterp.interpret(jsast) graphics.word(result)
def interpret(webpage): for node in webpage: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1] tagargs = node[2] subpage = node[3] closetagname = node[4] if (tagname != closetagname): graphics.warning("(mistmatched" + tagname + "" + closetagname + ")") else: graphics.begintag(tagname, tagargs) interpret(subpage) graphics.endtag() elif nodetype == "javascript-element": jstext = node[1] jspage = jsparser.parse(jstext, lexer=jslexer) result = jsinterp.interpret(jspage) htmlast = htmlparser.parse(result, lexer=htmllexer) interpret(htmlast) # htmlast = htmlparser.parse(webpage, lexer=htmllexer) # graphics.initialize() # interpret(htmlast) # graphics.finalize()
def interpret(ast): # AST for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1]; tagargs = node[2]; subast = node[3]; closetagname = node[4]; if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname,tagargs); interpret(subast) graphics.endtag(); elif nodetype == "javascript-element": jstext = node[1]; jslexer = lex.lex(module=jstokens) if False: # JavaScript lexer/parser debugging print jstext jslexer.input(jstext) while True: tok = jslexer.token() if not tok: break print tok jsparser = yacc.yacc(module=jsgrammar,tabmodule="parsetabjs") jsast = jsparser.parse(jstext,lexer=jslexer) result = jsinterp.interpret(jsast) htmlast = htmlparser.parse(result,lexer=htmllexer) interpret(htmlast)
def interpret(ast): for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1]; tagargs = node[2]; subast = node[3]; closetagname = node[4]; if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname,tagargs); interpret(subast) graphics.endtag(); elif nodetype == "javascript-element": jstext = node[1]; jsast = jsparser.parse(jstext,lexer=jslexer) result = jsinterp.interpret(jsast) # rather than just call graphics we re-interpret here. # graphics.word(result) htmlast = htmlparser.parse(result,lexer=htmllexer) interpret(htmlast)
def interpret(ast): # graphics.initialize() for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1] tagargs = node[2] subast = node[3] closetagname = node[4] if tagname != closetagname: # this error has detected at the parse step. graphics.warning("(mismatched" + tagname + " " + closetagname + ")") else: graphics.begintag(tagname, tagargs) interpret(subast) graphics.endtag() elif nodetype == "javascript-element": jstext = node[1] jslexer = lex.lex(module=jstokens) jsparser = yacc.yacc(module=jsgrammar,tabmodule="parsetabjs") jsast = jsparser.parse(jstext,lexer=jslexer) result = jsinterp.jsinterp(jsast) htmlast = htmlparser.parse(result,lexer=htmllexer) interpret(htmlast)
def interpret(ast): for tree in ast: if tree[0] == "word-element": graphics.word(tree[1]) elif tree[0] == "tag-elemnet": tagname = tree[1] tagargs = tree[2] subtree = tree[3] closetagname = tree[4] if tagname != closename: graphics.warning("tagname not match") else: graphics.begintag(tagname,tagargs) interpret(subtree) graphics.endtag()
def interpret(trees): for tree in trees: nodetype[0] = tree[0] if nodetype == 'word-element': graphics.word(tree[1]) elif nodetype == 'tag-element': tagname = tree[1] tagargs = tree[2] subtrees = tree[3] closetagname = tree[4] if tagname != closetagname: graphics.warning("miscatch tag!") else: graphics.begintag(tagname,tagargs) interpret(subtrees) graphics.endtag()
def interpret (trees): for tree in trees: nodetype = tree[0] if (nodetype == "word-element"): graphics.word(tree[1]) elif (nodetype=="tag-element"): tagname = tree[1] tagargs = tree[2] subtrees = tree[3] closetagname = tree[4] if (tagname <> closetagname): graphics.warning("unbalanced tags") else: graphics.begintag(tagname,tagargs) interpret(subtrees) graphics.endtag()
def interpret(trees): # Hello, friend for tree in trees: # Hello, # ("word-element","Hello") nodetype=tree[0] # "word-element" if nodetype == "word-element": graphics.word(tree[1]) elif nodetype == "tag-element": # <b>Strong text</b> tagname = tree[1] # b tagargs = tree[2] # [] subtrees = tree[3] # ...Strong Text!... closetagname = tree[4] # b elif tagname != closetagname: graphics.warning("Warning mismatched tags") else: graphics.begintag(tagname, tagargs) interpret(subtrees) graphics.endtag()
def interpret(trees): # Hello, friend for tree in trees: # Hello, # ("word-element","Hello") nodetype=tree[0] # "word-element" if nodetype == "word-element": graphics.word(tree[1]) elif nodetype == "tag-element": # <b>Strong text</b> tagname = tree[1] # b tagargs = tree[2] # [] subtrees = tree[3] # ...Strong Text!... closetagname = tree[4] # b if (tagname <> closetagname): graphics.warning("something bad happened:(") # if not use graphics.warning() else: graphics.begintag(tagname,tagargs) interpret(subtrees) graphics.endtag()
def interpret(trees): # Hello, Friend for tree in trees: # Hello # ("word-element", "Hello") nodeType = tree[0] # word-element if nodeType == "word-element": graphics.word(tree[1]) elif nodeType == "tag-element": # <b> Strong Text ! </b> tagName = tree[1] tagArgs = tree[2] # <a href="">Some Text</a> href="" is tagArgs subTrees = tree[3] # Strong Text! closeTagName = tree[4] #checking whether opening tag and close tag elements matches or not if (tagName != closeTagName): graphics.warning("Tags don't match", tagName, closeTagName) else: graphics.begintag(tagName, tagArgs) interpret(subTrees) graphics.endtag()
def interpret(trees): # Hello, friend for tree in trees: # Hello, # ("word-element","Hello") nodetype=tree[0] # "word-element" if nodetype == "word-element": graphics.word(tree[1]) elif nodetype == "tag-element": # <b>Strong text</b> tagname = tree[1] # b tagargs = tree[2] # [] subtrees = tree[3] # ...Strong Text!... closetagname = tree[4] # b # QUIZ: (1) check that the tags match # if not use graphics.warning() # (2): Interpret the subtree # HINT: Call interpret recursively if tagname == closetagname : graphics.begintag(tagname,tagargs) interpret(subtrees) graphics.endtag() else : graphics.warning("WARNING")
def interpret(ast): for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1]; tagargs = node[2]; subast = node[3]; closetagname = node[4]; if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname,tagargs); interpret(subast) graphics.endtag(); elif nodetype == "javascript-element": jstext = node[1]; jsast = jsparser.parse(jstext,lexer=jslexer) result = jsinterp.interpret(jsast) interpret(htmlparser.parse(result,lexer=htmllexer))
def interpret(trees): # Hello, friend for tree in trees: # Hello, # ("word-element","Hello") nodetype = tree[0] # "word-element" if nodetype == "word-element": graphics.word(tree[1]) elif nodetype == "tag-element": # <b>Strong text</b> tagname = tree[1] # b tagargs = tree[2] # [] subtrees = tree[3] # ...Strong Text!... closetagname = tree[4] # b # QUIZ: (1) check that the tags match # if not use graphics.warning() # (2): Interpret the subtree # HINT: Call interpret recursively if tagname <> closetagname: graphics.warning("mismatched tag") else: graphics.begintag(tagname, tagargs) interpret(subtrees) graphics.endtag()
def interpret(trees): # Hello, friend for tree in trees: # Hello, nodetype=tree[0] # "word-element" if nodetype == "word-element": # ("word-element","Hello") graphics.word(tree[1]) elif nodetype == "tag-element": # ("tag-element", tagname, ..., closetagname) # <b>Strong text</b> tagname = tree[1] # b tagargs = tree[2] # [] subtrees = tree[3] # ...Strong Text!... closetagname = tree[4] # b # ensure tags match if tagname != closetagname: graphics.warning("mismatched tag") else: # recursively call interpret on subtree graphics.begintag(tagname, tagargs) interpret(subtrees) graphics.endtag()
def interpret(ast): for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1] tagargs = node[2] subast = node[3] closetagname = node[4] if tagname != closetagname: graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname, tagargs) interpret(subast) graphics.endtag() elif nodetype == "javascript-element": jstext = node[1] jsast = jsparser.parse(jstext, lexer=jslexer) result = jsinterp.interpret(jsast) htmlast = htmlparser.parse(result, lexer=htmllexer) interpret(htmlast)
def interpret(trees): # Hello, friend for tree in trees: # Hello, # ("word-element","Hello") nodetype = tree[0] # "word-element" if nodetype == "word-element": graphics.word(tree[1]) elif nodetype == "tag-element": # <b>Strong text</b> tagname = tree[1] # b tagargs = tree[2] # [] subtrees = tree[3] # ...Strong Text!... closetagname = tree[4] # b if tagname != closetagname: graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname, tagargs) interpret(subtrees) graphics.endtag() elif nodetype == "javascript-element": jstext = node[1] jslexer = lex.lex(module=jstokens) jsparser = yacc.yacc(module=jsgrammar) jstree = jsparser.parse(jstext, lexer=jslexer) result = jsinterp.interpret(jstree) graphics.word(result) ## if False: ## print jstext ## jslexer.input(jstext) ## while True: ## tok = jslexer.token() ## if not tok: break ## print tok jsparser = yacc.yacc(module=jsgrammar, tabmodule="parsetabjs") jsast = jsparser.parse(jstext, lexer=jslexer) result = jsinterp.interpret(jsast) graphics.word(result)
def interpret(ast): for node in ast: nodetype = node[0] if nodetype == "word-element": # eg: ("word-element", "hello") graphics.word(node[1]) elif nodetype == "tag-element": # eg: <b class="info"> strong text! </b> tagname = node[1] # b tagargs = node[2] # {"class": "info"} subast = node[3] # ... Strong text! closetagname = node[4] # b if tagname != closetagname: graphics.warning("(mismatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname, tagargs) interpret(subast) # 递归解释标签内的html内容. graphics.endtag() elif nodetype == "javascript-element": jstext = node[1] pass
def interpret(trees): # Hello, friend for tree in trees: # Hello, # ("word-element","Hello") nodetype=tree[0] # "word-element" if nodetype == "word-element": graphics.word(tree[1]) elif nodetype == "tag-element": # <b>Strong text</b> tagname = tree[1] # b tagargs = tree[2] # [] subtrees = tree[3] # ...Strong Text!... closetagname = tree[4] # b if tagname != closetagname: graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname, tagargs) interpret(subtrees) graphics.endtag() elif nodetype == "javascript-element": jstext = node[1] jslexer = lex.lex(module = jstokens) jsparser = yacc.yacc(module=jsgrammar) jstree = jsparser.parse(jstext, lexer=jslexer) result = jsinterp.interpret(jstree) graphics.word(result) ## if False: ## print jstext ## jslexer.input(jstext) ## while True: ## tok = jslexer.token() ## if not tok: break ## print tok jsparser = yacc.yacc(module=jsgrammar,tabmodule="parsetabjs") jsast = jsparser.parse(jstext,lexer=jslexer) result = jsinterp.interpret(jsast) graphics.word(result)
def interpret(ast): for node in ast: nodetype = node[0] if nodetype == "word-element": graphics.word(node[1]) elif nodetype == "tag-element": tagname = node[1]; tagargs = node[2]; subast = node[3]; closetagname = node[4]; if (tagname <> closetagname): graphics.warning("(mistmatched " + tagname + " " + closetagname + ")") else: graphics.begintag(tagname,tagargs); interpret(subast) graphics.endtag(); elif nodetype == "javascript-element": jstext = node[1]; jsast = jsparser.parse(jstext,lexer=jslexer) result = jsinterp.interpret(jsast) # result1 = "<b>This whole sentence should be bold, and the concepts in this problem touch on the <a href='http://en.wikipedia.org/wiki/Document_Object_Model'>Document Object Model</a>, which allows web browsers and scripts to <i>manipulate</i> webpages.</b>" # result2 = '<i>10.0</i><b>9.0</b><i>8.0</i><b>7.0</b><i>6.0</i><b>5.0</b><i>4.0</i><b>3.0</b><i>2.0</i><b>1.0</b>' gHtml = htmlparser.parse(result,lexer=htmllexer) interpret(gHtml)
import graphics graphics.initialize() graphics.begintag(tagname, tagargs) interpret(subtrees) graphics.endtag()