Skip to content

Instantly share code, notes, and snippets.

@jugmac00
Created June 7, 2020 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jugmac00/d2acada18753d5aea1bca6a39e0abd3f to your computer and use it in GitHub Desktop.
Save jugmac00/d2acada18753d5aea1bca6a39e0abd3f to your computer and use it in GitHub Desktop.
add almost equal operator
diff --git a/Grammar/Grammar b/Grammar/Grammar
index 170518af74..25067147bf 100644
--- a/Grammar/Grammar
+++ b/Grammar/Grammar
@@ -138,7 +138,7 @@ not_test: 'not' not_test | comparison
comparison: expr (comp_op expr)*
# <> isn't actually a valid comparison operator in Python. It's here for the
# sake of a __future__ import described in PEP 401 (which really works :-)
-comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
+comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'~='|'in'|'not' 'in'|'is'|'is' 'not'
star_expr: '*' expr
expr: xor_expr ('|' xor_expr)*
xor_expr: and_expr ('^' and_expr)*
diff --git a/Grammar/Tokens b/Grammar/Tokens
index 9de2da5d15..c2451f3964 100644
--- a/Grammar/Tokens
+++ b/Grammar/Tokens
@@ -53,6 +53,7 @@ ATEQUAL '@='
RARROW '->'
ELLIPSIS '...'
COLONEQUAL ':='
+ALMOSTEQUAL '~='
@jugmac00
Copy link
Author

jugmac00 commented Jun 7, 2020

So, I think there are three problems with the current version cpython-internals-2020-06-03.pdf and the adding of the almost equal operator

  • not mentioning to add ALMOSTEQUAL '~=' to Grammar/Tokens
  • usage of make regen-all (which produces this pyconfig.h problem) instead of make regen-grammar
  • not mentioning to start python as ./python -X oldparser

@jugmac00
Copy link
Author

jugmac00 commented Jun 7, 2020

On page 117 there seems to be another problem - it is written...

Now recompile CPython and open up a REPL to test the command:
>>> a = 1
>>> b = 1.0
>>> a ~= b
True

But further down you can read, that "At this stage, the tokenizer and the AST can parse this code, but the
compiler won’t know how to handle the operator. "

So, I guess, the above snippet should be deleted here - and you just have to compile Python, but do not try the almost equal operator on the REPL - or do.. a funny XKCD message is shown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment