A token is a string of characters, categorized according to the rules as a symbol (e.g., IDENTIFIER, NUMBER, COMMA). The process of forming tokens from an input stream of characters is called tokenization, and the lexer categorizes them according to a symbol type. A token can look like anything that is useful for processing an input text stream or text file.
A lexical analyzer generally does nothing with combinations of tokens, a task left for a parser. For example, a typical lexical analyzer recognizes parentheses as tokens, but does nothing to ensure that each "(" is matched with a ")".
Consider this expression in the C programming language:
sum=3+2;
Tokenized in the following table:
Lexeme | Token type |
---|---|
sum | Identifier |
= | Assignment operator |
3 | Integer literal |
+ | Addition operator |
2 | Integer literal |
; | End of statement |
Tokens are frequently defined by regular expressions, which are understood by a lexical analyzer generator such as lex. The lexical analyzer (either generated automatically by a tool like lex, or hand-crafted) reads in a stream of characters, identifies the lexemes in the stream, and categorizes them into tokens. This is called "tokenizing." If the lexer finds an invalid token, it will report an error.
Following tokenizing is parsing. From there, the interpreted data may be loaded into data structures for general use, interpretation, or compiling.
Read more about this topic: Lexical Analysis
Famous quotes containing the word token:
“The token woman carries a bouquet of hothouse celery
and a stenographers pad; she will take
the minutes, perk the coffee, smile
like a plastic daisy and put out
the black cat of her sensuous anger
to howl on the fence all night.”
—Marge Piercy (b. 1936)
“With the gracious consent of the audience, you will be made to don the red tophatMa token phrase that the courts had evolved, whose true meaning was known to every schoolboy.”
—Vladimir Nabokov (18991977)
“A woman with cut hair is a filthy spectacle, and much like a monster ... it being natural and comely to women to nourish their hair, which even God and nature have given them for a covering, a token of subjection, and a natural badge to distinguish them from men.”
—William Prynne (16001669)