Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22462

Ruslan Spivak: Let’s Build A Simple Interpreter. Part 2.

$
0
0

In their amazing book “The 5 Elements of Effective Thinking” the authors Burger and Starbird share a story about how they observed Tony Plog, an internationally acclaimed trumpet virtuoso, conduct a master class for accomplished trumpet players. The students first played complex music phrases, which they played perfectly well. But then they were asked to play very basic, simple notes. When they played the notes, the notes sounded childish compared to the previously played complex phrases. After they finished playing, the master teacher also played the same notes, but when he played them, they did not sound childish. The difference was stunning. Tony explained that mastering the performance of simple notes allows one to play complex pieces with greater control. The lesson was clear - to build true virtuosity one must focus on mastering simple, basic ideas.1

The lesson in the story clearly applies not only to music but also to software development. The story is a good reminder to all of us to not lose sight of the importance of deep work on simple, basic ideas even if it sometimes feels like a step back. While it is important to be proficient with a tool or framework you use, it is also extremely important to know the principles behind them. As Ralph Waldo Emerson said:

If you learn only methods, you’ll be tied to your methods. But if you learn principles, you can devise your own methods.”

On that note, let’s dive into interpreters and compilers again.

Today I will show you a new version of the calculator from Part 1 that will be able to:

  1. Handle whitespace characters anywhere in the input string
  2. Consume multi-digit integers from the input
  3. Subtract two integers (currently it can only add integers)

Here is the source code for your new version of the calculator that can do all of the above:

# Token types# EOF (end-of-file) token is used to indicate that# there is no more input left for lexical analysisINTEGER,PLUS,MINUS,EOF='INTEGER','PLUS','MINUS','EOF'classToken(object):def__init__(self,type,value):# token type: INTEGER, PLUS, MINUS, or EOFself.type=type# token value: non-negative integer value, '+', '-', or Noneself.value=valuedef__str__(self):"""String representation of the class instance.        Examples:            Token(INTEGER, 3)            Token(PLUS '+')"""return'Token({type}, {value})'.format(type=self.type,value=repr(self.value))def__repr__(self):returnself.__str__()classInterpreter(object):def__init__(self,text):# client string input, e.g. "3 + 5", "12 - 5", etcself.text=text# self.pos is an index into self.textself.pos=0# current token instanceself.current_token=Noneself.current_char=self.text[self.pos]deferror(self):raiseException('Error parsing input')defadvance(self):"""Advance the 'pos' pointer and set the 'current_char' variable."""self.pos+=1ifself.pos>len(self.text)-1:self.current_char=None# Indicates end of inputelse:self.current_char=self.text[self.pos]defskip_whitespace(self):whileself.current_charisnotNoneandself.current_char.isspace():self.advance()definteger(self):"""Return a (multidigit) integer consumed from the input."""result=''whileself.current_charisnotNoneandself.current_char.isdigit():result+=self.current_charself.advance()returnint(result)defget_next_token(self):"""Lexical analyzer (also known as scanner or tokenizer)        This method is responsible for breaking a sentence        apart into tokens."""whileself.current_charisnotNone:ifself.current_char.isspace():self.skip_whitespace()continueifself.current_char.isdigit():returnToken(INTEGER,self.integer())ifself.current_char=='+':self.advance()returnToken(PLUS,'+')ifself.current_char=='-':self.advance()returnToken(MINUS,'-')self.error()returnToken(EOF,None)defeat(self,token_type):# compare the current token type with the passed token# type and if they match then "eat" the current token# and assign the next token to the self.current_token,# otherwise raise an exception.ifself.current_token.type==token_type:self.current_token=self.get_next_token()else:self.error()defexpr(self):"""Parser / Interpreter        expr -> INTEGER PLUS INTEGER        expr -> INTEGER MINUS INTEGER"""# set current token to the first token taken from the inputself.current_token=self.get_next_token()# we expect the current token to be an integerleft=self.current_tokenself.eat(INTEGER)# we expect the current token to be either a '+' or '-'op=self.current_tokenifop.type==PLUS:self.eat(PLUS)else:self.eat(MINUS)# we expect the current token to be an integerright=self.current_tokenself.eat(INTEGER)# after the above call the self.current_token is set to# EOF token# at this point either the INTEGER PLUS INTEGER or# the INTEGER MINUS INTEGER sequence of tokens# has been successfully found and the method can just# return the result of adding or subtracting two integers,# thus effectively interpreting client inputifop.type==PLUS:result=left.value+right.valueelse:result=left.value-right.valuereturnresultdefmain():whileTrue:try:# To run under Python3 replace 'raw_input' call# with 'input'text=raw_input('calc> ')exceptEOFError:breakifnottext:continueinterpreter=Interpreter(text)result=interpreter.expr()print(result)if__name__=='__main__':main()

Save the above code into the calc2.py file or download it directly from GitHub. Try it out. See for yourself that it works as expected: it can handle whitespace characters anywhere in the input; it can accept multi-digit integers, and it can also subtract two integers as well as add two integers.

Here is a sample session that I ran on my laptop:

$ python calc2.py
calc> 27 + 3
30
calc> 27 - 7
20
calc>

The major code changes compared with the version from Part 1 are:

  1. The get_next_token method was refactored a bit. The logic to increment the pos pointer was factored into a separate method advance.
  2. Two more methods were added: skip_whitespace to ignore whitespace characters and integer to handle multi-digit integers in the input.
  3. The expr method was modified to recognize INTEGER -> MINUS -> INTEGER phrase in addition to INTEGER -> PLUS -> INTEGER phrase. The method now also interprets both addition and subtraction after having successfully recognized the corresponding phrase.

In Part 1 you learned two important concepts, namely that of a token and a lexical analyzer. Today I would like to talk a little bit about lexemes, parsing, and parsers.

You already know about tokens. But in order for me to round out the discussion of tokens I need to mention lexemes. What is a lexeme? A lexeme is a sequence of characters that form a token. In the following picture you can see some examples of tokens and sample lexemes and hopefully it will make the relationship between them clear:

Now, remember our friend, the expr method? I said before that that’s where the interpretation of an arithmetic expression actually happens. But before you can interpret an expression you first need to recognize what kind of phrase it is, whether it is addition or subtraction, for example. That’s what the expr method essentially does: it finds the structure in the stream of tokens it gets from the get_next_token method and then it interprets the phrase that is has recognized, generating the result of the arithmetic expression.

The process of finding the structure in the stream of tokens, or put differently, the process of recognizing a phrase in the stream of tokens is called parsing. The part of an interpreter or compiler that performs that job is called a parser.

So now you know that the expr method is the part of your interpreter where both parsing and interpreting happens - the expr method first tries to recognize (parse) the INTEGER -> PLUS -> INTEGER or the INTEGER -> MINUS -> INTEGER phrase in the stream of tokens and after it has successfully recognized (parsed) one of those phrases, the method interprets it and returns the result of either addition or subtraction of two integers to the caller.

And now it’s time for exercises again.

  1. Extend the calculator to handle multiplication of two integers
  2. Extend the calculator to handle division of two integers
  3. Modify the code to interpret expressions containing an arbitrary number of additions and subtractions, for example “9 - 5 + 3 + 11”

Check your understanding.

  1. What is a lexeme?
  2. What is the name of the process that finds the structure in the stream of tokens, or put differently, what is the name of the process that recognizes a certain phrase in that stream of tokens?
  3. What is the name of the part of the interpreter (compiler) that does parsing?


I hope you liked today’s material. In the next article of the series you will extend your calculator to handle more complex arithmetic expressions. Stay tuned.

And here is a list of books I recommend that will help you in your study of interpreters and compilers:

  1. Language Implementation Patterns: Create Your Own Domain-Specific and General Programming Languages (Pragmatic Programmers)

  2. Writing Compilers and Interpreters: A Software Engineering Approach

  3. Modern Compiler Implementation in Java

  4. Modern Compiler Design

  5. Compilers: Principles, Techniques, and Tools (2nd Edition)


BTW, I’m writing a book “Let’s Build A Web Server: First Steps” that explains how to write a basic web server from scratch. You can get a feel for the book here, here, and here. Subscribe to the mailing list to get the latest updates about the book and the release date.

OPTIN_FORM_PLACEHOLDER


Viewing all articles
Browse latest Browse all 22462

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>