
    
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/parsers","feed_url":"https:\/\/mathspp.com\/blog\/tags\/parsers.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"Parsing TXRX files in Python","date_published":"2024-11-14T10:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/parsing-txrx-files-in-python","url":"https:\/\/mathspp.com\/blog\/parsing-txrx-files-in-python","content_html":"<p>In this article I show how I wrote a parser to parse TXRX files into a more manageable object in Python.<\/p>\n\n<p>In the <a href=\"https:\/\/skool.com\/python\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Python community<\/a> that I am creating, a friend of mine mentioned he was trying to write a parser for TXRX files.\nI like parsers, so I offered to help with that task and this article is a short overview of what I did and how I did it.<\/p>\n<h2 id=\"txrx-files\">TXRX files<a href=\"#txrx-files\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>First and foremost, I have no idea what TXRX files are.\nMy friend sent me one sample TXRX file for me to play around with, and I will include it here for you to take a look at the data as well:<\/p>\n<details><summary>Sample TXRX file contents<\/summary><pre><code class=\"language-txrx\">begin_&lt;points&gt; BS\nproject_id 1\nactive\nvertical_line no\npattern_shown no\ncube_size 2.50000\nCVxLength 10.00000\nCVyLength 10.00000\nCVzLength 10.00000\nAutoPatternScale\nShowDescription yes\nCVsVisible no\nCVsThickness 3\nbegin_&lt;location&gt; \nbegin_&lt;reference&gt; \ncartesian\nlongitude -0.000000000000000\nlatitude 0.000000000000000\nvisible no\nterrain\nend_&lt;reference&gt;\nnVertices 1\n-33.000000000000000 11.000000000000000 32.000000000000000\nend_&lt;location&gt;\npattern_show_arrow no\npattern_show_as_sphere no\ngenerate_p2p yes\nuse_apg_acceleration no\nis_transmitter yes\nis_receiver yes\nbegin_&lt;transmitter&gt; \nbegin_&lt;pattern&gt; \nantenna 1\nwaveform 1\nrotation_x 0.00000\nrotation_y 0.00000\nrotation_z 0.00000\nend_&lt;pattern&gt;\npower 0.00000\nend_&lt;transmitter&gt;\nbegin_&lt;receiver&gt; \nbegin_&lt;pattern&gt; \nantenna 1\nwaveform 1\nrotation_x 0.00000\nrotation_y 0.00000\nrotation_z 0.00000\nend_&lt;pattern&gt;\nNoiseFigure 3.00000\nend_&lt;receiver&gt;\npowerDistribution Uniform 10.00000 10.00000 inactive nosampling 10\nend_&lt;points&gt;\nbegin_&lt;grid&gt; ue_grid\nproject_id 2\nactive\nvertical_line no\npattern_shown no\nCVxLength 10.00000\nCVyLength 10.00000\nCVzLength 10.00000\nAutoPatternScale\nShowDescription yes\nCVsVisible no\nCVsThickness 3\nbegin_&lt;location&gt; \nbegin_&lt;reference&gt; \ncartesian\nlongitude -0.000000000000000\nlatitude -0.000000000000000\nvisible no\nterrain\nend_&lt;reference&gt;\nside1 180.00000\nside2 120.00000\nspacing 2.00000\nnVertices 1\n-90.000000000000000 -60.000000000000000 1.500000000000000\nend_&lt;location&gt;\npattern_show_arrow no\npattern_show_as_sphere no\ngenerate_p2p no\nuse_apg_acceleration yes\nis_transmitter no\nis_receiver yes\nbegin_&lt;receiver&gt; \nbegin_&lt;pattern&gt; \nantenna 1\nwaveform 1\nrotation_x 0.00000\nrotation_y 0.00000\nrotation_z 0.00000\nend_&lt;pattern&gt;\nNoiseFigure 3.00000\nend_&lt;receiver&gt;\npowerDistribution Uniform 10.00000 10.00000 inactive nosampling 10\nend_&lt;grid&gt;\n<\/code>\n<\/pre>\n<\/details><p>The most important thing that I noticed is that there seem to be opening and closings tags that look like <code>begin_&lt;...&gt;<\/code> \/ <code>end_&lt;...&gt;<\/code> and in between those you get lines of values, and possibly more nested tags.<\/p>\n<p>Here is an excerpt of the TXRX file that shows this structure.\nTo make it easier to visualise, I will indent the lines within opening \/ closing tags:<\/p>\n<pre><code class=\"language-txrx\">begin_&lt;points&gt; BS\n    project_id 1\n    AutoPatternScale\n    CVsThickness 3\n    begin_&lt;location&gt;\n        begin_&lt;reference&gt;\n            longitude -0.000000000000000\n            latitude 0.000000000000000\n            terrain\n        end_&lt;reference&gt;\n        -33.000000000000000 11.000000000000000 32.000000000000000\n    end_&lt;location&gt;\n    generate_p2p yes\n    use_apg_acceleration no\n    begin_&lt;transmitter&gt; \n        begin_&lt;pattern&gt; \n            rotation_x 0.00000\n        end_&lt;pattern&gt;\n        power 0.00000\n    end_&lt;transmitter&gt;\n    begin_&lt;receiver&gt; \n        begin_&lt;pattern&gt; \n            waveform 1\n            rotation_z 0.00000\n        end_&lt;pattern&gt;\n        NoiseFigure 3.00000\n    end_&lt;receiver&gt;\n    powerDistribution Uniform 10.00000 10.00000 inactive nosampling 10\nend_&lt;points&gt;<\/code><\/pre>\n<h2 id=\"txrx-format-grammar\">TXRX format grammar<a href=\"#txrx-format-grammar\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>With this in mind, the first thing I did was write a pseudo <a href=\"https:\/\/en.wikipedia.org\/wiki\/Backus%E2%80%93Naur_form\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">BNF grammar<\/a> that represents the format of the data.\nThat's because, with the grammar in place, writing the parser is much easier.<\/p>\n<p>This is what I came up with:<\/p>\n<pre><code class=\"language-bnf\">document := node* EOF\nnode := BEGIN_TAG TAG_NAME? values END_TAG NL\nvalues := (node | line_value)*\nline_value := (STR | \"yes\" | \"no\" | INT | FLOAT)+ NL<\/code><\/pre>\n<h2 id=\"tokenizing-the-file\">Tokenizing the file<a href=\"#tokenizing-the-file\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>To make parsing easier, I also decided to write a tiny tokenizer.\nThe function <code>tokenize_file<\/code> accepts the...<\/p>","summary":"In this article I show how I wrote a parser to parse TXRX files into a more manageable object in Python.","date_modified":"2025-11-22T09:33:35+01:00","tags":["parsers","programming","python"],"image":"\/user\/pages\/02.blog\/parsing-txrx-files-in-python\/thumbnail.webp"},{"title":"TIL #088 \u2013 Python&#039;s soft keywords","date_published":"2023-11-12T09:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/til\/pythons-soft-keywords","url":"https:\/\/mathspp.com\/blog\/til\/pythons-soft-keywords","content_html":"<p>Today I learned how many soft keywords Python has and what they are.<\/p>\n\n<h2 id=\"python-s-soft-keywords\">Python's soft keywords<a href=\"#python-s-soft-keywords\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Python 3.12 has 4 soft keywords:<\/p>\n<ol><li><code>match<\/code> &ndash; became a soft keyword in Python 3.10 when <a href=\"\/blog\/pydonts\/structural-pattern-matching-tutorial\">structural pattern matching<\/a> was introduced;<\/li>\n<li><code>case<\/code> &ndash; see above;<\/li>\n<li><code>_<\/code> &ndash; surprisingly, <code>_<\/code> was also turned into a soft keyword because of structural pattern matching (<a href=\"\/blog\/til\/underscore-is-a-soft-keyword\">this short read<\/a> will give you a bit more context); and<\/li>\n<li><code>type<\/code> &ndash; with the introduction of <a href=\"\/blog\/til\/type-statement-and-type-aliases\"><code>type<\/code> statements in Python 3.12<\/a>, <code>type<\/code> became a soft keyword (it is also a built-in!)<\/li>\n<\/ol><p>In older versions of Python (I <em>think<\/em> Python 3.5 and 3.6), <code>async<\/code> and <code>await<\/code> were soft keywords too, but that's no longer the case.<\/p>\n<h3 id=\"what-are-soft-keywords\">What are soft keywords?<a href=\"#what-are-soft-keywords\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<p>Loosely speaking, soft keywords are keywords that can be assigned to, in opposition to hard keywords that can never be assigned to.<\/p>\n<p><code>if<\/code> is an example of a hard keyword.\nNo matter what you do, <code>if<\/code> will always be used to represent the beginning of a conditional statement and you definitely cannot assign to <code>if<\/code> under no circumstance, as writing something like <code>if = 3<\/code> will raise a <code>SyntaxError<\/code>.<\/p>\n<p>On the other hand, a keyword like <code>match<\/code> is a soft keyword because you can use it for structural pattern matching, but it's also a legal variable name elsewhere: <code>match = re.match(...)<\/code> works perfectly well.<\/p>\n<h3 id=\"how-to-determine-all-of-python-s-soft-keywords\">How to determine all of Python's soft keywords?<a href=\"#how-to-determine-all-of-python-s-soft-keywords\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h3>\n<div class=\"notices green\">\n<p>EDIT: After sharing my original method and posting this article on X, Pablo Galindo Salgado &ndash; a Python core dev &ndash; <a href=\"https:\/\/x.com\/pyblogsal\/status\/1723690831318577591\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">pointed out I could've simply done the following<\/a>:<\/p>\n<pre><code class=\"language-pycon\">&gt;&gt;&gt; import keyword\n&gt;&gt;&gt; keyword.softkwlist\n['_', 'case', 'match', 'type']<\/code><\/pre>\n<\/div>\n<p>In order to determine Python's soft keywords, I started by opening the Python grammar (at the time of writing, <a href=\"https:\/\/github.com\/python\/cpython\/blob\/12a30bc1aa0586308bf3fe12c915bcc5e54a032f\/Grammar\/python.gram\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this file<\/a> was the most recent grammar version).<\/p>\n<ul><li>I opened the Python REPL;<\/li>\n<li>I loaded the file; and<\/li>\n<li>I used a regular expression to look for all keywords:<\/li>\n<\/ul><pre><code class=\"language-pycon\">&gt;&gt;&gt; import re\n&gt;&gt;&gt; pat = re.compile(r'\\'.{,15}\\'|\".{,15}?\"')\n&gt;&gt;&gt; matches = set(pat.findall(gram))\n&gt;&gt;&gt; matches\n{\n    \"':' &amp;('}'|','\",\n    \"'=', or '!', or '\",\n    \"' | '**'\",\n    \"'+' | '-'\",\n    \"'\/\/'\",\n    '\"invalid_\"',\n    \"'except' ':'\",\n    # ...\n}<\/code><\/pre>\n<p>As you can see, that gives plenty of false positives, but that's ok.\nWe have 222 possible keywords to go through:<\/p>\n<pre><code class=\"language-pycon\">&gt;&gt;&gt; len(matches)\n222<\/code><\/pre>\n<p>However, if we get rid of all of the ones that contain whitespace, only 105 matches are left:<\/p>\n<pre><code class=\"language-pycon\">&gt;&gt;&gt; matches = {match for match in matches if \" \" not in match}\n&gt;&gt;&gt; len(matches)\n105<\/code><\/pre>\n<p>After this, I painstakingly went through all 105 matches and checked manually whether they were soft keywords or not.\nI took none for granted.\nI even went through the trouble of checking <code>if<\/code> and <code>for<\/code>, for example, so that I could be 100% certain I wouldn't miss a single soft keyword.<\/p>\n<p>Then, I was doing some research on structural pattern matching and I was reading <a href=\"https:\/\/peps.python.org\/pep-0634\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">PEP 634<\/a> that introduces it, when I read this sentence referring to the Python grammar:<\/p>\n<blockquote>\n<p>&ldquo;By convention, hard keywords use single quotes while...<\/p><\/blockquote>","summary":"Today I learned how many soft keywords Python has and what they are.","date_modified":"2025-07-23T16:49:02+02:00","tags":["parsers","programming","python"],"image":"\/user\/pages\/02.blog\/04.til\/088.pythons-soft-keywords\/thumbnail.webp"},{"title":"TIL #087 \u2013 underscore is a soft keyword","date_published":"2023-11-12T08:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/til\/underscore-is-a-soft-keyword","url":"https:\/\/mathspp.com\/blog\/til\/underscore-is-a-soft-keyword","content_html":"<p>Today I learned that the underscore <code>_<\/code> is a soft keyword in Python.<\/p>\n\n<h2 id=\"underscore-is-a-soft-keyword\">Underscore is a soft keyword<a href=\"#underscore-is-a-soft-keyword\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>I've written about <a href=\"\/blog\/pydonts\/usages-of-underscore\">the multiple usages of the underscore <code>_<\/code> in Python<\/a> and in that article I write about the fact that the underscore is idiomatically used to assign a value we don't care about.\nSomething like this:<\/p>\n<pre><code class=\"language-py\">## Suppose we have a colour like `colour = (\"red\", (255, 0, 0))`\ncolour_name, _ = colour<\/code><\/pre>\n<p>The code above uses <a href=\"\/blog\/pydonts\/deep-unpacking\">unpacking<\/a> to extract the colour name from the variable <code>colour<\/code> and we use the second variable name <code>_<\/code> to idiomatically say \u201cwe expect there to be a second value to unpack, but we don't care about its value\u201d.\nBut this is just a convention.\nThere is nothing special about the underscore <code>_<\/code> in this situation; it's just a valid variable name:<\/p>\n<pre><code class=\"language-py\">_ = 3\n_ *= 2\nprint(_)  # 6<\/code><\/pre>\n<p>However, in <a href=\"\/blog\/pydonts\/structural-pattern-matching-tutorial\">Python's 3.10 <code>match<\/code> statement<\/a>, the underscore <code>_<\/code> was turned into a soft keyword.\nThis means that when you write a <code>match<\/code> statement containing a <code>case<\/code> statement that looks like <code>case _:<\/code>, Python actually parses the underscore <code>_<\/code> as a keyword!<\/p>\n<p>So, if <code>_<\/code> can be used as a regular variable name and if <code>_<\/code> can be parsed as a keyword, depending on the context, that makes it a soft keyword!<\/p>\n<div class=\"notices blue\">\n<p>To confirm that <code>_<\/code> can be parsed as a keyword inside a <code>case<\/code> statement, don't take my word for it.\nYou can <a href=\"https:\/\/github.com\/python\/cpython\/blob\/12a30bc1aa0586308bf3fe12c915bcc5e54a032f\/Grammar\/python.gram#L533\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">open Python's grammar and see for yourself<\/a>!<\/p>\n<\/div>","summary":"Today I learned that the underscore `_` is a soft keyword in Python.","date_modified":"2025-07-23T16:49:02+02:00","tags":["parsers","programming","python"],"image":"\/user\/pages\/02.blog\/04.til\/087.underscore-is-a-soft-keyword\/thumbnail.webp"},{"title":"Finding all strings a regular expression can find","date_published":"2017-11-20T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/regex-printer","url":"https:\/\/mathspp.com\/blog\/regex-printer","content_html":"<p>A <a href=\"https:\/\/en.wikipedia.org\/wiki\/Regular_expression\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">regular expression<\/a>, without much rigor, is a very compact way of representing several different strings. Given a regular expression (regex), can I find out all the strings the regex can find?<\/p>\n\n<figure class=\"image-caption\"><img title=\"Photo by Aaron Burden on Unsplash\" alt=\"page of a book with a paragraph highlighted\" src=\"\/images\/8\/8\/6\/5\/1\/8865141b562f6ac1143c768912109de36cf835d5-hightlighted-book.jpg\"><figcaption class=\"\">Photo by Aaron Burden on Unsplash<\/figcaption><\/figure><p>One common use of regular expressions is to look for strings that have a certain structure in a bigger string (say a text). As an example, the regular expression <code>abc(d|e)<\/code> can be used to look for the strings \"abcd\" and \"abce\", where the character <code>|<\/code> denotes we have to make a choice (see image above). Thus <code>cat|dog<\/code> would match the strings \"cat\" and \"dog\". There are other special symbols that have meanings and purposes.<\/p>\n<p><img alt=\"two screenshots of my program\" src=\"\/user\/pages\/02.blog\/regex-printer\/regex.webp\"><\/p>\n<p>One very interesting question that arises is: given a regular expression, what are the strings matched by it? To answer that question I wrote a small Python program, that I called <code>regexPrinter<\/code>, that prints all strings matched by a given regular expression! In order to manage that task, I chose a subset of the regex syntax that I wanted to be able to print and also decided that whenever a piece of a pattern was infinite, at a given point the program would just print \"...\" to denote that infinity. This way, for any regex given, the program always stops.<\/p>\n<p>The <code>regexPrinter<\/code> supports:<\/p>\n<ul><li>the <code>*<\/code> operator, that denotes that <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> or more repetitions are to be matched. For example, <code>ah*<\/code> matches \"a\", \"ah\", \"ahh\", ...;<\/li>\n<li>the <code>+<\/code> operator that denotes that <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span> or more repetitions are to be matched. For example, <code>(hue)+<\/code> matches \"hue\", \"huehue\", \"huehuehue\", ...;<\/li>\n<li>the <code>?<\/code> operator that denotes either <span class=\"mathjax mathjax--inline\">\\(0\\)<\/span> or <span class=\"mathjax mathjax--inline\">\\(1\\)<\/span> occurrences of the preceding pattern. For example, <code>woo(hoo)?<\/code> matches \"woo\" and \"woohoo\";<\/li>\n<li>the <code>{a:b}<\/code> operator that matches no less than <span class=\"mathjax mathjax--inline\">\\(a\\)<\/span> and no more than <span class=\"mathjax mathjax--inline\">\\(b\\)<\/span> repetitions of the preceding pattern. As an example, <code>su{1:3}re<\/code> matches the strings \"sure\", \"suure\" and \"suuure\";<\/li>\n<li>the <code>|<\/code> operator that denotes a choice. <code>cat|kat<\/code> matches \"cat\" and \"kat\" and <code>thank(s| you)<\/code> matches \"thanks\" and \"thank you\";<\/li>\n<li>the <code>[]<\/code> denote that only one pattern from the ones given are to be matched. For example <code>[abc]<\/code> matches \"a\", \"b\" and \"c\";<\/li>\n<li>the parentheses <code>()<\/code> that are used to group things. One thing to note is that the quantifiers <code>*+?{:}<\/code> all have higher precedence than string concatenation e.g., <code>ab?<\/code> is interpreted as <code>a(b?)<\/code> and <em>not<\/em> <code>(ab)?<\/code>.<\/li>\n<\/ul><p>Please bear in mind that any character with no special meaning is interpreted literally, except inside the grouping operator <code>[]<\/code>, where every character is interpreted literally. That is, <code>[ab*]<\/code> matches \"a\", \"b\" and \"*\", while <code>b&amp;='<\/code> will match the string \"b&amp;='\".<\/p>\n<p><img alt=\"another screenshot of my program\" src=\"\/user\/pages\/02.blog\/regex-printer\/regex2.webp\"><\/p>\n<p>The code for the program can be found <a href=\"https:\/\/github.com\/RodrigoGiraoSerrao\/projects\/blob\/master\/misc\/regexPrinter.py\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">here<\/a> on GitHub and all it takes is vanilla Python 3 to run. Just run the script and you will be prompted to insert regular expressions. The techniques I used were very, very similar to the ones I used to create <a href=\"https:\/\/mathspp.com\/blog\/creating-programming-language-from-scratch\">my toy programming language<\/a>, as I saw in <a href=\"https:\/\/ruslanspivak.com\/lsbasi-part1\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this<\/a> blog series. The way I went about writing this...<\/p>","summary":"In this post I explain briefly what a regex is and show something interesting I did with them.","date_modified":"2024-08-13T12:47:46+02:00","tags":["python","grammars","computation theory","parsers","regex"],"image":"\/user\/pages\/02.blog\/regex-printer\/hightlighted-book.jpg"}]}
