































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
































Prof. Fateman CS 164 Lecture 5
Prof. Fateman CS 164 Lecture 5
Prof. Fateman CS 164 Lecture 5
noun, verb, adjective, …
Identifier, Integer, Single-Float, Double-Float, operator
(perhaps single or multiple character), Comment,Keyword, Whitespace, string constant, …
Prof. Fateman CS 164 Lecture 5
Token categories
correspond to sets of strings
, some
of them finite sets, like keywords, but some, likeidentifiers, unbounded.
-^
Identifier: (typically)
Integer:
Keyword:
else
if
begin
Whitespace:
Prof. Fateman CS 164 Lecture 5
Output of lexical analysis is a stream of tokens... InLisp, we just make a list.. (token1 token2 token3 ….).[In an interactive system we would just burp outtokens as we find their ends; we don’t wait for EOF]
-^
This stream is the input to the next pass, the parser.^ –
(parser (lexer “filename”))
in Lisp.
Parser makes use of token distinctions^ –
An identifier FOO may be treated differently from akeyword ELSE.
(Some language have no keywords. Lisp.)
Prof. Fateman CS 164 Lecture 5
parser
\tif
(i
==
j)\n\t\tz
=
0;\n\telse\n\t\tz
=
1;
Integer, Keyword, operator, Identifier, Whitespace,
Prof. Fateman CS 164 Lecture 5
What kind of thing is it? Identifier? Number?Keyword?
-^
If it is an identifier, which one exactly? Foo? Bar?
-^
Where did this token appear in the source code(line/col).
-^
Whitespace and comments are skipped.
Prof. Fateman CS 164 Lecture 5
Recall:^ \tif (i == j)\n\t\tz = 0;\n\telse\n\t\tz = 1;
-^
Possible Token-lexeme groupings:^ –
Identifier: i, j, z
-^
Keyword: if, else
-^
Multi-character operator: ==
(not in MiniJava)
-^
Integer: 0, 1
-^
(, ), =, ; single character operators of the same name
-^
Illegal.
E.g. for MiniJava, ^ % $
… everything else?
Prof. Fateman CS 164 Lecture 5
Prof. Fateman CS 164 Lecture 5
Prof. Fateman CS 164 Lecture 5
A nicer design limits the amount of lookaheadneeded
Even our simple example has lookahead issues
ii
vs.
if
vs.
&& (not in MJ…)
But this uses only one or two characters, and is
certainly resolved by the time you find whitespace, or another character, e.g. X&Y or X&&Y.In C, consider a+++++b. Is it a++ + ++b? Or(a++)++ +b
or some other breakup?
Prof. Fateman CS 164 Lecture 5
each lexeme (probably. E.g.convert_string_to_integer(“12345") )
Prof. Fateman CS 164 Lecture 5
Prof. Fateman CS 164 Lecture 5
Alphabet = Englishcharacters
-^
Language = Englishsentences
-^
Not every string ofEnglish characters is anEnglish sentence.
Alphabet = ASCII,
-^
Language = C programs,
-^
Note: ASCII characterset is different fromEnglish character set.
-^
UNICODE is another,much larger set, used byJava, Common Lisp.