Code cleanup and applied pyflakes' suggestions.

parent 3e04c23b
...@@ -2,9 +2,7 @@ ...@@ -2,9 +2,7 @@
""" """
A simple pybison parser program implementing a calculator A simple pybison parser program implementing a calculator
""" """
import sys from bison import BisonParser
from bison import BisonParser, BisonNode
class Parser(BisonParser): class Parser(BisonParser):
""" """
...@@ -18,7 +16,7 @@ class Parser(BisonParser): ...@@ -18,7 +16,7 @@ class Parser(BisonParser):
'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'POW', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'POW',
'LPAREN', 'RPAREN', 'LPAREN', 'RPAREN',
'NEWLINE', 'QUIT'] 'NEWLINE', 'QUIT']
# ------------------------------ # ------------------------------
# precedences # precedences
# ------------------------------ # ------------------------------
...@@ -28,7 +26,7 @@ class Parser(BisonParser): ...@@ -28,7 +26,7 @@ class Parser(BisonParser):
('left', ('NEG', )), ('left', ('NEG', )),
('right', ('POW', )), ('right', ('POW', )),
) )
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# override default read method with a version that prompts for input # override default read method with a version that prompts for input
# ------------------------------------------------------------------ # ------------------------------------------------------------------
...@@ -37,7 +35,7 @@ class Parser(BisonParser): ...@@ -37,7 +35,7 @@ class Parser(BisonParser):
return raw_input("> ") + "\n" return raw_input("> ") + "\n"
except EOFError: except EOFError:
return '' return ''
# --------------------------------------------------------------- # ---------------------------------------------------------------
# These methods are the python handlers for the bison targets. # These methods are the python handlers for the bison targets.
# (which get called by the bison code each time the corresponding # (which get called by the bison code each time the corresponding
...@@ -47,17 +45,17 @@ class Parser(BisonParser): ...@@ -47,17 +45,17 @@ class Parser(BisonParser):
# you are doing - they are in bison rule syntax, and are passed # you are doing - they are in bison rule syntax, and are passed
# verbatim to bison to build the parser engine library. # verbatim to bison to build the parser engine library.
# --------------------------------------------------------------- # ---------------------------------------------------------------
# Declare the start target here (by name) # Declare the start target here (by name)
start = "input" start = "input"
def on_input(self, target, option, names, values): def on_input(self, target, option, names, values):
""" """
input : input :
| input line | input line
""" """
return return
def on_line(self, target, option, names, values): def on_line(self, target, option, names, values):
""" """
line : NEWLINE line : NEWLINE
...@@ -65,7 +63,7 @@ class Parser(BisonParser): ...@@ -65,7 +63,7 @@ class Parser(BisonParser):
""" """
if option == 1: if option == 1:
print values[0] print values[0]
def on_exp(self, target, option, names, values): def on_exp(self, target, option, names, values):
""" """
exp : NUMBER exp : NUMBER
...@@ -94,7 +92,7 @@ class Parser(BisonParser): ...@@ -94,7 +92,7 @@ class Parser(BisonParser):
return values[0] ** values[2] return values[0] ** values[2]
elif option == 7: elif option == 7:
return values[1] return values[1]
# ----------------------------------------- # -----------------------------------------
# raw lex script, verbatim here # raw lex script, verbatim here
# ----------------------------------------- # -----------------------------------------
...@@ -111,9 +109,9 @@ class Parser(BisonParser): ...@@ -111,9 +109,9 @@ class Parser(BisonParser):
#define returntoken(tok) yylval = PyString_FromString(strdup(yytext)); return (tok); #define returntoken(tok) yylval = PyString_FromString(strdup(yytext)); return (tok);
#define YY_INPUT(buf,result,max_size) { (*py_input)(py_parser, buf, &result, max_size); } #define YY_INPUT(buf,result,max_size) { (*py_input)(py_parser, buf, &result, max_size); }
%} %}
%% %%
[0-9]+ { returntoken(NUMBER); } [0-9]+ { returntoken(NUMBER); }
"(" { returntoken(LPAREN); } "(" { returntoken(LPAREN); }
")" { returntoken(RPAREN); } ")" { returntoken(RPAREN); }
...@@ -123,13 +121,13 @@ class Parser(BisonParser): ...@@ -123,13 +121,13 @@ class Parser(BisonParser):
"**" { returntoken(POW); } "**" { returntoken(POW); }
"/" { returntoken(DIVIDE); } "/" { returntoken(DIVIDE); }
"quit" { printf("lex: got QUIT\n"); yyterminate(); returntoken(QUIT); } "quit" { printf("lex: got QUIT\n"); yyterminate(); returntoken(QUIT); }
[ \t\v\f] {} [ \t\v\f] {}
[\n] {yylineno++; returntoken(NEWLINE); } [\n] {yylineno++; returntoken(NEWLINE); }
. { printf("unknown char %c ignored, yytext=0x%lx\n", yytext[0], yytext); /* ignore bad chars */} . { printf("unknown char %c ignored, yytext=0x%lx\n", yytext[0], yytext); /* ignore bad chars */}
%% %%
yywrap() { return(1); } yywrap() { return(1); }
""" """
......
...@@ -2,9 +2,7 @@ ...@@ -2,9 +2,7 @@
""" """
A simple pybison parser program implementing a calculator A simple pybison parser program implementing a calculator
""" """
import sys from bison import BisonParser
from bison import BisonParser, BisonNode
class Parser(BisonParser): class Parser(BisonParser):
""" """
...@@ -18,7 +16,7 @@ class Parser(BisonParser): ...@@ -18,7 +16,7 @@ class Parser(BisonParser):
'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'POW', 'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'POW',
'LPAREN', 'RPAREN', 'LPAREN', 'RPAREN',
'NEWLINE', 'QUIT'] 'NEWLINE', 'QUIT']
# ------------------------------ # ------------------------------
# precedences # precedences
# ------------------------------ # ------------------------------
...@@ -28,7 +26,7 @@ class Parser(BisonParser): ...@@ -28,7 +26,7 @@ class Parser(BisonParser):
('left', ('NEG', )), ('left', ('NEG', )),
('right', ('POW', )), ('right', ('POW', )),
) )
# ------------------------------------------------------------------ # ------------------------------------------------------------------
# override default read method with a version that prompts for input # override default read method with a version that prompts for input
# ------------------------------------------------------------------ # ------------------------------------------------------------------
...@@ -37,7 +35,7 @@ class Parser(BisonParser): ...@@ -37,7 +35,7 @@ class Parser(BisonParser):
return raw_input("> ") + "\n" return raw_input("> ") + "\n"
except EOFError: except EOFError:
return '' return ''
# --------------------------------------------------------------- # ---------------------------------------------------------------
# These methods are the python handlers for the bison targets. # These methods are the python handlers for the bison targets.
# (which get called by the bison code each time the corresponding # (which get called by the bison code each time the corresponding
...@@ -47,17 +45,17 @@ class Parser(BisonParser): ...@@ -47,17 +45,17 @@ class Parser(BisonParser):
# you are doing - they are in bison rule syntax, and are passed # you are doing - they are in bison rule syntax, and are passed
# verbatim to bison to build the parser engine library. # verbatim to bison to build the parser engine library.
# --------------------------------------------------------------- # ---------------------------------------------------------------
# Declare the start target here (by name) # Declare the start target here (by name)
start = "input" start = "input"
def on_input(self, target, option, names, values): def on_input(self, target, option, names, values):
""" """
input : input :
| input line | input line
""" """
return return
def on_line(self, target, option, names, values): def on_line(self, target, option, names, values):
""" """
line : NEWLINE line : NEWLINE
...@@ -65,7 +63,7 @@ class Parser(BisonParser): ...@@ -65,7 +63,7 @@ class Parser(BisonParser):
""" """
if option == 1: if option == 1:
print values[0] print values[0]
def on_exp(self, target, option, names, values): def on_exp(self, target, option, names, values):
""" """
exp : NUMBER exp : NUMBER
...@@ -94,7 +92,7 @@ class Parser(BisonParser): ...@@ -94,7 +92,7 @@ class Parser(BisonParser):
return values[0] ** values[2] return values[0] ** values[2]
elif option == 7: elif option == 7:
return values[1] return values[1]
# ----------------------------------------- # -----------------------------------------
# raw lex script, verbatim here # raw lex script, verbatim here
# ----------------------------------------- # -----------------------------------------
...@@ -111,9 +109,9 @@ class Parser(BisonParser): ...@@ -111,9 +109,9 @@ class Parser(BisonParser):
#define returntoken(tok) yylval = PyString_FromString(strdup(yytext)); return (tok); #define returntoken(tok) yylval = PyString_FromString(strdup(yytext)); return (tok);
#define YY_INPUT(buf,result,max_size) { (*py_input)(py_parser, buf, &result, max_size); } #define YY_INPUT(buf,result,max_size) { (*py_input)(py_parser, buf, &result, max_size); }
%} %}
%% %%
[0-9]+ { returntoken(NUMBER); } [0-9]+ { returntoken(NUMBER); }
"(" { returntoken(LPAREN); } "(" { returntoken(LPAREN); }
")" { returntoken(RPAREN); } ")" { returntoken(RPAREN); }
...@@ -123,13 +121,13 @@ class Parser(BisonParser): ...@@ -123,13 +121,13 @@ class Parser(BisonParser):
"**" { returntoken(POW); } "**" { returntoken(POW); }
"/" { returntoken(DIVIDE); } "/" { returntoken(DIVIDE); }
"quit" { printf("lex: got QUIT\n"); yyterminate(); returntoken(QUIT); } "quit" { printf("lex: got QUIT\n"); yyterminate(); returntoken(QUIT); }
[ \t\v\f] {} [ \t\v\f] {}
[\n] {yylineno++; returntoken(NEWLINE); } [\n] {yylineno++; returntoken(NEWLINE); }
. { printf("unknown char %c ignored, yytext=0x%lx\n", yytext[0], yytext); /* ignore bad chars */} . { printf("unknown char %c ignored, yytext=0x%lx\n", yytext[0], yytext); /* ignore bad chars */}
%% %%
yywrap() { return(1); } yywrap() { return(1); }
""" """
......
#!/usr/bin/env python #!/usr/bin/env python
import readline
import sys import sys
sys.path.insert(0, '../../build/lib.linux-x86_64-2.7/') sys.path.insert(0, '../../build/lib.linux-x86_64-2.7/')
......
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
A more advanced calculator example, with variable storage and scientific A more advanced calculator example, with variable storage and scientific
functions (courtesy of python 'math' module) functions (courtesy of python 'math' module)
""" """
import sys, math, readline import math
from bison import BisonParser
from bison import BisonParser, BisonNode, BisonError
class Parser(BisonParser): class Parser(BisonParser):
""" """
......
...@@ -25,7 +25,7 @@ else: ...@@ -25,7 +25,7 @@ else:
src = "I2PClient.java" src = "I2PClient.java"
p = myjava.Parser(verbose=verbose) p = javaparser.Parser(verbose=verbose)
print "delmebld.py: running parser on HelloWorldApp.java" print "delmebld.py: running parser on HelloWorldApp.java"
res = p.run(file=src) res = p.run(file=src)
......
...@@ -11,7 +11,7 @@ You can do much worse than to copy this file ...@@ -11,7 +11,7 @@ You can do much worse than to copy this file
somewhere, and tinker away to your heart's content. somewhere, and tinker away to your heart's content.
""" """
import sys, traceback import sys
from bison import BisonParser, BisonNode from bison import BisonParser, BisonNode
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment