Alex and Happy are the intellectual descendants of the Unix programs Lex and YACC (Yet Another Compiler Compiler). Given a high-level description of the rules governing the language to be parsed, alex produces a lexical analyser and happy produces a parser. 1.2 Alex and HappyĪlex and Happy are programs that generate other programs. At the same time by getting rid of extraneous data like whitespace and comments, the presence of a lexer simplifies the design and implementation of the parser since the parser can now deal with a more abstract representation of the input. Boundaries between tokens can usually be found by looking at a few characters at a time. The advantage of having a separate lexer is that lexers are easy to write since the breaking a string of bytes into tokens usually does not require a understanding of the entire input at a time. The parser then looks at this sequence of tokens and recognizes this as a statement declaring the module “Main”. It may also mark module and where as keywords of the language. The lexical analyser will produce the tokens module, Main and where while skipping the whitespaces and the comment. Thus, given the following line of Haskell source code The parser proper then takes these tokens and assembles them into larger structures according to rules given by a grammar. This phase breaks up the sequence of bytes provided as input into a sequence of indivisible tokens while at the same time carrying out other tasks such as keeping track of line numbers of the source file and skipping whitespace and comments. The first phase is lexical analysis, is carried out by a part of the program called the lexical analyser or lexer for short. Parsing itself is often divided into two phases. It is the task of the program to interpret the bytes and discover the intended structure. In all these cases the text first appears to the program as a simple sequence of bytes. In the case of a web server it may be a configuration file describing the different sites to be served. In the case of a analytics tool it might be a log file with entries in a particular format. In the case of a compiler this text is a program written in a certain programming language. Many programs take structured text as input. The artwork for the cover page is released under a newer version of the same license and is included in the Github repository for this book. Used under the terms of the CC BY-SA 2.0 license. Please send your comments and bug reports to Cover image The full source code for the examples can be downloaded from Github
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |