Docsity

Lexical Analysis in Programming Languages: A Comprehensive Guide, Summaries of Compiler Design

Toggle sidebar
bg1
CS143 Handout 04
Summer 2012 June 27, 2012
Lexical Analysis
Handout written by Maggie Johnson and Julie Zelenski.
The Basics
Lexical analysis or scanning is the process where the stream of characters making up the
source program is read from left-to-right and grouped into tokens. Tokens are sequences
of characters with a collective meaning. There are usually only a small number of tokens
for a programming language: constants (integer, double, char, string, etc.), operators
(arithmetic, relational, logical), punctuation, and reserved words.
while (i > 0)
i = i - 2;
Lexical
Analyzer
error messages
source
language
token
stream
while
(
i
>
0
)
i
=
...
The lexical analyzer takes a source program as input, and produces a stream of tokens as
output. The lexical analyzer might recognize particular instances of tokens such as:
3 or 255 for an integer constant token
"Fred" or "Wilma" for a string constant token
numTickets or queue for a variable token
Such specific instances are called lexemes. A lexeme is the actual character sequence
forming a token, the token is the general class that a lexeme belongs to. Some tokens
have exactly one lexeme (e.g., the > character); for others, there are many lexemes (e.g.,
integer constants).
The scanner is tasked with determining that the input stream can be divided into valid
symbols in the source language, but has no smarts about which token should come
where. Few errors can be detected at the lexical level alone because the scanner has a
very localized view of the source program without any context. The scanner can report
about characters that are not valid tokens (e.g., an illegal or unrecognized symbol) and a
few other malformed entities (illegal characters within a string constant, unterminated
comments, etc.) It does not look for or detect garbled sequences, tokens out of place,
undeclared identifiers, misspelled keywords, mismatched types and the like. For
example, the following input will not generate any errors in the lexical analysis phase,
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Lexical Analysis in Programming Languages: A Comprehensive Guide and more Summaries Compiler Design in PDF only on Docsity!

CS143 Handout 04 Summer 2012 June 27, 2012

Lexical Analysis

Handout written by Maggie Johnson and Julie Zelenski. The Basics Lexical analysis or scanning is the process where the stream of characters making up the source program is read from left-to-right and grouped into tokens. Tokens are sequences of characters with a collective meaning. There are usually only a small number of tokens for a programming language: constants (integer, double, char, string, etc.), operators (arithmetic, relational, logical), punctuation, and reserved words. while (i > 0) i = i - 2; Lexical Analyzer error messages source language token stream while ( i > 0 ) i = ... The lexical analyzer takes a source program as input, and produces a stream of tokens as output. The lexical analyzer might recognize particular instances of tokens such as: 3 or 255 for an integer constant token "Fred" or "Wilma" for a string constant token numTickets or queue for a variable token Such specific instances are called lexemes. A lexeme is the actual character sequence forming a token, the token is the general class that a lexeme belongs to. Some tokens have exactly one lexeme (e.g., the > character); for others, there are many lexemes (e.g., integer constants). The scanner is tasked with determining that the input stream can be divided into valid symbols in the source language, but has no smarts about which token should come where. Few errors can be detected at the lexical level alone because the scanner has a very localized view of the source program without any context. The scanner can report about characters that are not valid tokens (e.g., an illegal or unrecognized symbol) and a few other malformed entities (illegal characters within a string constant, unterminated comments, etc.) It does not look for or detect garbled sequences, tokens out of place, undeclared identifiers, misspelled keywords, mismatched types and the like. For example, the following input will not generate any errors in the lexical analysis phase,

because the scanner has no concept of the appropriate arrangement of tokens for a declaration. The syntax analyzer will catch this error later in the next phase. int a double } switch b[2] =; Furthermore, the scanner has no idea how tokens are grouped. In the above sequence, it returns b , [ , 2 , and ] as four separate tokens, having no idea they collectively form an array access. The lexical analyzer can be a convenient place to carry out some other chores like stripping out comments and white space between tokens and perhaps even some features like macros and conditional compilation (although often these are handled by some sort of preprocessor which filters the input before the compiler runs). Scanner Implementation 1: Loop and Switch There are two primary methods for implementing a scanner. The first is a program that is hard-coded to perform the scanning tasks. The second uses regular expression and finite automata theory to model the scanning process. A "loop & switch" implementation consists of a main loop that reads characters one by one from the input file and uses a switch statement to process the character(s) just read. The output is a list of tokens and lexemes from the source program. The following program fragment shows a skeletal implementation of a simple loop and switch scanner. The main program calls InitScanner and loops calling ScanOneToken until EOF. ScanOneToken reads the next character from the file and switch es off that char to decide how to handle what is coming up next in the file. The return values from the scanner can be passed on to the parser in the next phase. #define T_SEMICOLON ';' // use ASCII values for single char tokens #define T_LPAREN '(' #define T_RPAREN ')' #define T_ASSIGN '=' #define T_DIVIDE '/' ... #define T_WHILE 257 // reserved words #define T_IF 258 #define T_RETURN 259 ... #define T_IDENTIFIER 268 // identifiers, constants, etc. #define T_INTEGER 269 #define T_DOUBLE 270 #define T_STRING 271 #define T_END 349 // code used when at end of file #define T_UNKNOWN 350 // token was unrecognized by scanner

token->val.stringValue[0] = ch; for (i = 1; islower(ch = getc(fp)); i++) token->val.stringValue[i] = ch; // gather lowercase ungetc(ch, fp); token->val.stringValue[i] = '\0'; if (lookup_symtab(token->val.stringValue) == NULL) add_symtab(token->val.stringValue); // get symbol for ident return T_IDENTIFIER; case '0': case '1': case '2': case '3': //.... and other digits token->type = T_INTEGER; token->val.intValue = ch - '0'; while (isdigit(ch = getc(fp))) // convert digit char to number token->val.intValue = token->val.intValue * 10 + ch - '0'; ungetc(ch, fp); return T_INTEGER; case EOF: return T_END; default: // anything else is not recognized token->val.intValue = ch; token->type = T_UNKNOWN; return T_UNKNOWN; } } The mythical source language tokenized by the above scanner requires that reserved words be in all upper case and identifiers in all lower case. This convenient feature makes it easy for the scanner to choose which path to pursue after reading just one character. It is sometimes necessary to design the scanner to "look ahead" before deciding what path to follow— notice the handling for the '/' character which peeks at the next character to check whether the first slash is followed by another slash or star which indicates the beginning of a comment. If not, the extra character is pushed back onto the input stream and the token is interpreted as the single char operator for division. Loop-and-switch scanners are sometimes called ad hoc scanners, indicating their design and purpose of solving a specific instance rather a general problem. For a sufficiently reasonable set of token types, a hand coded, loop and switch scanner might be all that’s needed— it requires no other tools. The gcc front-end uses an ad hoc scanner, in fact. On the other hand, gcc ’s C lexer is over 2,500 lines of code; verifying that such an amount of code is correct is much harder if your lexer does not see the extent of use that gcc’s front-end experiences.

Scanner Implementation 2: Regular Expressions and Finite Automata The other method of implementing a scanner is using regular expressions and finite automata. A quick detour for some background review and then let’s see how we can generate a scanner building on techniques from automata theory. Regular expression review We assume that you are well acquainted with regular expressions and all this is old news to you. symbol an abstract entity that we shall not define formally (such as “point” in geometry). Letters, digits and punctuation are examples of symbols. alphabet a finite set of symbols out of which we build larger structures. An alphabet is typically denoted using the Greek sigma ∑, e.g., ∑ = {0,1}. string a finite sequence of symbols from a particular alphabet juxtaposed. For example: a , b , c , are symbols and abcb is a string. empty string denoted ε (or sometimes ∂) is the string consisting of zero symbols. formal language ∑* the set of all possible strings that can be generated from a given alphabet. regular expressions rules that define exactly the set of words that are valid tokens in a formal language. The rules are built up from three operators: concatenation xy alternation x | y x or y repetition x x* repeated 0 or more times Formally, the set of regular expressions can be defined by the following recursive rules:

  1. Every symbol of ∑ is a regular expression
  2. ε is a regular expression
  3. if r 1 and r 2 are regular expressions, so are ( r 1 ) r 1 r 2 r 1 | r 2 r 1 *
  4. Nothing else is a regular expression. Here are a few practice exercises to get you thinking about regular expressions again. Give regular expressions for the following languages over the alphabet {a,b}: all strings beginning and ending in a ______________________ all strings with an odd number of a's ______________________ all strings without two consecutive a's ______________________

Now that we remember what FAs are, here is a regular expression and a simple finite automata that recognizes an integer. (0|1|2|3|4|5|6|7|8|9)+ Here is an FA that recognizes a subset of tokens in the Pascal language:

  • 1

A

B C

D

E

F

G

H

letter letter| digit digit digit {

+,- : (^) = < > =

= = . ; ( ) } Anything but } } This FA handles only a subset of all Pascal tokens but it should give you an idea of how an FA can be used to drive a scanner. The numbered/lettered states are final states. The loops on states 1 and 2 continue to execute until a character other than a letter or digit is read. For example, when scanning "temp := temp + 1;" it would report the first token at final state 1 after reading the ":" having recognized the lexeme "temp" as an identifier token. digit not digit not digit digit int

What happens in an FA-driven scanner is we read the source program one character at a time beginning with the start state. As we read each character, we move from our current state to the next by following the appropriate transition for that. When we end up in a final state, we perform an action associated with that final state. For example, the action associated with state 1 is to first check if the token is a reserved word by looking it up in the reserved word list. If it is, the reserved word is passed to the token stream being generated as output. If it is not a reserved word, it is an identifier so a procedure is called to check if the name is in the symbol table. If it is not there, it is inserted into the table. Once a final state is reached and the associated action is performed, we pick up where we left off at the first character of the next token and begin again at the start state. If we do not end in a final state or encounter an unexpected symbol while in any state, we have an error condition. For example, if you run "ASC@I" through the above FA, we would error out of state 1. From regular expressions to NFA So that’s how FAs can be used to implement scanners. Now we need to look at how to create an FA given the regular expressions for our tokens. There is a looser definition of an FA that is especially useful to us in this process. A nondeterministic finite automaton (NFA) has:

  1. A finite set of states with one start state and some (maybe none) final state
  2. An alphabet ∑ of possible input symbols.
  3. A finite set of transitions that describe how to proceed from one state to another along edges labeled with symbols from the alphabet (but not ε). We allow the possibility of more than one edge with the same label from any state, and some states for which certain input letters have no edge. Here is an NFA that accepts the language (0|1)(000|111)(0|1)** Notice that there is more than one path through the machine for a given string. For example, 000 can take you to a final state, or it can leave you in the start state. This is 0 0, 1 1 0 1 1 (^0) 0,

Rule 5 : There is an ε-NFA that accepts r1*: Going Deterministic: Subset Construction Using Thompson's construction, we can build an NFA from a regular expression, we can then employ subset construction to convert the NFA to a DFA. Subset construction is an algorithm for constructing the deterministic FA that recognizes the same language as the original nondeterministic FA. Each state in the new DFA is made up of a set of states from the original NFA. The start state of the DFA will be the start state of NFA. The alphabet for both automata is the same. So, given a state of from the original NFA, an input symbol x takes us from this state to the union of original states that we can get to on that symbol x. We then have to analyze this new state with its definition of the original states, for each possible input symbol, building a new state in the DFA. The states of the DFA are all subsets of S, which is the set of original sets. There will be a max of 2n^ of these (because we might need to explore the entire power set), but there are usually far fewer. The final states of the DFA are those sets that contain a final state of the original NFA. Here is an example: This is non-deterministic for several reasons. For example, the two transitions on ‘b’ coming out of the final state, and no ‘b’ transition coming out of the start state. To create an equivalent deterministic FA, we begin by creating a start state, and analyzing where we go from the start state in the NFA, on all the symbols of the alphabet. We create a set ε (^) ε (^) ε Q F Q F ε ε ε Machine for R 1 Q F ε a b a a a b X1 (^) X2 X X

of states where applicable (or a sink hole if there were no such transitions). Notice if a final state is in the set of states, then that state in the DFA becomes a final state. (after creating start state and filling in its transitions) (after filling in transitions from {X2} state) We continue with this process analyzing all the new states that we create. We need to determine where we go in the NFA from each state, on all the symbols of the alphabet. (after filling in transitions from {X3, X4} state) And finally, filling in the transitions from {X2, X3} state brings us full circle. This is now a deterministic FA that accepts the same language as the original NFA. We have 5 states instead of original 4, a rather modest increase in this case. a a,b {X1}^ b {X2} a a,b b {X1} (^) {X2} a b {X3,X4} a a,b b {X1} {X2} a b {X3,X4} {X2,X3} a b a a,b b {X1} (^) {X2} a b {X3,X4} {X2,X3} a b a (^) b

o DO statement: DO 17 I = 1 , N , 2 which specifies that the set of statements following, up to and including that labeled 17 , is to be executed with I first assigned value 1 , incrementing by 2 , up through N.

  • No user-defined subroutines allowed, although several built-ins were provided including READ and WRITE for I/O.
  • Blanks are ignored in all statements, e.g., all the following are equivalent: o DIMENSION IN DATA(10000) o DIMENSIONINDATA(10000) o D I M E N S I O N I N D A T A ( 1 0 0 0 0 )
  • Reserved words are valid variable names, e.g., DIMENSION IF(100) declares an array called IF. The task of building the first compiler was a huge one compared to what we face today. First of all, we can see there were considerable challenges in the definition of the language itself. Allowing variables to have the same name as reserved words makes the scanning process much more difficult (although they didn’t really do “scanning” in the first compiler). Not requiring variable declarations meant the compiler had to remember lots of rules, and try to infer data type from context. In addition, these early compiler- writers had no formalisms to help as we do today, e.g., regular expressions and context- free grammars. Everything had to be designed and written from scratch. These formal techniques and the automatic tools based on them, allow us to build a fairly involved compiler over the course of 8 weeks, which is a far cry from 18 person-years. The first FORTRAN compiler itself started as three phase but by the end, it had expanded to six. The first phase read the entire source program, filing all relevant information into tables, and translating the arithmetic expressions into IBM 704 machine code. The second phase analyzed the entire structure of the program in order to generate optimal code for the DO statements and array references. Phase 3 was the code generator as we know it. After a certain amount of work on phase 2, additional phase were added (3, 4, 5 with 6 becoming the code generator) to do further optimizations. Recall that optimization was key to the acceptance of the compiler since early FORTRAN programmers were skeptical of a machine creating truly optimal assembly code. Comparing this structure to our "modern" compiler structure, we see that it does not map too well. In the original FORTRAN compiler, expressions are scanned, parsed and code-generated in the first and immensely complicated kitchen-sink of a first phase. One could view almost all of their inner four phases as just one great big optimization phase. So this compiler looks very different from what we know. Still, the same tasks were performed although in some cases less efficiently than what we can do today. The most

important achievement, besides creating the first working compiler (which was a considerable achievement), was the foundations laid in optimization techniques. We will see that many of these techniques are still used in modern compilers today. Bibliography A. Aho, R. Sethi, J. Ullman, Compilers: Principles, Techniques, and Tools. Reading, MA: Addison-Wesley, 1986. J. Backus, “The History of FORTRAN I, II, III.”, SIGPLAN Notices, Vol. 13, no. 8, August, 1978, pp. 165-180. J.P. Bennett, Introduction to Compiling Techniques. Berkshire, England: McGraw-Hill, 1990. D. Cohen, Introduction to Computer Theory, New York: Wiley, 1986. C. Fischer, R. LeBlanc, Crafting a Compiler. Menlo Park, CA: Benjamin/Cummings, 1988. J. Hopcroft, J. Ullman, Introduction to Automata Theory, Languages, and Computation, Reading MA: Addison-Wesley, 1979. S. Kleene, "Representation of Events in Nerve Nets and Finite Automata," in C. Shannon and J. McCarthy (eds), Automata Studies, Princeton, NJ: Princeton University Press, 1956. A. McGettrick, The Definition of Programming Languages. Cambridge: Cambridge University Press, 1980. T. Sudkamp, Languages and Machines: An Introduction to the Theory of Computer Science, Reading, MA: Addison-Wesley, 1988. R.L.Wexelblat, History of Programming Languages. London: Academic Press, 1981.