PATH: //opt/alt/python311/share/doc/alt-python311-pyparsing-doc/examples
FILE_BARU
CREATE
FOLDER_BARU
MKDIR
UPLOAD_FILE
GO
[ .. KEMBALI ]
📄 0README.html
↓
X
📄 AcManForm.dfm
↓
X
📄 LAparser.py
↓
X
📄 Setup.ini
↓
X
📄 SimpleCalc.py
↓
X
📄 SingleForm.dfm
↓
X
📄 TAP.py
↓
X
📄 __init__.py
↓
X
📁 __pycache__/
X
📄 adventureEngine.py
↓
X
📄 antlr_grammar.py
↓
X
📄 antlr_grammar_tests.py
↓
X
📄 apicheck.py
↓
X
📄 bigquery_view_parser.py
↓
X
📄 booleansearchparser.py
↓
X
📄 btpyparse.py
↓
X
📄 builtin_parse_action_demo.py
↓
X
📄 cLibHeader.py
↓
X
📄 chemicalFormulas.py
↓
X
📄 commasep.py
↓
X
📄 configParse.py
↓
X
📄 cpp_enum_parser.py
↓
X
📄 cuneiform_python.py
↓
X
📄 datetimeParseActions.py
↓
X
📄 decaf_parser.py
↓
X
📄 delta_time.py
↓
X
📄 dfmparse.py
↓
X
📄 dhcpd_leases_parser.py
↓
X
📄 dictExample.py
↓
X
📄 dictExample2.py
↓
X
📄 ebnf.py
↓
X
📄 ebnftest.py
↓
X
📄 eval_arith.py
↓
X
📄 excelExpr.py
↓
X
📄 fourFn.py
↓
X
📄 gen_ctypes.py
↓
X
📄 getNTPserversNew.py
↓
X
📄 greeting.py
↓
X
📄 greetingInGreek.py
↓
X
📄 greetingInKorean.py
↓
X
📄 groupUsingListAllMatches.py
↓
X
📄 holaMundo.py
↓
X
📄 htmlStripper.py
↓
X
📄 htmlTableParser.py
↓
X
📄 httpServerLogParser.py
↓
X
📄 idlParse.py
↓
X
📄 include_preprocessor.py
↓
X
📄 indentedGrammarExample.py
↓
X
📄 indented_block_example.py
↓
X
📄 invRegex.py
↓
X
📄 javascript_grammar.g
↓
X
📄 jsonParser.py
↓
X
📄 left_recursion.py
↓
X
📄 linenoExample.py
↓
X
📄 listAllMatches.py
↓
X
📄 lua_parser.py
↓
X
📄 lucene_grammar.py
↓
X
📄 macroExpander.py
↓
X
📄 make_diagram.py
↓
X
📄 matchPreviousDemo.py
↓
X
📄 mozilla.ics
↓
X
📄 mozillaCalendarParser.py
↓
X
📄 nested.py
↓
X
📄 nested_markup.py
↓
X
📄 number_words.py
↓
X
📄 numerics.py
↓
X
📄 oc.py
↓
X
📄 one_to_ninety_nine.py
↓
X
📄 parsePythonValue.py
↓
X
📄 parseResultsSumExample.py
↓
X
📄 parseTabularData.py
↓
X
📄 partial_gene_match.py
↓
X
📄 pgn.py
↓
X
📄 position.py
↓
X
📄 protobuf_parser.py
↓
X
📄 pymicko.py
↓
X
📄 pythonGrammarParser.py
↓
X
📄 railroad_diagram_demo.py
↓
X
📄 rangeCheck.py
↓
X
📄 readJson.py
↓
X
📄 removeLineBreaks.py
↓
X
📄 romanNumerals.py
↓
X
📄 rosettacode.py
↓
X
📄 scanExamples.py
↓
X
📄 searchParserAppDemo.py
↓
X
📄 searchparser.py
↓
X
📄 select_parser.py
↓
X
📄 sexpParser.py
↓
X
📄 shapes.py
↓
X
📄 simpleArith.py
↓
X
📄 simpleBool.py
↓
X
📄 simpleSQL.py
↓
X
📄 simpleWiki.py
↓
X
📄 snmp_api.h
↓
X
📄 sparser.py
↓
X
📄 sql2dot.py
↓
X
📄 stackish.py
↓
X
📁 statemachine/
X
📄 test_bibparse.py
↓
X
📄 unicode_denormalizer.py
↓
X
📄 urlExtractor.py
↓
X
📄 urlExtractorNew.py
↓
X
📁 verilog/
X
📄 verilogParse.py
↓
X
📄 withAttribute.py
↓
X
📄 wordsToNum.py
↓
X
SAVING...
BERHASIL DIUBAH!
EDITING: SimpleCalc.py
# SimpleCalc.py # # Demonstration of the parsing module, # Sample usage # # $ python SimpleCalc.py # Type in the string to be parse or 'quit' to exit the program # > g=67.89 + 7/5 # 69.29 # > g # 69.29 # > h=(6*g+8.8)-g # 355.25 # > h + 1 # 356.25 # > 87.89 + 7/5 # 89.29 # > ans+10 # 99.29 # > quit # Good bye! # # # Uncomment the line below for readline support on interactive terminal # import readline from pyparsing import ParseException, Word, alphas, alphanums # Debugging flag can be set to either "debug_flag=True" or "debug_flag=False" debug_flag = False variables = {} from fourFn import BNF, exprStack, evaluate_stack # from fourFn import BNF, exprStack, fn, opn # def evaluateStack( s ): # op = s.pop() # if op == 'unary -': # return -evaluateStack( s ) # if op in "+-*/^": # op2 = evaluateStack( s ) # op1 = evaluateStack( s ) # return opn[op]( op1, op2 ) # elif op == "PI": # return math.pi # 3.1415926535 # elif op == "E": # return math.e # 2.718281828 # elif op in fn: # return fn[op]( evaluateStack( s ) ) # elif op[0].isalpha(): # if op in variables: # return variables[op] # raise Exception("invalid identifier '%s'" % op) # else: # return float( op ) arithExpr = BNF() ident = Word(alphas, alphanums).setName("identifier") assignment = ident("varname") + "=" + arithExpr pattern = assignment | arithExpr if __name__ == "__main__": # input_string input_string = "" # Display instructions on how to quit the program print("Type in the string to be parsed or 'quit' to exit the program") input_string = input("> ") while input_string.strip().lower() != "quit": if input_string.strip().lower() == "debug": debug_flag = True input_string = input("> ") continue # Reset to an empty exprStack del exprStack[:] if input_string != "": # try parsing the input string try: L = pattern.parseString(input_string, parseAll=True) except ParseException as err: L = ["Parse Failure", input_string, (str(err), err.line, err.column)] # show result of parsing the input string if debug_flag: print(input_string, "->", L) if len(L) == 0 or L[0] != "Parse Failure": if debug_flag: print("exprStack=", exprStack) for i, ob in enumerate(exprStack): if isinstance(ob, str) and ob in variables: exprStack[i] = str(variables[ob]) # calculate result , store a copy in ans , display the result to user try: result = evaluate_stack(exprStack) except Exception as e: print(str(e)) else: variables["ans"] = result print(result) # Assign result to a variable if required if L.varname: variables[L.varname] = result if debug_flag: print("variables=", variables) else: print("Parse Failure") err_str, err_line, err_col = L[-1] print(err_line) print(" " * (err_col - 1) + "^") print(err_str) # obtain new input string input_string = input("> ") # if user type 'quit' then say goodbye print("Good bye!")
SIMPAN PERUBAHAN