Compare commits

...

4 Commits

Author SHA1 Message Date
Peter Norvig 082a0c212d
Update BASIC.ipynb 2024-04-17 18:11:05 -07:00
Peter Norvig 66e6dd9273
Update BASIC.ipynb 2024-04-17 18:07:22 -07:00
Peter Norvig ec29440c14
Update BASIC.ipynb 2024-04-17 18:03:33 -07:00
Peter Norvig b7366db896
Update BASIC.ipynb 2024-04-17 17:09:35 -07:00
1 changed files with 2 additions and 7 deletions

View File

@ -84,7 +84,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The only complicated part is the syntax for numbers: optional digits followed by an optional decimal point, some digits, and optionally a power of 10 marked by `\"E\"` and followed by an (optional) minus sign and some digits. \n",
"In this version of BASIC, variable names must be either a single letter, or a letter followed by a digit. The only complicated part is the syntax for numbers: optional digits followed by an optional decimal point, some digits, and optionally a power of 10 marked by `\"E\"` and followed by an (optional) minus sign and some digits. \n",
"Example usage of `tokenize`:"
]
},
@ -201,10 +201,6 @@
" top = peek()\n",
" if constraint is None or (top == constraint) or (callable(constraint) and constraint(top)):\n",
" return tokens.pop(0)\n",
" \n",
"def remove_spaces(line): \n",
" \"Remove white space from line, except space inside double quotes.\"\n",
" return \n",
"\n",
"def lines(text): \n",
" \"A list of the non-empty lines in a text.\"\n",
@ -396,7 +392,6 @@
" 'RETURN': [],\n",
" 'DIM': [list_of(variable)], \n",
" 'REM': [anycharacters],\n",
" 'A': []\n",
" }"
]
},
@ -699,7 +694,7 @@
"source": [
"# Parsing: Grammar of Expressions\n",
"\n",
"Now for the single most complicated part of the grammar: the `expression`. The biggest complication is operator precedence: the string `\"A + B * X + C\"` should be parsed as if it were `\"A + (B * X) + C\"`, and not as `\"(A + B) * (X + C),\"` because multiplication binds more tightly than addition. There are [many schemes](https://en.wikipedia.org/wiki/Operator-precedence_parser) for parsing expressions, I'll use [an approach](https://groups.google.com/forum/#!topic/comp.compilers/ruJLlQTVJ8o) attributed to Keith Clarke.\n",
"Now for the single most complicated part of the grammar: the `expression`. The biggest complication is operator precedence: the string `\"A + B * X + C\"` should be parsed as if it were `\"A + (B * X) + C\"`, and not as `\"(A + B) * (X + C),\"` because multiplication binds more tightly than addition. There are [many schemes](https://en.wikipedia.org/wiki/Operator-precedence_parser) for parsing expressions, I'll use [an approach](https://groups.google.com/forum/#!topic/comp.compilers/ruJLlQTVJ8o) attributed to Keith Clarke called Compact Recursive-Descent Parsing.\n",
"\n",
"Like all our grammatical categories, calling `expression()` pops off some tokens and returns a data object. The first thing it does is parse one of five types of simple \"primary\" expressions: \n",
"a number like `1.23`; \n",