onfirのブログ -31ページ目

onfirのブログ

ブログの説明を入力します。

Google Daniel Steve Yegge once said:. 'If you do not know how the Michael Kors Designer compiler works, then you also do not know how to work the computer if you are not 100% sure you know how the compiler works, that You do not really know how they work. 'It has nothing to do with you is a novice or an experienced software developers: If you do not know the compiler and interpreter how it works, then you do not know how a computer works ʱ?? It's that simple. Well, you know the compiler and interpreter how the job? I mean, yes, you are 100% sure you know how they work it? If you do not know. Or if you do not know, and therefore uneasy. Do not worry. If you stay to learn the complete series, and with me to construct an interpreter and compiler, you will eventually know how they work. And you will become a confident happy person, at least I hope so. Why learn interpreter and compiler? There are three reasons: 1, in order to write an interpreter or a compiler, integrated application of some of the skills you need. Write an interpreter or compiler will help you improve these skills, so that you become a better software developer. At the same time these technologies in the preparation of other software (non-compiler and interpreter) Similarly useful. 2. Do you really want to know how the computer works. Often interpreter and compiler looks like magic. And you do not feel very comfortable on this magic. Do you want to figure out a configuration interpreter and compiler process, understand how they work, understand that everything inside. 3. You want to create your own language or a language specific areas. If you created it, then you also need to create a compiler or interpreter for it. Recently, the renewed rise of the interest in new languages. You can almost see the emergence of new language every day: Elixir, Go, Rust, just having a few examples. Well, what is the interpreter, what is the compiler do? Interpreter and compiler's goal is to use high-level language source code into another form. What form? Do not just sit dry in subsequent parts of this series, you'll be sure to learn the source code will be converted into anything. Now you might be curious about what is the difference between the interpreter and compiler. For this series, we agreed that if a translator will translate into machine language source code, then it is a compiler. If you deal directly with a translator and run the source code, the source code is not first translated into machine language, then it is an interpreter. Intuitively it would look like this: I hope that at this moment, you are sure you are willing to learn, and build an interpreter and compiler. On this interpretation series, you expect? You see that okay. We created a simple interpreter of a large subset Cheap Michael Kors of the Pascal language. Explain in this series Finally, you will be able to get a job, and like Python's pdb same source-level debugger. So the question is, why choose Michael Kors Hamilton Pascal? First of all, I did not order this series fabricated language: This is a true programming language with many important language structures. Secondly, some computer books, although old, but practical examples of these books with the Pascal language. (I admit this is not a reason to choose it to construct interpreter irresistible, but I think this is a good opportunity to learn the language of the non-mainstream :) This is an example of using the Pascal write factorial. You will be able to use their own interpreter and debugger to interpret and debug the code. program factorial; function factorial (n: integer): longint; beginif n = 0 thenfactorial: = 1elsefactorial: = n * factorial (n - 1); end; varn: integer; beginfor n: = 0 to 16 dowriteln (n, ' ! = ', factorial (n));. end we use to achieve Pascal Python interpreter, but you can also use any language to implement it, the expression of ideas should not be limited to any particular language. Well, let us begin to act. Ready, start! You will write an interpreter of the four arithmetic expression (commonly known as a calculator), to complete the interpreter and compiler of the first march. Today's goal is simple: Let the calculator can handle the addition of a bit integers, such as 3 + 5. Here is your code calculator, I'm sorry, Michael Kors Handbags your interpreter code. # Token types ## EOF (end-of-file) token is used to indicate that # there is no more input left for lexical analysisINTEGER, PLUS, EOF = 'INTEGER', 'PLUS', 'EOF'class Token (object) : def __init __ (self, type, value): # token type: Christmas Beauty iPhone 4 4S Cases INTEGER, PLUS, or EOFself.type = type # token value: 0, 1, 2. 3, 4, 5, 6, 7, 8, 9, ' + ', or Noneself.value = valuedef __str __ (self): \u0026 quot; \u0026 quot; \u0026 quot; String representation of the class instance.Examples: Token (INTEGER, 3) Token (PLUS' + ') \u0026 quot; \u0026 quot; \u0026 quot; return' Token ({type}, Michael Kors Accessories {value}) 'format (type = self.type, value = repr (self.value)) def __repr __ (self):. return self .__ str __ () class Interpreter (object): def __init __ ( self, text): # client string input, eg \u0026 quot; 3 + 5 \u0026 quot; self.text = text # self.pos is an index into self.textself.pos = 0 # current token instanceself.current_token = Nonedef error (self): raise Exception ('Error parsing input') def get_next_token (self): \u0026 quot; \u0026 quot; \u0026 quot; Lexical analyzer (also known as scanner or tokenizer) This method is responsible for Michael Kors New Christmas Angles iPhone 5 5S Cases Arrivals breaking a sentenceapart into tokens One token at a time \u0026 quot;.. \u0026 quot; \u0026 quot; text = self.text # is self.pos index past the end of the self.text # if so, then return EOF token because there is no more # input left to convert into tokensif self.pos \u0026 gt;? len (text) - 1: return Token (EOF, None) # get a character at the position self.pos and decide # what token to create based on the single charactercurrent_char = text [self.pos] # if the character is a digit then convert it to # integer, create an INTEGER token, increment self.pos # index to point to the next character after the digit, # and return the INTEGER tokenif current_char.isdigit (): token = Token (INTEGER, int (current_char)) self.pos + = 1return tokenif current_char == '+': token = Token (PLUS, current_char) self.pos + = 1return tokenself.error Christmas Beauty iPhone 5 5S Cases () def eat (self, token_type): # compare the current token type with the passed token # type and if they match then \u0026 quot; eat \u0026 quot; the current token # and assign the next token to the self.current_token, # otherwise raise an exception.if self.current_token.type == token_type: self.current_token = self.get_next_token () else: self.error () def expr (self): \u0026 quot; \u0026 quot; \u0026 quot; expr - \u0026 gt; INTEGER PLUS INTEGER \u0026 quot; \u0026 quot; \u0026 quot; # set current token to the first token taken from the inputself.current_token = self. get_next_token () # we expect the current token to be a single-digit integerleft = self.current_tokenself.eat (INTEGER) # we expect the current token to be a '+' tokenop = self.current_tokenself.eat (PLUS) # we expect the current token to be a single-digit integerright = self.current_tokenself.eat (INTEGER) # after the above call the self.current_token is set to # EOF token # at this point INTEGER PLUS INTEGER sequence of tokens # has been successfully found and the method can just # return the result of adding two integers, thus # effectively interpreting client inputresult = left.value + right.valuereturn resultdef main (): while True: try: # To run under Python3 Michael Kors Clutches replace 'raw_input' call # with ' input'text = raw_input ('calc \u0026 gt;') except EOFError: breakif not text: continueinterpreter = Interpreter (text) result = interpreter.expr () print (result) if __name__ == '__main __': main () the above code Save as file calc1.py, or downloaded directly from the Github. Before diving into the code to the command line to run, to observe the results. A good play this program. This is in my notebook on a sample session (if you use Python3, code raw_input need to rewrite input) $ python calc1.pycalc \u0026 gt; 3 + 47calc \u0026 gt; 3 + 58calc \u0026 gt; 3 + 912calc \u0026 gt; To make the calculator work, do not throw an exception, enter should follow a few rules: Enter only single-digit integer arithmetic currently supported only addition is not allowed in the input box in order to make a simple calculator These restrictions are necessary. Do not worry, soon Christmas Angles iPhone 4 4S Cases you will make it very complicated. Okay, now let us understand the interpreter how it works, and how is it calculated arithmetic expressions. When you enter the command line Michael Kors Hot Sale 3 + 5, the interpreter to give a string of characters '3 + 5.' To make the interpreter Michael Kors Christmas Cases really understand how to deal with this Michael Kors Satchels string, the first step you need to '3 + 5' cut into different parts, which we call sign (tokens). A token (token) is a pair of type * values. For example, the symbol '3' is of type INTEGER, the corresponding value is an integer 3. Cut the string into the token input process is called lexical analysis. So, the first step in the interpreter needs to read input and convert it into a series Michael Kors Bedford & Astor of tokens. The interpreter to do this part of the work of assembly is called the lexical analyzer (lexical ananlyzer, referred lexer). You may come across other terminologies, like scanner (scanner), segmenter (tokenizer). They mean the same: the interpreter or compiler converts the input string into a string of tokens components. Interpreter class