Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
trs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
trs
Commits
8c9a6c6e
Commit
8c9a6c6e
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Introduces FUNCTION token instead of seperate tokens for each function.
parent
6139bd82
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/node.py
+12
-6
12 additions, 6 deletions
src/node.py
src/parser.py
+24
-14
24 additions, 14 deletions
src/parser.py
with
36 additions
and
20 deletions
src/node.py
+
12
−
6
View file @
8c9a6c6e
...
...
@@ -79,12 +79,18 @@ TOKEN_MAP = {
OP_MUL
:
'
TIMES
'
,
OP_DIV
:
'
DIVIDE
'
,
OP_POW
:
'
POW
'
,
OP_SQRT
:
'
SQRT
'
,
OP_SIN
:
'
SIN
'
,
OP_COS
:
'
COS
'
,
OP_TAN
:
'
TAN
'
,
OP_INT
:
'
INT
'
,
OP_SOLVE
:
'
SOLVE
'
,
OP_SQRT
:
'
FUNCTION
'
,
OP_SIN
:
'
FUNCTION
'
,
OP_COS
:
'
FUNCTION
'
,
OP_TAN
:
'
FUNCTION
'
,
OP_INT
:
'
FUNCTION
'
,
OP_SOLVE
:
'
FUNCTION
'
,
#OP_SQRT: 'SQRT',
#OP_SIN: 'SIN',
#OP_COS: 'COS',
#OP_TAN: 'TAN',
#OP_INT: 'INT',
#OP_SOLVE: 'SOLVE',
OP_EQ
:
'
EQ
'
,
OP_POSSIBILITIES
:
'
POSSIBILITIES
'
,
OP_HINT
:
'
HINT
'
,
...
...
This diff is collapsed.
Click to expand it.
src/parser.py
+
24
−
14
View file @
8c9a6c6e
...
...
@@ -51,8 +51,9 @@ class Parser(BisonParser):
# ----------------------------------------------------------------
# TODO: add a runtime check to verify that this token list match the list
# of tokens of the lex script.
tokens
=
[
'
NUMBER
'
,
'
IDENTIFIER
'
,
'
NEWLINE
'
,
'
QUIT
'
,
'
RAISE
'
,
'
GRAPH
'
,
\
'
LPAREN
'
,
'
RPAREN
'
]
+
TOKEN_MAP
.
values
()
tokens
=
[
'
NUMBER
'
,
'
IDENTIFIER
'
,
'
NEWLINE
'
,
'
QUIT
'
,
'
RAISE
'
,
'
GRAPH
'
,
'
LPAREN
'
,
'
RPAREN
'
,
'
FUNCTION
'
]
\
+
filter
(
lambda
t
:
t
!=
'
FUNCTION
'
,
TOKEN_MAP
.
values
())
# ------------------------------
# precedences
...
...
@@ -64,7 +65,8 @@ class Parser(BisonParser):
(
'
left
'
,
(
'
EQ
'
,
)),
(
'
left
'
,
(
'
NEG
'
,
)),
(
'
right
'
,
(
'
POW
'
,
)),
(
'
right
'
,
(
'
SIN
'
,
'
COS
'
,
'
TAN
'
,
'
SOLVE
'
,
'
INT
'
,
'
SQRT
'
)),
(
'
right
'
,
(
'
FUNCTION
'
,
)),
#('right', ('SIN', 'COS', 'TAN', 'SOLVE', 'INT', 'SQRT')),
)
interactive
=
0
...
...
@@ -152,11 +154,14 @@ class Parser(BisonParser):
+
'
|([a-z])\s*([0-9])
'
# match: a4 result: a ^ 4
+
'
|([0-9])\s+([0-9]))
'
)
# match: 4 4 result: 4 * 4
words
=
zip
(
*
filter
(
lambda
(
s
,
op
):
TOKEN_MAP
[
op
]
==
'
FUNCTION
'
,
\
OP_MAP
.
iteritems
()))[
0
]
+
(
'
raise
'
,
'
graph
'
)
def
preprocess_data
(
match
):
left
,
right
=
filter
(
None
,
match
.
groups
())
# Filter words (otherwise they will be preprocessed as well)
if
(
left
+
right
).
upper
()
in
self
.
token
s
:
if
left
+
right
in
word
s
:
return
left
+
right
# If all characters on the right are numbers. e.g. "a4", the
...
...
@@ -336,12 +341,7 @@ class Parser(BisonParser):
def
on_unary
(
self
,
target
,
option
,
names
,
values
):
"""
unary : MINUS exp %prec NEG
| SIN exp
| COS exp
| TAN exp
| INT exp
| SOLVE exp
| SQRT exp
| FUNCTION exp
"""
if
option
==
0
:
# rule: NEG exp
...
...
@@ -354,9 +354,10 @@ class Parser(BisonParser):
return
values
[
1
]
if
option
<
7
:
# rule:
SIN exp | COS exp | TAN exp | INT
exp
if
values
[
1
].
type
==
TYPE_OPERATOR
and
values
[
1
].
op
==
OP_COMMA
:
if
option
==
1
:
# rule:
FUNCTION
exp
if
values
[
1
].
is_op
(
OP_COMMA
)
:
return
Node
(
values
[
0
],
*
values
[
1
])
return
Node
(
*
values
)
raise
BisonSyntaxError
(
'
Unsupported option %d in target
"
%s
"
.
'
...
...
@@ -408,10 +409,19 @@ class Parser(BisonParser):
# operator tokens
# -----------------------------------------
operators
=
''
functions
=
[]
for
op_str
,
op
in
OP_MAP
.
iteritems
():
operators
+=
'"
%s
"
%s{ returntoken(%s); }
\n
'
\
%
(
op_str
,
'
'
*
(
8
-
len
(
op_str
)),
TOKEN_MAP
[
op
])
if
TOKEN_MAP
[
op
]
==
'
FUNCTION
'
:
functions
.
append
(
op_str
)
else
:
operators
+=
'"
%s
"
%s{ returntoken(%s); }
\n
'
\
%
(
op_str
,
'
'
*
(
8
-
len
(
op_str
)),
TOKEN_MAP
[
op
])
# Put all functions in a single regex
if
functions
:
operators
+=
'
(
"
%s
"
) { returntoken(FUNCTION); }
\n
'
\
%
'"
|
"'
.
join
(
functions
)
# -----------------------------------------
# raw lex script, verbatim here
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment