This video demonstrates how to convert a Scala implementation of context-free grammars into Python, covering key differences such as Python's dynamic typing versus Scala's static type checking, the use of inheritance and super() for class hierarchies, string formatting methods, file I/O handling, and debugging common Python-specific issues like attribute errors and type errors when implementing grammar rule parsing.
Convert CFG/CNF Grammar Code from Scala to Python
Added:okay let's continue by taking this set of classes that we've created and converting it to Python so currently we've got this file it's a scalar file that we've created in Eclipse that has a number of classes it's got a trait called symbol got an object called Epsilon that extends symbol got a class non-terminal that extends symbol got a class terminal and non-terminal each of which extends symbol we've got a class that's a rule that encapsulate the left hand side in the right hand side and then we've got a grammar object that it caps lights a couple of functions read rules and refuel and then finally we've got the main that just does the boilerplate that we needed to okay so let's start simple let's go let's start with symbol okay and so we're gonna open a new file to Python 3 file ok and we're gonna have a class called simple okay and we're instead of using a trait we're just going to make it a regular class okay right okay and then we will want the to string so Python uses a little bit different syntax remember we can look it up looks like underscore underscore STR okay so we'll start there let's add the is epsilon method is Epsilon okay and here we'll rely on a pythonic way of doing things so the non value so let's say that if symbol is initialized and value is none then we will treat that as Epsilon so if self dot value equals none then the turn three else return false okay and then we'll change this for the string if self dot is Epsilon then we'll return the string Epsilon otherwise we'll return value okay so there we've got our symbol class okay and next let's do non terminal that inherits from symbol okay okay so now we need to figure out how to do inheritance in Python I thought inheritance this is the situation for a more complicated inheritance model we've got person employee inherits from person and we're going to explicitly call the superclass superclasses constructors so net self value and we're going to call symbol net so okay now at this point we might ask ourselves whether we even need this separate class hierarchy so in Scala there was a good reason to use symbol epsilon terminal and non-terminal all as separate categories and it's because of the type system is doing static type checking for us which means that there's going to be some really nice things that we can do later on involving pattern matching because of the fact that we have this static type system but in Python we don't have static type checking so we're gonna go ahead and say you know what this we could do this but there's gonna be a lot of overlap for example when we look here at this non-terminal class we're inheriting from symbol but what really does it get us and the answer is it really doesn't get us anything so we'll go ahead and say for this Python implementation we're not even going to bother what we could do if we wanted to is have an extra definition here that is is non terminal so and if cell value starts with so if the zeroeth element see we'll start by if is epsilon itself that is epsilon then return false if we're epsilon then we're not a non-terminal otherwise we will look to see if self dot value at zero is uppercase so python or uppercase is upper is upper house boss okay and then we could have if we want death is terminal so that is yeah self dot is epsilon both otherwise try not is okay alright so there's our symbol class what else do we need to do we want a rural class okay so that's going to be pretty simple class rule in it self left hand side right hand side we don't specify the types okay and what do we need to do for the two string STR turn okay and how do we do string interpolation in Python 3 same population and we can do with format okay looks like with Python 3.6 which let's check to see if we're using Python 3.6 let's check on the server 3.4 so we can't do it there so we won't use string interpolation so we want to make sure this works on the server that we're using okay so we will take the left hand side and convert it to a string plus and then we want the arrow literal plus right hand side is going to be a list of non terminals inside how do we do string do the equivalent of make string here like on strings join [Music] so the ass here is going to be what we want to join it with so that is going to be a space dot join and right-hand side okay so we'll try that so let's test it out so left hand side equals non-terminal and P right hand side equals can list this was how we did a list and skaila with just the list notation let's check eight this literal Python you can also go to the terminal on check equals a right hand side okay so hand side with the determiner now inside non-terminal is not defined and I need to use symbol instead of non-terminal expect its string sequence instance found so here join is expecting a list of strings and right now we've got a list of symbol objects so we can fix that by doing a Python list comprehension [Music] let's try that still going like that yeah let's test the string syntax and see if that's the problem okay so we're hearing in source and let's start a Python interpreter and import our class my grammar and it's not liking something so let's create a new symbol s equals symbol competing didn't like that ok let's try from my grammar import simple and it's still not liking something that's it not like let's comment some stuff out and see if we can figure out what it doesn't like okay that's good okay so it's not liking my [Music] string method up here so let's figure out why not Table of Contents for Python documentation look for classes classes underscore underscore een it is a special thing that's the sort of thing we want and let's change this so that instead of value it returns the literal string value uh-huh okay okay it seems to work yeah I think that's gonna be fine but just to be safe we'll do that okay and I would say left-hand side strings right hand side strings equals this and ice fantasies in there there we go all right some top power you because value okay all right okay so now it's working the only question is why is it giving us epsilon for everything because we don't want epsilon for everything we want the symbol to use the value that we provide when it prints it up when it prints itself out so let's let's check to see what's going on so you knit should be saving the value that we provide in self dot value if self dot is epsilon return epsilon otherwise return self dot value there we go let's try that there we go much better all right so now we've got a rule and now we're going to read in all of the rules from the file okay so let's create a method that reads a line and provides a rule so this is going to be very similar to the grammar dot read rule in the scalar version okay so we're gonna take parts equals line split and let's test the syntax here I equals NP e to e & split okay okay we'll just stick with the default split so nine don't split it's gonna be parts right-hand side left hand side it's going to equal parts sub-zero and let's see if yeah hearts the length hearts is greater than one will do one thing I'll do something else so if it is not greater than one that means we have an epsilon rule and we're going to return a rule with the left-hand side and none for the right-hand side which will give us an epsilon rule otherwise we're going to take those parts so we will say the right-hand side is going to be equal to a new I'm sorry a symbol with the right-hand side string for each right-hand side string in parts from one to the end okay and then turn rule with the left-hand side and the right-hand side okay let's try that over here so let's say rule equals read line and p PT and and it works great okay so now let's do we tap three lines okay and let's see read lines file and Python okay so there's a number of ways of doing it try to find the best way with open as file that looks pretty good with open as file for line and print lines okay and rather than having lines here we're going to have filename so we're going to open file name that's F and that will take care of closing the file for us or line and F equals this and we want to append to this in Python just call a pen duo stop app and rule if you don't need to do that we'll just call read line so another way of doing this same rule equals read line line that's going to give us a rule and we're going to pad the rule and then return rules okay all right so now we can get the argument so sISTAR V the length of sista RB is less than one all right the first one is going to be the script so we want it to be less than it's less than two and we'll print usage else is v1 [Music] we got to import sis nun type is not iterable right so in the string which we checked for is epsilon right hand side string for right hand side and self dot right hand side none type object is not iterable right here this line needs to be inside a list there we go so right now we have the equivalent code in Python as we have in Scala and the Python code is considerably shorter but the scalar code has some additional type information that will come in handy later so that's all for now
Up Next

Gradient Descent Step-by-Step: Machine Learning Optimization Explained
@statquest
1.7M views•2019-02-05

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science

















![Parsing Algorithms. Lecture [1/22] Formal grammars, context-free](https://i.ytimg.com/vi/VZ5DJopq5JA/maxresdefault.jpg)




















