Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pybison
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
pybison
Commits
2331ecff
Commit
2331ecff
authored
Dec 08, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code cleanup and applied pyflakes' suggestions.
parent
3e04c23b
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
33 additions
and
37 deletions
+33
-37
doc/calc.py
doc/calc.py
+14
-16
examples/calc/calc.py
examples/calc/calc.py
+14
-16
examples/calc/run.py
examples/calc/run.py
+0
-1
examples/calc1/calc1.py
examples/calc1/calc1.py
+3
-2
examples/java/run.py
examples/java/run.py
+1
-1
examples/template/template.py
examples/template/template.py
+1
-1
No files found.
doc/calc.py
View file @
2331ecff
...
...
@@ -2,9 +2,7 @@
"""
A simple pybison parser program implementing a calculator
"""
import
sys
from
bison
import
BisonParser
,
BisonNode
from
bison
import
BisonParser
class
Parser
(
BisonParser
):
"""
...
...
@@ -18,7 +16,7 @@ class Parser(BisonParser):
'PLUS'
,
'MINUS'
,
'TIMES'
,
'DIVIDE'
,
'POW'
,
'LPAREN'
,
'RPAREN'
,
'NEWLINE'
,
'QUIT'
]
# ------------------------------
# precedences
# ------------------------------
...
...
@@ -28,7 +26,7 @@ class Parser(BisonParser):
(
'left'
,
(
'NEG'
,
)),
(
'right'
,
(
'POW'
,
)),
)
# ------------------------------------------------------------------
# override default read method with a version that prompts for input
# ------------------------------------------------------------------
...
...
@@ -37,7 +35,7 @@ class Parser(BisonParser):
return
raw_input
(
"> "
)
+
"
\
n
"
except
EOFError
:
return
''
# ---------------------------------------------------------------
# These methods are the python handlers for the bison targets.
# (which get called by the bison code each time the corresponding
...
...
@@ -47,17 +45,17 @@ class Parser(BisonParser):
# you are doing - they are in bison rule syntax, and are passed
# verbatim to bison to build the parser engine library.
# ---------------------------------------------------------------
# Declare the start target here (by name)
start
=
"input"
def
on_input
(
self
,
target
,
option
,
names
,
values
):
"""
input :
| input line
"""
return
def
on_line
(
self
,
target
,
option
,
names
,
values
):
"""
line : NEWLINE
...
...
@@ -65,7 +63,7 @@ class Parser(BisonParser):
"""
if
option
==
1
:
print
values
[
0
]
def
on_exp
(
self
,
target
,
option
,
names
,
values
):
"""
exp : NUMBER
...
...
@@ -94,7 +92,7 @@ class Parser(BisonParser):
return
values
[
0
]
**
values
[
2
]
elif
option
==
7
:
return
values
[
1
]
# -----------------------------------------
# raw lex script, verbatim here
# -----------------------------------------
...
...
@@ -111,9 +109,9 @@ class Parser(BisonParser):
#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); }
%}
%%
[0-9]+ { returntoken(NUMBER); }
"(" { returntoken(LPAREN); }
")" { returntoken(RPAREN); }
...
...
@@ -123,13 +121,13 @@ class Parser(BisonParser):
"**" { returntoken(POW); }
"/" { returntoken(DIVIDE); }
"quit" { printf("lex: got QUIT\n"); yyterminate(); returntoken(QUIT); }
[ \t\v\f] {}
[\n] {yylineno++; returntoken(NEWLINE); }
. { printf("unknown char %c ignored, yytext=0x%lx\n", yytext[0], yytext); /* ignore bad chars */}
%%
yywrap() { return(1); }
"""
...
...
examples/calc/calc.py
View file @
2331ecff
...
...
@@ -2,9 +2,7 @@
"""
A simple pybison parser program implementing a calculator
"""
import
sys
from
bison
import
BisonParser
,
BisonNode
from
bison
import
BisonParser
class
Parser
(
BisonParser
):
"""
...
...
@@ -18,7 +16,7 @@ class Parser(BisonParser):
'PLUS'
,
'MINUS'
,
'TIMES'
,
'DIVIDE'
,
'POW'
,
'LPAREN'
,
'RPAREN'
,
'NEWLINE'
,
'QUIT'
]
# ------------------------------
# precedences
# ------------------------------
...
...
@@ -28,7 +26,7 @@ class Parser(BisonParser):
(
'left'
,
(
'NEG'
,
)),
(
'right'
,
(
'POW'
,
)),
)
# ------------------------------------------------------------------
# override default read method with a version that prompts for input
# ------------------------------------------------------------------
...
...
@@ -37,7 +35,7 @@ class Parser(BisonParser):
return
raw_input
(
"> "
)
+
"
\
n
"
except
EOFError
:
return
''
# ---------------------------------------------------------------
# These methods are the python handlers for the bison targets.
# (which get called by the bison code each time the corresponding
...
...
@@ -47,17 +45,17 @@ class Parser(BisonParser):
# you are doing - they are in bison rule syntax, and are passed
# verbatim to bison to build the parser engine library.
# ---------------------------------------------------------------
# Declare the start target here (by name)
start
=
"input"
def
on_input
(
self
,
target
,
option
,
names
,
values
):
"""
input :
| input line
"""
return
def
on_line
(
self
,
target
,
option
,
names
,
values
):
"""
line : NEWLINE
...
...
@@ -65,7 +63,7 @@ class Parser(BisonParser):
"""
if
option
==
1
:
print
values
[
0
]
def
on_exp
(
self
,
target
,
option
,
names
,
values
):
"""
exp : NUMBER
...
...
@@ -94,7 +92,7 @@ class Parser(BisonParser):
return
values
[
0
]
**
values
[
2
]
elif
option
==
7
:
return
values
[
1
]
# -----------------------------------------
# raw lex script, verbatim here
# -----------------------------------------
...
...
@@ -111,9 +109,9 @@ class Parser(BisonParser):
#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); }
%}
%%
[0-9]+ { returntoken(NUMBER); }
"(" { returntoken(LPAREN); }
")" { returntoken(RPAREN); }
...
...
@@ -123,13 +121,13 @@ class Parser(BisonParser):
"**" { returntoken(POW); }
"/" { returntoken(DIVIDE); }
"quit" { printf("lex: got QUIT\n"); yyterminate(); returntoken(QUIT); }
[ \t\v\f] {}
[\n] {yylineno++; returntoken(NEWLINE); }
. { printf("unknown char %c ignored, yytext=0x%lx\n", yytext[0], yytext); /* ignore bad chars */}
%%
yywrap() { return(1); }
"""
...
...
examples/calc/run.py
View file @
2331ecff
#!/usr/bin/env python
import
readline
import
sys
sys
.
path
.
insert
(
0
,
'../../build/lib.linux-x86_64-2.7/'
)
...
...
examples/calc1/calc1.py
View file @
2331ecff
...
...
@@ -3,9 +3,10 @@
A more advanced calculator example, with variable storage and scientific
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
):
"""
...
...
examples/java/run.py
View file @
2331ecff
...
...
@@ -25,7 +25,7 @@ else:
src
=
"I2PClient.java"
p
=
myjava
.
Parser
(
verbose
=
verbose
)
p
=
javaparser
.
Parser
(
verbose
=
verbose
)
print
"delmebld.py: running parser on HelloWorldApp.java"
res
=
p
.
run
(
file
=
src
)
...
...
examples/template/template.py
View file @
2331ecff
...
...
@@ -11,7 +11,7 @@ You can do much worse than to copy this file
somewhere, and tinker away to your heart's content.
"""
import
sys
,
traceback
import
sys
from
bison
import
BisonParser
,
BisonNode
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment